Support option strings for combo fields (#1259, #1709)

This commit is contained in:
Bryan Housel
2014-07-21 14:37:42 -04:00
parent f8a99d03e2
commit a68cb30fd2

View File

@@ -1,52 +1,61 @@
iD.ui.preset.combo =
iD.ui.preset.typeCombo = function(field) {
var event = d3.dispatch('change'),
optstrings = field.strings && field.strings.options,
strings = {},
input;
if (optstrings) {
_.each(optstrings, function(v, k) {
strings[k] = field.t('options.' + k, { 'default': v });
});
} else {
iD.taginfo().values({key: field.key}, function(err, data) {
if (!err) {
_.each(_.pluck(data, 'value'), function(k) {
strings[k] = k.replace(/_+/g, ' ');
});
}
});
}
function combo(selection) {
var combobox = d3.combobox();
input = selection.selectAll('input')
.data([0]);
input.enter().append('input')
var enter = input.enter()
.append('input')
.attr('type', 'text')
.attr('id', 'preset-input-' + field.id);
if (optstrings) { enter.attr('readonly', 'readonly'); }
input
.call(combobox)
.on('change', change)
.on('blur', change)
.each(function() {
if (field.options) {
options(field.options);
} else {
iD.taginfo().values({
key: field.key
}, function(err, data) {
if (!err) options(_.pluck(data, 'value'));
});
}
var keys = _.keys(strings);
combobox.data(keys.map(function(k) {
var o = {};
o.title = o.value = strings[k];
return o;
}));
input.attr('placeholder', field.placeholder() ||
(keys.length < 3 ? '' : keys.slice(0, 3).join(', ') + '...'));
});
function options(opts) {
combobox.data(opts.map(function(d) {
var o = {};
o.title = o.value = d.replace(/_+/g, ' ');
return o;
}));
input.attr('placeholder', field.placeholder() ||
(opts.length < 3 ? '' : opts.slice(0, 3).join(', ') + '...'));
}
}
function change() {
var value = input.value()
.split(';')
.map(function(s) { return s.trim(); })
.join(';')
.replace(/\s+/g, '_');
var optstring = _.find(_.keys(strings), function(k) { return strings[k] === input.value(); }),
value = optstring || (input.value()
.split(';')
.map(function(s) { return s.trim(); })
.join(';')
.replace(/\s+/g, '_'));
if (field.type === 'typeCombo' && !value) value = 'yes';
@@ -56,8 +65,9 @@ iD.ui.preset.typeCombo = function(field) {
}
combo.tags = function(tags) {
var value = tags[field.key] || '';
if (field.type === 'typeCombo' && value === 'yes') value = '';
var key = tags[field.key],
value = strings[key] || key || '';
if (field.type === 'typeCombo' && value.toLowerCase() === 'yes') value = '';
input.value(value);
};