mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-23 16:49:40 +02:00
Localize numbers in numeric fields
This commit is contained in:
+33
-17
@@ -32,6 +32,7 @@ export function uiFieldText(field, context) {
|
||||
var _tags;
|
||||
var _phoneFormats = {};
|
||||
const isDirectionField = field.key.split(':').some(keyPart => keyPart === 'direction');
|
||||
const parseLocaleFloat = localizer.floatParser(localizer.languageCode());
|
||||
|
||||
if (field.type === 'tel') {
|
||||
fileFetcher.get('phone_formats')
|
||||
@@ -132,7 +133,8 @@ export function uiFieldText(field, context) {
|
||||
var raw_vals = input.node().value || '0';
|
||||
var vals = raw_vals.split(';');
|
||||
vals = vals.map(function(v) {
|
||||
var num = Number(v);
|
||||
v = v.trim();
|
||||
var num = parseLocaleFloat(v);
|
||||
if (isDirectionField) {
|
||||
const compassDir = cardinal[v.trim().toLowerCase()];
|
||||
if (compassDir !== undefined) {
|
||||
@@ -140,10 +142,10 @@ export function uiFieldText(field, context) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!isFinite(num)) {
|
||||
// do nothing if the value is neither a number, nor a cardinal direction
|
||||
return v.trim();
|
||||
}
|
||||
// do nothing if the value is neither a number, nor a cardinal direction
|
||||
if (!isFinite(num)) return v;
|
||||
num = parseFloat(num, 10);
|
||||
if (!isFinite(num)) return v;
|
||||
|
||||
num += d;
|
||||
// clamp to 0..359 degree range if it's a direction field
|
||||
@@ -153,7 +155,7 @@ export function uiFieldText(field, context) {
|
||||
}
|
||||
// make sure no extra decimals are introduced
|
||||
const numDecimals = v.includes('.') ? v.split('.')[1].length : 0;
|
||||
return clamped(num).toFixed(numDecimals);
|
||||
return clamped(num).toFixed(numDecimals).toLocaleString(localizer.languageCode());
|
||||
});
|
||||
input.node().value = vals.join(';');
|
||||
change()();
|
||||
@@ -393,17 +395,20 @@ export function uiFieldText(field, context) {
|
||||
// don't override multiple values with blank string
|
||||
if (!val && Array.isArray(_tags[field.key])) return;
|
||||
|
||||
if (!onInput) {
|
||||
if (field.type === 'number' && val) {
|
||||
var vals = val.split(';');
|
||||
vals = vals.map(function(v) {
|
||||
var num = Number(v);
|
||||
return isFinite(num) ? clamped(num) : v.trim();
|
||||
});
|
||||
val = vals.join(';');
|
||||
}
|
||||
utilGetSetValue(input, val);
|
||||
var displayVal = val;
|
||||
if (field.type === 'number' && val) {
|
||||
var vals = val.split(';');
|
||||
vals = vals.map(function(v) {
|
||||
v = v.trim();
|
||||
var num = parseLocaleFloat(v);
|
||||
if (!isFinite(num)) return v;
|
||||
num = parseFloat(num, 10);
|
||||
if (!isFinite(num)) return v;
|
||||
return clamped(num);
|
||||
});
|
||||
val = vals.join(';');
|
||||
}
|
||||
if (!onInput) utilGetSetValue(input, displayVal);
|
||||
t[field.key] = val || undefined;
|
||||
dispatch.call('change', this, t, onInput);
|
||||
};
|
||||
@@ -422,7 +427,18 @@ export function uiFieldText(field, context) {
|
||||
|
||||
var isMixed = Array.isArray(tags[field.key]);
|
||||
|
||||
utilGetSetValue(input, !isMixed && tags[field.key] ? tags[field.key] : '')
|
||||
var val = !isMixed && tags[field.key] ? tags[field.key] : '';
|
||||
if (field.type === 'number' && val) {
|
||||
var vals = val.split(';');
|
||||
vals = vals.map(function(v) {
|
||||
v = v.trim();
|
||||
var num = parseFloat(v, 10);
|
||||
if (!isFinite(num)) return v;
|
||||
return clamped(num).toLocaleString(localizer.languageCode());
|
||||
});
|
||||
val = vals.join(';');
|
||||
}
|
||||
utilGetSetValue(input, val)
|
||||
.attr('title', isMixed ? tags[field.key].filter(Boolean).join('\n') : undefined)
|
||||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : (field.placeholder() || t('inspector.unknown')))
|
||||
.classed('mixed', isMixed);
|
||||
|
||||
@@ -3,7 +3,7 @@ import { select as d3_select } from 'd3-selection';
|
||||
import * as countryCoder from '@ideditor/country-coder';
|
||||
|
||||
import { uiCombobox } from '../combobox';
|
||||
import { t } from '../../core/localizer';
|
||||
import { t, localizer } from '../../core/localizer';
|
||||
import { utilGetSetValue, utilNoAuto, utilRebind, utilTotalExtent } from '../../util';
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ export function uiFieldRoadheight(field, context) {
|
||||
var _entityIDs = [];
|
||||
var _tags;
|
||||
var _isImperial;
|
||||
var parseLocaleFloat = localizer.floatParser(localizer.languageCode());
|
||||
|
||||
var primaryUnits = [
|
||||
{
|
||||
@@ -129,16 +130,23 @@ export function uiFieldRoadheight(field, context) {
|
||||
|
||||
if (!primaryValue && !secondaryValue) {
|
||||
tag[field.key] = undefined;
|
||||
} else if (isNaN(primaryValue) || isNaN(secondaryValue) || !_isImperial) {
|
||||
tag[field.key] = context.cleanTagValue(primaryValue);
|
||||
} else {
|
||||
if (primaryValue !== '') {
|
||||
primaryValue = context.cleanTagValue(primaryValue + '\'');
|
||||
var rawPrimaryValue = parseLocaleFloat(primaryValue);
|
||||
if (isNaN(rawPrimaryValue)) rawPrimaryValue = primaryValue;
|
||||
var rawSecondaryValue = parseLocaleFloat(secondaryValue);
|
||||
if (isNaN(rawSecondaryValue)) rawSecondaryValue = secondaryValue;
|
||||
|
||||
if (isNaN(rawPrimaryValue) || isNaN(rawSecondaryValue) || !_isImperial) {
|
||||
tag[field.key] = context.cleanTagValue(rawPrimaryValue);
|
||||
} else {
|
||||
if (rawPrimaryValue !== '') {
|
||||
rawPrimaryValue = context.cleanTagValue(rawPrimaryValue + '\'');
|
||||
}
|
||||
if (rawSecondaryValue !== '') {
|
||||
rawSecondaryValue = context.cleanTagValue(rawSecondaryValue + '"');
|
||||
}
|
||||
tag[field.key] = rawPrimaryValue + rawSecondaryValue;
|
||||
}
|
||||
if (secondaryValue !== '') {
|
||||
secondaryValue = context.cleanTagValue(secondaryValue + '"');
|
||||
}
|
||||
tag[field.key] = primaryValue + secondaryValue;
|
||||
}
|
||||
|
||||
dispatch.call('change', this, tag);
|
||||
@@ -156,26 +164,33 @@ export function uiFieldRoadheight(field, context) {
|
||||
if (primaryValue && (primaryValue.indexOf('\'') >= 0 || primaryValue.indexOf('"') >= 0)) {
|
||||
secondaryValue = primaryValue.match(/(-?[\d.]+)"/);
|
||||
if (secondaryValue !== null) {
|
||||
secondaryValue = secondaryValue[1];
|
||||
secondaryValue = parseFloat(secondaryValue[1], 10).toLocaleString(localizer.languageCode());
|
||||
}
|
||||
primaryValue = primaryValue.match(/(-?[\d.]+)'/);
|
||||
if (primaryValue !== null) {
|
||||
primaryValue = primaryValue[1];
|
||||
primaryValue = parseFloat(primaryValue[1], 10).toLocaleString(localizer.languageCode());
|
||||
}
|
||||
_isImperial = true;
|
||||
} else if (primaryValue) {
|
||||
var rawValue = primaryValue;
|
||||
primaryValue = parseFloat(rawValue, 10);
|
||||
if (isNaN(primaryValue)) primaryValue = rawValue;
|
||||
primaryValue = primaryValue.toLocaleString(localizer.languageCode());
|
||||
_isImperial = false;
|
||||
}
|
||||
}
|
||||
|
||||
setUnitSuggestions();
|
||||
|
||||
// If feet are specified but inches are omitted, assume zero inches.
|
||||
var inchesPlaceholder = (0).toLocaleString(localizer.languageCode());
|
||||
|
||||
utilGetSetValue(primaryInput, typeof primaryValue === 'string' ? primaryValue : '')
|
||||
.attr('title', isMixed ? primaryValue.filter(Boolean).join('\n') : null)
|
||||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : t('inspector.unknown'))
|
||||
.classed('mixed', isMixed);
|
||||
utilGetSetValue(secondaryInput, typeof secondaryValue === 'string' ? secondaryValue : '')
|
||||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : (_isImperial ? '0' : null))
|
||||
.attr('placeholder', isMixed ? t('inspector.multiple_values') : (_isImperial ? inchesPlaceholder : null))
|
||||
.classed('mixed', isMixed)
|
||||
.classed('disabled', !_isImperial)
|
||||
.attr('readonly', _isImperial ? null : 'readonly');
|
||||
|
||||
@@ -3,7 +3,7 @@ import { select as d3_select } from 'd3-selection';
|
||||
import * as countryCoder from '@ideditor/country-coder';
|
||||
|
||||
import { uiCombobox } from '../combobox';
|
||||
import { t } from '../../core/localizer';
|
||||
import { t, localizer } from '../../core/localizer';
|
||||
import { utilGetSetValue, utilNoAuto, utilRebind, utilTotalExtent } from '../../util';
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ export function uiFieldRoadspeed(field, context) {
|
||||
var _entityIDs = [];
|
||||
var _tags;
|
||||
var _isImperial;
|
||||
var parseLocaleFloat = localizer.floatParser(localizer.languageCode());
|
||||
|
||||
var speedCombo = uiCombobox(context, 'roadspeed');
|
||||
var unitCombo = uiCombobox(context, 'roadspeed-unit')
|
||||
@@ -91,8 +92,8 @@ export function uiFieldRoadspeed(field, context) {
|
||||
|
||||
function comboValues(d) {
|
||||
return {
|
||||
value: d.toString(),
|
||||
title: d.toString()
|
||||
value: d.toLocaleString(localizer.languageCode()),
|
||||
title: d.toLocaleString(localizer.languageCode())
|
||||
};
|
||||
}
|
||||
|
||||
@@ -106,10 +107,14 @@ export function uiFieldRoadspeed(field, context) {
|
||||
|
||||
if (!value) {
|
||||
tag[field.key] = undefined;
|
||||
} else if (isNaN(value) || !_isImperial) {
|
||||
tag[field.key] = context.cleanTagValue(value);
|
||||
} else {
|
||||
tag[field.key] = context.cleanTagValue(value + ' mph');
|
||||
var rawValue = parseLocaleFloat(value);
|
||||
if (isNaN(rawValue)) rawValue = value;
|
||||
if (isNaN(rawValue) || !_isImperial) {
|
||||
tag[field.key] = context.cleanTagValue(rawValue);
|
||||
} else {
|
||||
tag[field.key] = context.cleanTagValue(rawValue + ' mph');
|
||||
}
|
||||
}
|
||||
|
||||
dispatch.call('change', this, tag);
|
||||
@@ -119,16 +124,20 @@ export function uiFieldRoadspeed(field, context) {
|
||||
roadspeed.tags = function(tags) {
|
||||
_tags = tags;
|
||||
|
||||
var value = tags[field.key];
|
||||
var rawValue = tags[field.key];
|
||||
var value = rawValue;
|
||||
var isMixed = Array.isArray(value);
|
||||
|
||||
if (!isMixed) {
|
||||
if (value && value.indexOf('mph') >= 0) {
|
||||
value = parseInt(value, 10).toString();
|
||||
_isImperial = true;
|
||||
} else if (value) {
|
||||
if (rawValue && rawValue.indexOf('mph') >= 0) {
|
||||
_isImperial = rawValue && rawValue.indexOf('mph') >= 0;
|
||||
} else if (rawValue) {
|
||||
_isImperial = false;
|
||||
}
|
||||
|
||||
value = parseInt(value, 10);
|
||||
if (isNaN(value)) value = rawValue;
|
||||
value = value.toLocaleString(localizer.languageCode());
|
||||
}
|
||||
|
||||
setUnitSuggestions();
|
||||
|
||||
Reference in New Issue
Block a user