|
| 1 | +import { dispatch as d3_dispatch } from 'd3-dispatch'; |
| 2 | +import { select as d3_select } from 'd3-selection'; |
| 3 | + |
| 4 | +import { t } from '../../core/localizer'; |
| 5 | +import { utilGetSetValue, utilNoAuto, utilRebind } from '../../util'; |
| 6 | + |
| 7 | +export function uiFieldSources(field, context) { |
| 8 | + let dispatch = d3_dispatch('change'); |
| 9 | + let items = d3_select(null); |
| 10 | + let _tags = {}; |
| 11 | + let _selection = d3_select(null); |
| 12 | + let _pendingChange; |
| 13 | + |
| 14 | + const mainKey = 'source'; |
| 15 | + const sourceHeader = mainKey + ':'; |
| 16 | + |
| 17 | + // Pre-selected subkeys to show |
| 18 | + const possibleSourceSubkeys = [{key:'name'}, {key:'url'}, {key:'date'}]; |
| 19 | + |
| 20 | + function scheduleChange() { |
| 21 | + if (!_pendingChange) return; |
| 22 | + dispatch.call('change', this, _pendingChange); |
| 23 | + _pendingChange = null; |
| 24 | + _selection.call(sources); |
| 25 | + } |
| 26 | + |
| 27 | + function valueChange(d3_event, d) { |
| 28 | + // exit if this is a multiselection and no value was entered |
| 29 | + if (typeof d.key !== 'string' && !this.value) return; |
| 30 | + |
| 31 | + var key = sourceHeader + d.key; |
| 32 | + |
| 33 | + _pendingChange = _pendingChange || {}; |
| 34 | + |
| 35 | + var value = context.cleanTagValue(this.value); |
| 36 | + |
| 37 | + _pendingChange[key] = value === '' ? undefined : value; |
| 38 | + _tags[key] = value === '' ? undefined : value; |
| 39 | + scheduleChange(); |
| 40 | + } |
| 41 | + |
| 42 | + function mainChange() { |
| 43 | + _pendingChange = _pendingChange || {}; |
| 44 | + var value = context.cleanTagValue(this.value); |
| 45 | + _pendingChange[mainKey] = value === '' ? undefined : value; |
| 46 | + _tags[mainKey] = value === '' ? undefined : value; |
| 47 | + scheduleChange(); |
| 48 | + } |
| 49 | + |
| 50 | + function sources(selection) { |
| 51 | + _selection = selection; |
| 52 | + |
| 53 | + var wrap = selection.selectAll('.form-field-input-wrap') |
| 54 | + .data([0]); |
| 55 | + |
| 56 | + selection.exit() |
| 57 | + .style('top', '0') |
| 58 | + .style('max-height', '240px') |
| 59 | + .transition() |
| 60 | + .duration(200) |
| 61 | + .style('opacity', '0') |
| 62 | + .style('max-height', '0px') |
| 63 | + .remove(); |
| 64 | + |
| 65 | + wrap = wrap.enter() |
| 66 | + .append('div') |
| 67 | + .attr('class', 'form-field-input-wrap form-field-input-' + field.type) |
| 68 | + .merge(wrap); |
| 69 | + |
| 70 | + // source key |
| 71 | + wrap.selectAll('input') |
| 72 | + .data([0]) |
| 73 | + .enter() |
| 74 | + .append('input') |
| 75 | + .attr('class', 'main-value') |
| 76 | + .attr('type', 'text') |
| 77 | + .attr('placeholder', t('inspector.source.main_input')) |
| 78 | + .call(utilNoAuto) |
| 79 | + .on('change', mainChange) |
| 80 | + .on('blur', mainChange); |
| 81 | + |
| 82 | + var list = wrap.selectAll('ul') |
| 83 | + .data([0]); |
| 84 | + |
| 85 | + list = list.enter() |
| 86 | + .append('ul') |
| 87 | + .attr('class', 'rows') |
| 88 | + .merge(list); |
| 89 | + |
| 90 | + list = list.merge(list); |
| 91 | + |
| 92 | + // source:*= keys |
| 93 | + items = list.selectAll('li.labeled-input-source') |
| 94 | + .data(possibleSourceSubkeys); |
| 95 | + |
| 96 | + items = items.enter() |
| 97 | + .append('li') |
| 98 | + .attr('class', 'labeled-input-source'); |
| 99 | + |
| 100 | + items |
| 101 | + .append('input') |
| 102 | + .attr('type', 'text') |
| 103 | + .attr('class', 'value') |
| 104 | + .attr('placeholder', function(d) { |
| 105 | + return t('inspector.source.' + d.key); |
| 106 | + }) |
| 107 | + .call(utilNoAuto) |
| 108 | + .call(utilGetSetValue, function(d) { |
| 109 | + return _tags[sourceHeader + d.key]; |
| 110 | + }) |
| 111 | + .on('change', valueChange) |
| 112 | + .on('blur', valueChange); |
| 113 | + |
| 114 | + items.exit() |
| 115 | + .remove(); |
| 116 | + |
| 117 | + utilGetSetValue(_selection.selectAll('.value'), function(d) { |
| 118 | + return (_tags[sourceHeader + d.key] === undefined) ? '' : _tags[sourceHeader + d.key]; |
| 119 | + }); |
| 120 | + |
| 121 | + utilGetSetValue(_selection.selectAll('.main-value'), function() { |
| 122 | + return (_tags[mainKey] === undefined) ? '' : _tags[mainKey]; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + sources.tags = function(tags){ |
| 127 | + if (!arguments.length) return _tags; |
| 128 | + _tags = tags; |
| 129 | + |
| 130 | + _selection.call(sources); |
| 131 | + }; |
| 132 | + |
| 133 | + return utilRebind(sources, dispatch, 'on'); |
| 134 | +} |
0 commit comments