diff --git a/modules/ui/fields/address.js b/modules/ui/fields/address.js index 979cbb9c2e..af5066bb32 100644 --- a/modules/ui/fields/address.js +++ b/modules/ui/fields/address.js @@ -10,7 +10,6 @@ import { utilArrayUniqBy, utilGetSetValue, utilNoAuto, utilRebind } from '../../ export function uiFieldAddress(field, context) { var dispatch = d3_dispatch('init', 'change'); - var countryCoder = services.countryCoder; var wrap = d3_select(null); var _isInitialized = false; var _entity; @@ -110,7 +109,6 @@ export function uiFieldAddress(field, context) { function updateForCountryCode(countryCode) { - if (!countryCode) return; countryCode = countryCode.toLowerCase(); var addressFormat; @@ -210,10 +208,10 @@ export function uiFieldAddress(field, context) { .attr('class', 'form-field-input-wrap form-field-input-' + field.type) .merge(wrap); - if (countryCoder && _entity) { + if (services.countryCoder && _entity) { var center = _entity.extent(context.graph()).center(); - var countryCode = countryCoder.iso1A2Code(center); - updateForCountryCode(countryCode); + var countryCode = services.countryCoder.iso1A2Code(center); + if (countryCode) updateForCountryCode(countryCode); } } diff --git a/modules/ui/fields/combo.js b/modules/ui/fields/combo.js index dbc76e12de..01251d3d2d 100644 --- a/modules/ui/fields/combo.js +++ b/modules/ui/fields/combo.js @@ -17,7 +17,7 @@ export { export function uiFieldCombo(field, context) { var dispatch = d3_dispatch('change'); - var nominatim = services.geocoder; + var countryCoder = services.countryCoder; var taginfo = services.taginfo; var isMulti = (field.type === 'multiCombo'); var isNetwork = (field.type === 'networkCombo'); @@ -35,7 +35,7 @@ export function uiFieldCombo(field, context) { var _comboData = []; var _multiData = []; var _entity; - var _country; + var _countryCode; // ensure multiCombo field.key ends with a ':' if (isMulti && /[^:]$/.test(field.key)) { @@ -163,9 +163,9 @@ export function uiFieldCombo(field, context) { function setTaginfoValues(q, callback) { var fn = isMulti ? 'multikeys' : 'values'; var query = (isMulti ? field.key : '') + q; - var hasCountryPrefix = isNetwork && _country && _country.indexOf(q.toLowerCase()) === 0; + var hasCountryPrefix = isNetwork && _countryCode && _countryCode.indexOf(q.toLowerCase()) === 0; if (hasCountryPrefix) { - query = _country + ':'; + query = _countryCode + ':'; } var params = { @@ -191,7 +191,7 @@ export function uiFieldCombo(field, context) { if (hasCountryPrefix) { data = data.filter(function(d) { - return d.value.toLowerCase().indexOf(_country + ':') === 0; + return d.value.toLowerCase().indexOf(_countryCode + ':') === 0; }); } @@ -351,11 +351,10 @@ export function uiFieldCombo(field, context) { .call(initCombo, selection) .merge(input); - if (isNetwork && nominatim && _entity) { + if (isNetwork && countryCoder && _entity) { var center = _entity.extent(context.graph()).center(); - nominatim.countryCode(center, function (err, code) { - _country = code; - }); + var countryCode = countryCoder.iso1A2Code(center); + _countryCode = countryCode && countryCode.toLowerCase(); } input diff --git a/modules/ui/fields/input.js b/modules/ui/fields/input.js index 1eb069bef6..8f9cad2520 100644 --- a/modules/ui/fields/input.js +++ b/modules/ui/fields/input.js @@ -17,7 +17,6 @@ export { export function uiFieldText(field, context) { var dispatch = d3_dispatch('change'); - var nominatim = services.geocoder; var input = d3_select(null); var _entity; @@ -56,13 +55,14 @@ export function uiFieldText(field, context) { .on('change', change()); - if (field.type === 'tel' && nominatim && _entity) { + if (field.type === 'tel' && services.countryCoder && _entity) { var center = _entity.extent(context.graph()).center(); - nominatim.countryCode(center, function (err, countryCode) { - if (err || !dataPhoneFormats[countryCode]) return; + var countryCode = services.countryCoder.iso1A2Code(center); + var format = countryCode && dataPhoneFormats[countryCode.toLowerCase()]; + if (format) { wrap.selectAll('#' + fieldID) - .attr('placeholder', dataPhoneFormats[countryCode]); - }); + .attr('placeholder', format); + } } else if (field.type === 'number') { var rtl = (textDirection === 'rtl'); diff --git a/modules/ui/fields/localized.js b/modules/ui/fields/localized.js index 19cfc29431..ad89d802e9 100644 --- a/modules/ui/fields/localized.js +++ b/modules/ui/fields/localized.js @@ -574,11 +574,8 @@ export function uiFieldLocalized(field, context) { function loadCountryCode() { var center = _entity.extent(context.graph()).center(); - services.geocoder.countryCode(center, function(err, result) { - if (!err && result) { - _countryCode = result; - } - }); + var countryCode = services.countryCoder.iso1A2Code(center); + _countryCode = countryCode && countryCode.toLowerCase(); } return utilRebind(localized, dispatch, 'on'); diff --git a/modules/ui/intro/intro.js b/modules/ui/intro/intro.js index 28e47d04c2..4584e8f457 100644 --- a/modules/ui/intro/intro.js +++ b/modules/ui/intro/intro.js @@ -70,7 +70,7 @@ export function uiIntro(context) { var opacity = d3_selectAll('#map .layer-background').style('opacity'); var caches = osm && osm.caches(); var baseEntities = context.history().graph().base().entities; - var countryCode = services.geocoder.countryCode; + var countryCode = services.countryCoder.iso1A2Code; // Show sidebar and disable the sidebar resizing button // (this needs to be before `context.inIntro(true)`) @@ -107,8 +107,8 @@ export function uiIntro(context) { }); // Mock geocoder - services.geocoder.countryCode = function(location, callback) { - callback(null, t('intro.graph.countrycode')); + services.countryCoder.iso1A2Code = function() { + return t('intro.graph.countrycode'); }; @@ -168,7 +168,7 @@ export function uiIntro(context) { if (history) { context.history().fromJSON(history, false); } context.map().centerZoom(center, zoom); window.location.replace(hash); - services.geocoder.countryCode = countryCode; + services.countryCoder.iso1A2Code = countryCode; context.inIntro(false); }); diff --git a/modules/ui/preset_list.js b/modules/ui/preset_list.js index 651469e63e..063ffe2529 100644 --- a/modules/ui/preset_list.js +++ b/modules/ui/preset_list.js @@ -22,7 +22,6 @@ export function uiPresetList(context) { var _entityID; var _currentPreset; var _autofocus = false; - var geocoder = services.geocoder; function presetList(selection) { @@ -100,34 +99,23 @@ export function uiPresetList(context) { function inputevent() { var value = search.property('value'); list.classed('filtered', value.length); - if (value.length) { - var entity = context.entity(_entityID); - if (geocoder && entity) { - var center = entity.extent(context.graph()).center(); - geocoder.countryCode(center, function countryCallback(err, countryCode) { - // get the input value again because it may have changed - var currentValue = search.property('value'); - - if (!currentValue.length) return; - - var results; - if (!err && countryCode) { - countryCode = countryCode.toLowerCase(); - results = presets.search(currentValue, geometry, countryCode); - } else { - results = presets.search(currentValue, geometry); - } - message.text(t('inspector.results', { - n: results.collection.length, - search: currentValue - })); - list.call(drawList, results); - }); - } + var entity = context.entity(_entityID); + var results, messageText; + if (value.length && entity && services.countryCoder) { + var center = entity.extent(context.graph()).center(); + var countryCode = services.countryCoder.iso1A2Code(center); + + results = presets.search(value, geometry, countryCode && countryCode.toLowerCase()); + messageText = t('inspector.results', { + n: results.collection.length, + search: value + }); } else { - list.call(drawList, context.presets().defaults(geometry, 36)); - message.text(t('inspector.choose')); + results = context.presets().defaults(geometry, 36); + messageText = t('inspector.choose'); } + list.call(drawList, results); + message.text(messageText); } var searchWrap = selection