Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow checkbox fields to support custom strings #2235

Merged
merged 9 commits into from
May 24, 2014
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
node_modules
dist/iD.js
dist/iD.min.js
dist/iD.css
Expand Down
8 changes: 8 additions & 0 deletions data/presets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,16 @@ en:
label: Type
oneway:
label: One Way
options:
undefined: Assumed to be No
yes: Yes
no: No
oneway_yes:
label: One Way
options:
undefined: Assumed to be Yes
yes: Yes
no: No
opening_hours:
label: Hours
operator:
Expand Down
18 changes: 16 additions & 2 deletions data/presets/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,27 @@
"oneway": {
"key": "oneway",
"type": "check",
"label": "One Way"
"label": "One Way",
"strings": {
"options": {
"undefined": "Assumed to be No",
"yes": "Yes",
"no": "No"
}
}
},
"oneway_yes": {
"key": "oneway",
"type": "check",
"default": "yes",
"label": "One Way"
"label": "One Way",
"strings": {
"options": {
"undefined": "Assumed to be Yes",
"yes": "Yes",
"no": "No"
}
}
},
"opening_hours": {
"key": "opening_hours",
Expand Down
9 changes: 8 additions & 1 deletion data/presets/fields/oneway.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"key": "oneway",
"type": "check",
"label": "One Way"
"label": "One Way",
"strings": {
"options": {
"undefined": "Assumed to be No",
"yes": "Yes",
"no": "No"
}
}
}
9 changes: 8 additions & 1 deletion data/presets/fields/oneway_yes.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
"key": "oneway",
"type": "check",
"default": "yes",
"label": "One Way"
"label": "One Way",
"strings": {
"options": {
"undefined": "Assumed to be Yes",
"yes": "Yes",
"no": "No"
}
}
}
2 changes: 1 addition & 1 deletion data/presets/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3822,7 +3822,7 @@
"highway/motorway": {
"icon": "highway-motorway",
"fields": [
"oneway",
"oneway_yes",
"maxspeed",
"structure",
"access",
Expand Down
4 changes: 2 additions & 2 deletions data/presets/presets/highway/motorway.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"icon": "highway-motorway",
"fields": [
"oneway",
"oneway_yes",
"maxspeed",
"structure",
"access",
Expand All @@ -17,4 +17,4 @@
},
"terms": [],
"name": "Motorway"
}
}
14 changes: 12 additions & 2 deletions dist/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,20 @@
"label": "Type"
},
"oneway": {
"label": "One Way"
"label": "One Way",
"options": {
"undefined": "Assumed to be No",
"yes": "Yes",
"no": "No"
}
},
"oneway_yes": {
"label": "One Way"
"label": "One Way",
"options": {
"undefined": "Assumed to be Yes",
"yes": "Yes",
"no": "No"
}
},
"opening_hours": {
"label": "Hours"
Expand Down
42 changes: 31 additions & 11 deletions js/id/ui/preset/check.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
iD.ui.preset.check =
iD.ui.preset.defaultcheck = function(field) {
iD.ui.preset.defaultcheck = function(field, context) {
var event = d3.dispatch('change'),
values = field.type === 'check' ?
[undefined, 'yes', 'no'] :
[undefined, 'yes'],
value,
box,
text,
label;
options = field.strings && field.strings.options,
values = [],
texts = [],
value, box, text, label;

if (options) {
for (var k in options) {
values.push(k === 'undefined' ? undefined : k);
texts.push(field.t('check.' + k, { 'default': options[k] }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely to be reached too early in initialization to use field.t or t -- I think it happens before the user's locale preference is set, so it will always return the English translation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, never mind. Wasn't thinking straight.

}
} else {
values = [undefined, 'yes'];
texts = [t('inspector.unknown'), t('inspector.check.yes')];
if (field.type === 'check') {
values.push('no');
texts.push(t('inspector.check.no'));
}
}

// hack: pretend oneway field is a oneway_yes field if `junction=roundabout` is set.
if (field.id === 'oneway') {
var ids = context.selectedIDs(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using context.selectedIDs(), define a check.entity setter function -- this is the convention for other fields like address and wikipedia.

way = ids.length && context.entity(ids[0]);
if (way && way.tags.junction === 'roundabout') {
texts.shift();
texts.unshift(t('presets.fields.oneway_yes.check.undefined', { 'default': 'Assumed to be Yes' }));
}
}

var check = function(selection) {
selection.classed('checkselect', 'true');
Expand All @@ -24,7 +45,7 @@ iD.ui.preset.defaultcheck = function(field) {
.attr('id', 'preset-input-' + field.id);

enter.append('span')
.text(t('inspector.unknown'))
.text(texts[0])
.attr('class', 'value');

box = label.select('input')
Expand All @@ -42,8 +63,7 @@ iD.ui.preset.defaultcheck = function(field) {
value = tags[field.key];
box.property('indeterminate', field.type === 'check' && !value);
box.property('checked', value === 'yes');
text.text(value ? t('inspector.check.' + value, {default: value}) :
field.type === 'check' ? t('inspector.unknown') : t('inspector.check.no'));
text.text(texts[values.indexOf(value)]);
label.classed('set', !!value);
};

Expand Down