Skip to content

Commit

Permalink
Add countryCodes property to preset schema
Browse files Browse the repository at this point in the history
Copy countryCodes from name suggestion index
Use countryCodes to filter the preset search results (close #6124)
  • Loading branch information
quincylvania committed Mar 28, 2019
1 parent d18b951 commit b12e727
Show file tree
Hide file tree
Showing 6 changed files with 1,541 additions and 1,505 deletions.
1 change: 1 addition & 0 deletions build_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ function suggestionsToPresets(presets) {
addTags: suggestion.tags,
removeTags: suggestion.tags,
reference: preset.reference,
countryCodes: suggestion.countryCodes,
matchScore: 2,
suggestion: true
};
Expand Down
6 changes: 6 additions & 0 deletions data/presets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ The complete JSON schema for presets can be found in [`data/presets/schema/prese

#### Preset Properties

##### `countryCodes`

An array of two-letter, lowercase [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country codes. The preset will only be searchable when the user is editing over the specified countries. The locale and language of iD are not factors, just the position of the map.

By default, presets are available everywhere.

##### `fields`/`moreFields`

Both these properties are arrays of field paths (e.g. `description` or `generator/type`).
Expand Down
2,990 changes: 1,495 additions & 1,495 deletions data/presets/presets.json

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions data/presets/schema/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@
"replacement": {
"description": "The ID of a preset that is preferable to this one",
"type": "string"
},
"countryCodes": {
"description": "Countries where to display the preset, as lowercase ISO 3166-1 alpha-2 codes (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "^[a-z]{2}$"
}
}
},
"additionalProperties": false
Expand Down
14 changes: 10 additions & 4 deletions modules/presets/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function presetCollection(collection) {
return this.item(id);
},

search: function(value, geometry) {
search: function(value, geometry, countryCode) {
if (!value) return this;

value = value.toLowerCase();
Expand Down Expand Up @@ -78,11 +78,17 @@ export function presetCollection(collection) {
return aCompare.length - bCompare.length;
}


var searchable = this.collection.filter(function(a) {
var pool = this.collection;
if (countryCode) {
pool = pool.filter(function(a) {
if (!a.countryCodes) return true;
return a.countryCodes.indexOf(countryCode) !== -1;
});
}
var searchable = pool.filter(function(a) {
return a.searchable !== false && a.suggestion !== true;
});
var suggestions = this.collection.filter(function(a) {
var suggestions = pool.filter(function(a) {
return a.suggestion === true;
});

Expand Down
26 changes: 20 additions & 6 deletions modules/ui/preset_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { t, textDirection } from '../util/locale';
import { actionChangePreset } from '../actions/index';
import { operationDelete } from '../operations/index';
import { services } from '../services';
import { svgIcon } from '../svg/index';
import { tooltip } from '../util/tooltip';
import { uiPresetIcon } from './preset_icon';
Expand All @@ -21,6 +22,7 @@ export function uiPresetList(context) {
var _entityID;
var _currentPreset;
var _autofocus = false;
var geocoder = services.geocoder;


function presetList(selection) {
Expand Down Expand Up @@ -99,12 +101,24 @@ export function uiPresetList(context) {
var value = search.property('value');
list.classed('filtered', value.length);
if (value.length) {
var results = presets.search(value, geometry);
message.text(t('inspector.results', {
n: results.collection.length,
search: value
}));
list.call(drawList, results);
var entity = context.entity(_entityID);
if (geocoder && entity) {
var center = entity.extent(context.graph()).center();
geocoder.countryCode(center, function countryCallback(err, countryCode) {
var results;
if (!err && countryCode) {
countryCode = countryCode.toLowerCase();
results = presets.search(value, geometry, countryCode);
} else {
results = presets.search(value, geometry);
}
message.text(t('inspector.results', {
n: results.collection.length,
search: value
}));
list.call(drawList, results);
});
}
} else {
list.call(drawList, context.presets().defaults(geometry, 36));
message.text(t('inspector.choose'));
Expand Down

0 comments on commit b12e727

Please sign in to comment.