Skip to content

Commit

Permalink
Replace remaining nominatim countryCode calls with country-coder calls (
Browse files Browse the repository at this point in the history
close #6941)
  • Loading branch information
quincylvania committed Nov 1, 2019
1 parent 51dbdb4 commit 8c07401
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 56 deletions.
8 changes: 3 additions & 5 deletions modules/ui/fields/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -110,7 +109,6 @@ export function uiFieldAddress(field, context) {


function updateForCountryCode(countryCode) {
if (!countryCode) return;
countryCode = countryCode.toLowerCase();

var addressFormat;
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 8 additions & 9 deletions modules/ui/fields/combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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;
});
}

Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions modules/ui/fields/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down
7 changes: 2 additions & 5 deletions modules/ui/fields/localized.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions modules/ui/intro/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)`)
Expand Down Expand Up @@ -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');
};


Expand Down Expand Up @@ -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);
});

Expand Down
42 changes: 15 additions & 27 deletions modules/ui/preset_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function uiPresetList(context) {
var _entityID;
var _currentPreset;
var _autofocus = false;
var geocoder = services.geocoder;


function presetList(selection) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c07401

Please sign in to comment.