forked from openstreetmap/iD
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from pedromml/add-source-subfield-to-other-fi…
…elds Add source subfield to other fields
- Loading branch information
Showing
10 changed files
with
268 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import { | ||
select as d3_select | ||
} from 'd3-selection'; | ||
|
||
import { t } from '../core/localizer'; | ||
import { svgIcon } from '../svg/icon'; | ||
import { uiTooltip } from './tooltip'; | ||
import { utilGetSetValue, utilUniqueDomId } from '../util'; | ||
|
||
export function uiSourceSubfield(context, field, tags, dispatch) { | ||
|
||
var sourceSubfield = {}; | ||
|
||
let sourceInput = d3_select(null); | ||
let sourceKey = field.key + ':source'; | ||
let _sourceValue = tags[sourceKey]; | ||
|
||
// Adapted from renderEDTF from modules/ui/fields/date.js | ||
function renderSourceInput(selection) { | ||
let entries = selection.selectAll('div.entry') | ||
.data((typeof _sourceValue === 'string' || Array.isArray(_sourceValue)) ? [_sourceValue] : []); | ||
|
||
entries.exit() | ||
.style('top', '0') | ||
.style('max-height', '240px') | ||
.transition() | ||
.duration(200) | ||
.style('opacity', '0') | ||
.style('max-height', '0px') | ||
.remove(); | ||
|
||
let entriesEnter = entries.enter() | ||
.append('div') | ||
.attr('class', 'entry') | ||
.each(function() { | ||
var wrap = d3_select(this); | ||
|
||
let domId = utilUniqueDomId('source-' + field.safeid); | ||
let label = wrap | ||
.append('label') | ||
.attr('class', 'field-label') | ||
.attr('for', domId); | ||
|
||
let text = label | ||
.append('span') | ||
.attr('class', 'label-text'); | ||
|
||
text | ||
.append('span') | ||
.attr('class', 'label-textvalue') | ||
.call(t.append('inspector.field_source_label', { field_name: field.title })); | ||
|
||
text | ||
.append('span') | ||
.attr('class', 'label-textannotation'); | ||
|
||
label | ||
.append('button') | ||
.attr('class', 'remove-icon-source') | ||
.attr('title', t('icons.remove')) | ||
.on('click', function(d3_event) { | ||
d3_event.preventDefault(); | ||
|
||
// remove the UI item manually | ||
_sourceValue = undefined; | ||
|
||
let t = {}; | ||
t[sourceKey] = undefined; | ||
dispatch.call('change', this, t); | ||
|
||
renderSourceInput(selection); | ||
}) | ||
.call(svgIcon('#iD-operation-delete')); | ||
|
||
wrap | ||
.append('input') | ||
.attr('type', 'text') | ||
.attr('class', 'field-source-value') | ||
.on('blur', changeSourceValue) | ||
.on('change', changeSourceValue); | ||
}); | ||
|
||
entriesEnter | ||
.style('margin-top', '0px') | ||
.style('max-height', '0px') | ||
.style('opacity', '0') | ||
.transition() | ||
.duration(200) | ||
.style('margin-top', '10px') | ||
.style('max-height', '240px') | ||
.style('opacity', '1') | ||
.on('end', function() { | ||
d3_select(this) | ||
.style('max-height', '') | ||
.style('overflow', 'visible'); | ||
}); | ||
|
||
entries = entries.merge(entriesEnter); | ||
|
||
entries.order(); | ||
|
||
// allow removing the entry UIs even if there isn't a tag to remove | ||
entries.classed('present', true); | ||
|
||
utilGetSetValue(entries.select('.field-source-value'), function(d) { | ||
return typeof d === 'string' ? d : ''; | ||
}) | ||
.attr('title', function(d) { | ||
return Array.isArray(d) ? d.filter(Boolean).join('\n') : null; | ||
}) | ||
.attr('placeholder', function(d) { | ||
return Array.isArray(d) ? t('inspector.multiple_values') : t('inspector.field_source_placeholder'); | ||
}) | ||
.classed('mixed', function(d) { | ||
return Array.isArray(d); | ||
}); | ||
} | ||
|
||
function changeSourceValue(d3_event, d) { | ||
let value = context.cleanTagValue(utilGetSetValue(d3_select(this))) || undefined; | ||
// don't override multiple values with blank string | ||
if (!value && Array.isArray(d.value)) return; | ||
|
||
let t = {}; | ||
t[sourceKey] = value; | ||
d.value = value; | ||
dispatch.call('change', this, t); | ||
} | ||
|
||
function addSource(d3_event) { | ||
d3_event.preventDefault(); | ||
|
||
if (typeof _sourceValue !== 'string' && !Array.isArray(_sourceValue)) { | ||
_sourceValue = ''; | ||
} | ||
sourceInput.call(renderSourceInput); | ||
|
||
} | ||
|
||
sourceSubfield.button = function(labelEnter, container) { | ||
labelEnter | ||
.append('button') | ||
.attr('class', 'source-icon') | ||
.attr('title', function() { | ||
return t('inspector.field_source'); | ||
}) | ||
.call(svgIcon('#fas-at', 'inline')); | ||
|
||
container = container | ||
.merge(labelEnter); | ||
|
||
container.select('.field-label > .source-icon') // propagate bound data | ||
.on('click', addSource); | ||
}; | ||
|
||
|
||
sourceSubfield.body = function(selection) { | ||
sourceInput = selection.selectChild().selectAll('.field-source') | ||
.data([0]); | ||
|
||
sourceInput = sourceInput.enter() | ||
.append('div') | ||
.attr('class', 'field-source') | ||
.merge(sourceInput); | ||
|
||
sourceInput | ||
.call(renderSourceInput); | ||
}; | ||
|
||
return sourceSubfield; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
describe('iD.uiSourceSubfield', function() { | ||
let entity, context, selection, field; | ||
|
||
beforeEach(function() { | ||
entity = iD.osmNode({id: 'n12345'}); | ||
context = iD.coreContext().assetPath('../dist/').init(); | ||
context.history().merge([entity]); | ||
selection = d3.select(document.createElement('div')); | ||
field = iD.presetField('name', { key: 'name', type: 'text' }); | ||
}); | ||
|
||
it('adds an source subfield when the @ button is clicked', function(done) { | ||
var uiField = iD.uiField(context, field, ['n12345'], {show: true, wrap: true}); | ||
window.setTimeout(function() { // async, so data will be available | ||
selection.call(uiField.render); | ||
happen.click(selection.selectAll('.source-icon').node()); | ||
expect(selection.selectAll('.field-source').nodes().length).to.equal(1); | ||
done(); | ||
}, 20); | ||
}); | ||
|
||
it('creates field:source tag after setting the value', function(done) { | ||
var uiField = iD.uiField(context, field, ['n12345'], {show: true, wrap: true}); | ||
window.setTimeout(function() { // async, so data will be available | ||
selection.call(uiField.render); | ||
happen.click(selection.selectAll('.source-icon').node()); | ||
|
||
iD.utilGetSetValue(selection.selectAll('.field-source-value'), 'Book 1'); | ||
|
||
uiField.on('change', function(tags) { | ||
expect(tags).to.eql({'name:source': 'Book 1'}); | ||
}); | ||
happen.once(selection.selectAll('.field-source-value').node(), {type: 'change'}); | ||
done(); | ||
}, 20); | ||
}); | ||
|
||
|
||
it('removes the tag when the value is emptied', function(done) { | ||
var uiField = iD.uiField(context, field, ['n12345'], {show: true, wrap: true}); | ||
window.setTimeout(function() { // async, so data will be available | ||
selection.call(uiField.render); | ||
happen.click(selection.selectAll('.source-icon').node()); | ||
iD.utilGetSetValue(selection.selectAll('.field-source-value'), 'abc'); | ||
|
||
uiField.on('change', function(tags) { | ||
expect(tags).to.eql({'name:source': undefined}); | ||
}); | ||
|
||
iD.utilGetSetValue(selection.selectAll('.field-source-value'), ''); | ||
happen.once(selection.selectAll('.field-source-value').node(), {type: 'change'}); | ||
done(); | ||
}, 20); | ||
}); | ||
|
||
it('there is no @ button on main source field', function(done) { | ||
var uiField = iD.uiField(context, {...field, source: false}, ['n12345'], {show: true, wrap: true}); | ||
window.setTimeout(function() { // async, so data will be available | ||
selection.call(uiField.render); | ||
expect(selection.selectAll('.source-icon').nodes().length).to.equal(0); | ||
done(); | ||
}, 20); | ||
}); | ||
}); |