Add countryCodes and notCountryCodes properties for fields (close #7085)

Add documentation note about `prerequisiteTag` property getting ignored if a value is present
This commit is contained in:
Quincy Morgan
2019-12-03 20:11:39 -05:00
parent 6e28e704cf
commit 1647b9addf
3 changed files with 52 additions and 3 deletions
+12
View File
@@ -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
+18
View File
@@ -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
+22 -3
View File
@@ -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;
};