Localize maxspeed field (mph and km/h)

This commit is contained in:
Ansis Brammanis
2013-04-01 19:36:39 -04:00
parent af7d003b88
commit bae7b13425
10 changed files with 114 additions and 7 deletions

View File

@@ -100,7 +100,8 @@ fs.writeFileSync('data/data.js', 'iD.data = ' + stringify({
defaults: rp('defaults.json'),
categories: rp('categories.json'),
fields: rp('fields.json')
}
},
imperial: r('imperial.json')
}) + ';');
// Push changes from data/core.yaml into data/locales.js

View File

@@ -1144,6 +1144,24 @@ input[type=number] {
text-align: center;
}
#preset-input-maxspeed {
border-right: none;
border-radius: 0;
width: 80%;
}
.form-field .maxspeed-unit {
border-radius: 0 0 4px 0;
border: 1px solid #ccc;
border-top: none;
height: 30px;
width: 20%;
float: right;
padding: 5px;
text-align: center;
}
/* Preset form address */
.form-field .addr-housename {

View File

@@ -13,7 +13,8 @@ iD.data = {
path + 'data/presets/presets.json',
path + 'data/presets/defaults.json',
path + 'data/presets/categories.json',
path + 'data/presets/fields.json'
path + 'data/presets/fields.json',
path + 'data/imperial.json'
], d3.json, function (err, data) {
iD.data = {
@@ -27,7 +28,8 @@ iD.data = {
defaults: data[6],
categories: data[7],
fields: data[8]
}
},
imperial: data[9]
};
callback();

9
data/imperial.json Normal file

File diff suppressed because one or more lines are too long

View File

@@ -198,7 +198,7 @@
},
"maxspeed": {
"key": "maxspeed",
"type": "combo",
"type": "maxspeed",
"label": "Speed Limit"
},
"name": {

View File

@@ -1,5 +1,5 @@
{
"key": "maxspeed",
"type": "combo",
"type": "maxspeed",
"label": "Speed Limit"
}
}

View File

@@ -23,6 +23,7 @@
"combo",
"defaultcheck",
"text",
"maxspeed",
"number",
"tel",
"email",

View File

@@ -100,6 +100,7 @@
<script src='js/id/ui/preset/combo.js'></script>
<script src='js/id/ui/preset/defaultcheck.js'></script>
<script src='js/id/ui/preset/input.js'></script>
<script src='js/id/ui/preset/maxspeed.js'></script>
<script src='js/id/ui/preset/radio.js'></script>
<script src='js/id/ui/preset/textarea.js'></script>
<script src='js/id/ui/preset/wikipedia.js'></script>

View File

@@ -15,7 +15,9 @@ iD.ui.preset = function(context, entity, preset) {
field.reference = iD.ui.TagReference(entity, {key: field.key});
if (field.type === 'address' || field.type === 'wikipedia') {
if (field.type === 'address' ||
field.type === 'wikipedia' ||
field.type === 'maxspeed') {
field.input.entity(entity);
}

View File

@@ -0,0 +1,73 @@
iD.ui.preset.maxspeed = function(field, context) {
var event = d3.dispatch('change', 'close'),
entity,
imperial,
input;
var metricValues = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
imperialValues = [20, 25, 30, 40, 45, 50, 55, 65, 70];
function maxspeed(selection) {
var combobox = d3.combobox();
input = selection.append('input')
.attr('type', 'text')
.attr('id', 'preset-input-' + field.id)
.on('change', change)
.on('blur', change)
.call(combobox);
var childNodes = context.graph().childNodes(context.entity(entity.id)),
centroid = d3.geom.polygon(_.pluck(childNodes, 'loc')).centroid();
imperial = _.any(iD.data.imperial.features, function(f) {
return _.any(f.geometry.coordinates, function(d) {
return iD.geo.pointInPolygon(centroid, d[0]);
});
});
selection.append('span')
.attr('class', 'maxspeed-unit')
.text(imperial ? 'mph' : 'km/h');
combobox.data((imperial ? imperialValues : metricValues).map(function(d) {
return {
value: d.toString(),
title: d.toString()
};
}));
}
function change() {
var value = input.property('value');
var t = {};
if (value) {
if (isNaN(value) || !imperial) {
t[field.key] = value;
} else {
t[field.key] = value + ' mph';
}
} else {
t[field.key] = '';
}
event.change(t);
}
maxspeed.tags = function(tags) {
var value = tags[field.key];
if (value && isNaN(value) && value.indexOf('mph') >= 0) value = parseInt(value, 10);
input.property('value', value || '');
};
maxspeed.focus = function() {
input.node().focus();
};
maxspeed.entity = function(_) {
entity = _;
};
return d3.rebind(maxspeed, event, 'on');
};