From d89996e52c4d50e0101a5d328e895bffe5a22bf1 Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Thu, 7 Nov 2019 18:45:46 +0100 Subject: [PATCH] Update country-coder to 2.0.0 (close #7029) Localize address field in walkthrough without overwriting country coder function --- modules/services/index.js | 3 --- modules/ui/fields/address.js | 14 ++++++++++---- modules/ui/fields/combo.js | 4 ++-- modules/ui/fields/input.js | 5 +++-- modules/ui/fields/localized.js | 3 ++- modules/ui/intro/intro.js | 7 ------- modules/ui/preset_list.js | 5 +++-- package.json | 2 +- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/modules/services/index.js b/modules/services/index.js index 6b26ce4dc..838ed435f 100644 --- a/modules/services/index.js +++ b/modules/services/index.js @@ -1,5 +1,3 @@ -import CountryCoder from 'country-coder'; - import serviceKeepRight from './keepRight'; import serviceImproveOSM from './improveOSM'; import serviceMapillary from './mapillary'; @@ -16,7 +14,6 @@ import serviceWikipedia from './wikipedia'; export var services = { - countryCoder: new CountryCoder(), geocoder: serviceNominatim, keepRight: serviceKeepRight, improveOSM: serviceImproveOSM, diff --git a/modules/ui/fields/address.js b/modules/ui/fields/address.js index af5066bb3..3f70d2fc7 100644 --- a/modules/ui/fields/address.js +++ b/modules/ui/fields/address.js @@ -1,9 +1,9 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select } from 'd3-selection'; +import * as countryCoder from 'country-coder'; import { dataAddressFormats } from '../../../data'; import { geoExtent, geoChooseEdge, geoSphericalDistance } from '../../geo'; -import { services } from '../../services'; import { uiCombobox } from '../combobox'; import { utilArrayUniqBy, utilGetSetValue, utilNoAuto, utilRebind } from '../../util'; @@ -208,9 +208,15 @@ export function uiFieldAddress(field, context) { .attr('class', 'form-field-input-wrap form-field-input-' + field.type) .merge(wrap); - if (services.countryCoder && _entity) { - var center = _entity.extent(context.graph()).center(); - var countryCode = services.countryCoder.iso1A2Code(center); + if (_entity) { + var countryCode; + if (context.inIntro()) { + // localize the address format for the walkthrough + countryCode = t('intro.graph.countrycode'); + } else { + var center = _entity.extent(context.graph()).center(); + countryCode = countryCoder.iso1A2Code(center); + } if (countryCode) updateForCountryCode(countryCode); } } diff --git a/modules/ui/fields/combo.js b/modules/ui/fields/combo.js index 01251d3d2..56d394b51 100644 --- a/modules/ui/fields/combo.js +++ b/modules/ui/fields/combo.js @@ -1,5 +1,6 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { event as d3_event, select as d3_select } from 'd3-selection'; +import * as countryCoder from 'country-coder'; import { osmEntity } from '../../osm/entity'; import { t } from '../../util/locale'; @@ -17,7 +18,6 @@ export { export function uiFieldCombo(field, context) { var dispatch = d3_dispatch('change'); - var countryCoder = services.countryCoder; var taginfo = services.taginfo; var isMulti = (field.type === 'multiCombo'); var isNetwork = (field.type === 'networkCombo'); @@ -351,7 +351,7 @@ export function uiFieldCombo(field, context) { .call(initCombo, selection) .merge(input); - if (isNetwork && countryCoder && _entity) { + if (isNetwork && _entity) { var center = _entity.extent(context.graph()).center(); var countryCode = countryCoder.iso1A2Code(center); _countryCode = countryCode && countryCode.toLowerCase(); diff --git a/modules/ui/fields/input.js b/modules/ui/fields/input.js index 8f9cad252..5242bf201 100644 --- a/modules/ui/fields/input.js +++ b/modules/ui/fields/input.js @@ -1,5 +1,6 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select, event as d3_event } from 'd3-selection'; +import * as countryCoder from 'country-coder'; import { t, textDirection } from '../../util/locale'; import { dataPhoneFormats } from '../../../data'; @@ -55,9 +56,9 @@ export function uiFieldText(field, context) { .on('change', change()); - if (field.type === 'tel' && services.countryCoder && _entity) { + if (field.type === 'tel' && _entity) { var center = _entity.extent(context.graph()).center(); - var countryCode = services.countryCoder.iso1A2Code(center); + var countryCode = countryCoder.iso1A2Code(center); var format = countryCode && dataPhoneFormats[countryCode.toLowerCase()]; if (format) { wrap.selectAll('#' + fieldID) diff --git a/modules/ui/fields/localized.js b/modules/ui/fields/localized.js index ad89d802e..8cf068ccc 100644 --- a/modules/ui/fields/localized.js +++ b/modules/ui/fields/localized.js @@ -1,5 +1,6 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select, event as d3_event } from 'd3-selection'; +import * as countryCoder from 'country-coder'; import { currentLocale, t, languageName } from '../../util/locale'; import { dataLanguages } from '../../../data'; @@ -574,7 +575,7 @@ export function uiFieldLocalized(field, context) { function loadCountryCode() { var center = _entity.extent(context.graph()).center(); - var countryCode = services.countryCoder.iso1A2Code(center); + var countryCode = countryCoder.iso1A2Code(center); _countryCode = countryCode && countryCode.toLowerCase(); } diff --git a/modules/ui/intro/intro.js b/modules/ui/intro/intro.js index 4584e8f45..faf9e955f 100644 --- a/modules/ui/intro/intro.js +++ b/modules/ui/intro/intro.js @@ -70,7 +70,6 @@ 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.countryCoder.iso1A2Code; // Show sidebar and disable the sidebar resizing button // (this needs to be before `context.inIntro(true)`) @@ -106,11 +105,6 @@ export function uiIntro(context) { } }); - // Mock geocoder - services.countryCoder.iso1A2Code = function() { - return t('intro.graph.countrycode'); - }; - d3_selectAll('#map .layer-background').style('opacity', 1); @@ -168,7 +162,6 @@ export function uiIntro(context) { if (history) { context.history().fromJSON(history, false); } context.map().centerZoom(center, zoom); window.location.replace(hash); - services.countryCoder.iso1A2Code = countryCode; context.inIntro(false); }); diff --git a/modules/ui/preset_list.js b/modules/ui/preset_list.js index 063ffe252..6c6a7eb4b 100644 --- a/modules/ui/preset_list.js +++ b/modules/ui/preset_list.js @@ -1,4 +1,5 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; +import * as countryCoder from 'country-coder'; import { event as d3_event, @@ -101,9 +102,9 @@ export function uiPresetList(context) { list.classed('filtered', value.length); var entity = context.entity(_entityID); var results, messageText; - if (value.length && entity && services.countryCoder) { + if (value.length && entity) { var center = entity.extent(context.graph()).center(); - var countryCode = services.countryCoder.iso1A2Code(center); + var countryCode = countryCoder.iso1A2Code(center); results = presets.search(value, geometry, countryCode && countryCode.toLowerCase()); messageText = t('inspector.results', { diff --git a/package.json b/package.json index f64095103..b473c2a8a 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "abortcontroller-polyfill": "^1.4.0", "alif-toolkit": "^1.2.6", "browser-polyfills": "~1.5.0", - "country-coder": "^1.0.2", + "country-coder": "^2.0.0", "diacritics": "1.3.0", "fast-deep-equal": "~2.0.1", "fast-json-stable-stringify": "2.0.0",