From 1a9c111d782495063a9911211d2f525057a973e0 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 22 Apr 2016 21:22:24 -0400 Subject: [PATCH] Set field.keys so that delete/undo buttons work For multiselect fields, field.key should have a ':' on the end of it - we use as the prefix, not the real key field.keys should contain the array of real keys being modified see https://github.com/openstreetmap/iD/blob/master/js/id/ui/preset.js#L17-L45 The address field preset works like this also.. It contains an array of key.fields for all the subfields that might be set. For multiselect fields, we don't know ahead of time what all the subfields are, so we append to the list in `change()` and rebuild list in `tags()` --- data/presets.yaml | 2 +- data/presets/fields.json | 2 +- .../fields/recycling/recycling_choices.json | 2 +- js/id/ui/preset/multiselect.js | 28 +++++++++++-------- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/data/presets.yaml b/data/presets.yaml index c79e1c932..572c41e69 100644 --- a/data/presets.yaml +++ b/data/presets.yaml @@ -774,7 +774,7 @@ en: # 'railway=*' label: Type recycling/recycling_choices: - # 'recycling=*' + # 'recycling:=*' label: Accepts ref: # 'ref=*' diff --git a/data/presets/fields.json b/data/presets/fields.json index 1c7bb7395..0a88d5232 100644 --- a/data/presets/fields.json +++ b/data/presets/fields.json @@ -1021,7 +1021,7 @@ "label": "Type" }, "recycling/recycling_choices": { - "key": "recycling", + "key": "recycling:", "type": "multiselect", "label": "Accepts" }, diff --git a/data/presets/fields/recycling/recycling_choices.json b/data/presets/fields/recycling/recycling_choices.json index 9500270e6..57398e714 100644 --- a/data/presets/fields/recycling/recycling_choices.json +++ b/data/presets/fields/recycling/recycling_choices.json @@ -1,5 +1,5 @@ { - "key": "recycling", + "key": "recycling:", "type": "multiselect", "label": "Accepts" } diff --git a/js/id/ui/preset/multiselect.js b/js/id/ui/preset/multiselect.js index ad54d2230..27424b867 100644 --- a/js/id/ui/preset/multiselect.js +++ b/js/id/ui/preset/multiselect.js @@ -9,7 +9,10 @@ iD.ui.preset.multiselect = function(field, context) { input, isInitialized; - field.key += ':'; + // ensure field.key ends with a ':' + if (field.key.match(/.*:$/) === null) { + field.key += ':'; + } function getOptStringKey(val) { if (optstrings) { @@ -35,7 +38,7 @@ iD.ui.preset.multiselect = function(field, context) { bObj[obj.key] = obj; }); // Return all elements in a, unless in b - return a.filter(function(obj){ + return a.filter(function(obj) { return !(obj.key in bObj); }); } @@ -71,10 +74,10 @@ iD.ui.preset.multiselect = function(field, context) { .each(function() { if (optstrings) { strings = Object.keys(optstrings).map(function(k) { - return { - key: k, - value: field.t('options.' + k, { 'default': optstrings[k] }) - }; + return { + key: k, + value: field.t('options.' + k, { 'default': optstrings[k] }) + }; }); dispatch.init(); isInitialized = true; @@ -111,12 +114,12 @@ iD.ui.preset.multiselect = function(field, context) { function update(data) { var chips = multiselectContainer.selectAll('.chips').data(data); - var chip = chips.enter() + var enter = chips.enter() .insert('li', 'input') .attr('class', 'chips'); - chip.append('span'); - chip.append('a'); + enter.append('span'); + enter.append('a'); chips.select('span').text(function(d) {return d.value;}); @@ -130,8 +133,8 @@ iD.ui.preset.multiselect = function(field, context) { function comboValues(d) { return { - value: d.value, - title: d.value + value: d.value, + title: d.value }; } @@ -142,6 +145,7 @@ iD.ui.preset.multiselect = function(field, context) { var t = {}; t[field.key + key] = 'yes'; input.value(''); + field.keys.push(field.key + key); dispatch.change(t); } } @@ -175,6 +179,8 @@ iD.ui.preset.multiselect = function(field, context) { } }); + field.keys = _.map(_.pluck(tagsData, 'key'), function(v) { return field.key + v; }); + update(tagsData); if (isInitialized) {