diff --git a/data/presets/README.md b/data/presets/README.md index a7751b898..1b477ff11 100644 --- a/data/presets/README.md +++ b/data/presets/README.md @@ -410,6 +410,18 @@ For example, this is how we show the Internet Access Fee field only if the featu } ``` +If a feature has a value for this field's `key` or `keys`, it will display regardless of the `prerequisiteTag` property. + +##### `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 field will only be available for features in the specified, whitelisted countries. + +By default, fields are available everywhere. + +##### `notCountryCodes` + +An array of two-letter, lowercase [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country codes. Similar to `countryCodes` except a blacklist instead of a whitelist. + ## Building diff --git a/data/presets/schema/field.json b/data/presets/schema/field.json index 82dc4e34a..13baeea10 100644 --- a/data/presets/schema/field.json +++ b/data/presets/schema/field.json @@ -183,6 +183,24 @@ "items": { "type": "string" } + }, + "countryCodes": { + "description": "Countries where to allow the field, 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}$" + } + }, + "notCountryCodes": { + "description": "Countries where NOT to allow the field, 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 diff --git a/modules/ui/field.js b/modules/ui/field.js index ecd840df3..11da3613e 100644 --- a/modules/ui/field.js +++ b/modules/ui/field.js @@ -1,3 +1,4 @@ +import * as countryCoder from '@ideditor/country-coder'; import { dispatch as d3_dispatch } from 'd3-dispatch'; import { event as d3_event, select as d3_select } from 'd3-selection'; @@ -295,13 +296,30 @@ export function uiField(context, presetField, entity, options) { // An allowed field can appear in the UI or in the 'Add field' dropdown. // A non-allowed field is hidden from the user altogether field.isAllowed = function() { - if (!entity || tagsContainFieldKey()) return true; // a field with a value should always display - var latest = context.hasEntity(entity.id); // check the most current copy of the entity + var latest = entity && context.hasEntity(entity.id); // check the most current copy of the entity if (!latest) return true; + if (field.countryCodes || field.notCountryCodes) { + var center = latest.extent(context.graph()).center(); + var countryCode = countryCoder.iso1A2Code(center); + + if (!countryCode) return false; + + countryCode = countryCode.toLowerCase(); + + if (field.countryCodes && field.countryCodes.indexOf(countryCode) === -1) { + return false; + } + if (field.notCountryCodes && field.notCountryCodes.indexOf(countryCode) !== -1) { + return false; + } + } + var prerequisiteTag = field.prerequisiteTag; - if (prerequisiteTag) { + + if (!tagsContainFieldKey() && // ignore tagging prerequisites if a value is already present + prerequisiteTag) { if (prerequisiteTag.key) { var value = latest.tags[prerequisiteTag.key]; if (!value) return false; @@ -316,6 +334,7 @@ export function uiField(context, presetField, entity, options) { if (latest.tags[prerequisiteTag.keyNot]) return false; } } + return true; };