Make sure all combo event listeners can handle accept/cancel w/o datum

(re: #5637, re: #5618)

This can happen if the user enters a value that is not matched to
one of the combo suggestions.

Also make sure to set `this` to the input field.  It might not be
already if the event was triggered from a keypress instead of a click.
This commit is contained in:
Bryan Housel
2019-01-22 23:12:40 -05:00
parent 0c7b9a631e
commit 4583e2d4d7
4 changed files with 56 additions and 28 deletions
+30 -19
View File
@@ -147,25 +147,8 @@ export function uiFieldLocalized(field, context) {
input
.call(brandCombo
.fetcher(fetchBrandNames(preset, allSuggestions))
.on('accept', function(d) {
var entity = context.entity(_entity.id); // get latest
var tags = entity.tags;
var geometry = entity.geometry(context.graph());
var removed = preset.unsetTags(tags, geometry);
for (var k in tags) {
tags[k] = removed[k]; // set removed tags to `undefined`
}
tags = d.suggestion.setTags(tags, geometry);
utilGetSetValue(input, tags.name);
dispatch.call('change', this, tags);
})
.on('cancel', function() {
// user hit escape, remove whatever is after the '-'
var name = utilGetSetValue(input);
name = name.split('-', 2)[0].trim();
utilGetSetValue(input, name);
dispatch.call('change', this, { name: name });
})
.on('accept', acceptBrand)
.on('cancel', cancelBrand)
);
}
}
@@ -216,6 +199,34 @@ export function uiFieldLocalized(field, context) {
function acceptBrand(d) {
if (!d) {
cancelBrand();
return;
}
var entity = context.entity(_entity.id); // get latest
var tags = entity.tags;
var geometry = entity.geometry(context.graph());
var removed = preset.unsetTags(tags, geometry);
for (var k in tags) {
tags[k] = removed[k]; // set removed tags to `undefined`
}
tags = d.suggestion.setTags(tags, geometry);
utilGetSetValue(input, tags.name);
dispatch.call('change', this, tags);
}
function cancelBrand() {
// user hit escape, remove whatever is after the ' - '
var name = utilGetSetValue(input);
name = name.split(' - ', 2)[0].trim();
utilGetSetValue(input, name);
dispatch.call('change', this, { name: name });
}
function fetchBrandNames(preset, suggestions) {
var pTag = preset.id.split('/', 2);
var pKey = pTag[0];