Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mechanism for displaying fields conditionally based on tags #5583

Merged
merged 5 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions data/presets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ For number fields, the lowest valid value. There is no default.

For number fields, the greatest valid value. There is no default.

##### `prerequisiteTag`

An object defining the tags the feature needs before this field will be displayed. It must have this property:

- `key`: The key for the required tag.

And may optionally have one of these properties:

- `value`: The value that the key must have.
- `valueNot`: The value that the key must not have.

For example, this is how we show the Internet Access Fee field only if the feature has an `internet_access` tag not equal to `no`.

```js
"prerequisiteTag": {
"key": "internet_access",
"valueNot": "no"
}
```

## Icons

You can use any of the following open source map icon sets as preset icons.
Expand Down
6 changes: 3 additions & 3 deletions data/presets/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"dance/style": {"key": "dance:style", "type": "semiCombo", "label": "Dance Styles"},
"date": {"key": "date", "type": "check", "label": "Date"},
"delivery": {"key": "delivery", "type": "check", "label": "Delivery"},
"denomination": {"key": "denomination", "type": "combo", "label": "Denomination"},
"denomination": {"key": "denomination", "type": "combo", "label": "Denomination", "prerequisiteTag": {"key": "religion"}},
"denotation": {"key": "denotation", "type": "combo", "label": "Denotation"},
"description": {"key": "description", "type": "textarea", "label": "Description", "universal": true},
"design": {"key": "design", "type": "combo", "label": "Design"},
Expand Down Expand Up @@ -151,8 +151,8 @@
"intermittent_yes": {"key": "intermittent", "type": "check", "label": "Intermittent", "default": "yes"},
"intermittent": {"key": "intermittent", "type": "check", "label": "Intermittent"},
"internet_access": {"key": "internet_access", "type": "combo", "label": "Internet Access", "strings": {"options": {"yes": "Yes", "no": "No", "wlan": "Wifi", "wired": "Wired", "terminal": "Terminal"}}},
"internet_access/fee": {"key": "internet_access:fee", "type": "check", "label": "Internet Access Fee"},
"internet_access/ssid": {"key": "internet_access:ssid", "type": "text", "label": "SSID (Network Name)"},
"internet_access/fee": {"key": "internet_access:fee", "type": "check", "label": "Internet Access Fee", "prerequisiteTag": {"key": "internet_access", "valueNot": "no"}},
"internet_access/ssid": {"key": "internet_access:ssid", "type": "text", "label": "SSID (Network Name)", "prerequisiteTag": {"key": "internet_access", "valueNot": "no"}},
"kerb": {"key": "kerb", "type": "combo", "label": "Curb"},
"label": {"key": "label", "type": "textarea", "label": "Label"},
"lamp_type": {"key": "lamp_type", "type": "combo", "label": "Type"},
Expand Down
7 changes: 5 additions & 2 deletions data/presets/fields/denomination.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"key": "denomination",
"type": "combo",
"label": "Denomination"
}
"label": "Denomination",
"prerequisiteTag": {
"key": "religion"
}
}
6 changes: 5 additions & 1 deletion data/presets/fields/internet_access/fee.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"key": "internet_access:fee",
"type": "check",
"label": "Internet Access Fee"
"label": "Internet Access Fee",
"prerequisiteTag": {
"key": "internet_access",
"valueNot": "no"
}
}
6 changes: 5 additions & 1 deletion data/presets/fields/internet_access/ssid.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"key": "internet_access:ssid",
"type": "text",
"label": "SSID (Network Name)"
"label": "SSID (Network Name)",
"prerequisiteTag": {
"key": "internet_access",
"valueNot": "no"
}
}
22 changes: 22 additions & 0 deletions data/presets/schema/field.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@
"maxValue": {
"description": "Maximum field value (number fields only)",
"type": "integer"
},
"prerequisiteTag": {
"description": "Tagging constraint for showing this field in the editor",
"type": {
"type": "object",
"properties": {
"key": {
"description": "The key of the required tag",
"type": "string",
"required": true
},
"value": {
"description": "The value that the tag must have. (alternative to 'valueNot')",
"type": "string"
},
"valueNot": {
"description": "The value that the tag cannot have. (alternative to 'value')",
"type": "string"
}
},
"additionalProperties": false
}
}
},
"additionalProperties": false
Expand Down
62 changes: 6 additions & 56 deletions dist/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7550,6 +7550,9 @@
"SPW_PICC": {
"name": "SPW(allonie) PICC numerical imagery"
},
"US-TIGER-Roads-2012": {
"name": "TIGER Roads 2012"
},
"US-TIGER-Roads-2014": {
"description": "At zoom level 16+, public domain map data from the US Census. At lower zooms, only changes since 2006 minus changes already incorporated into OpenStreetMap",
"name": "TIGER Roads 2014"
Expand All @@ -7558,10 +7561,6 @@
"description": "Yellow = Public domain map data from the US Census. Red = Data not found in OpenStreetMap",
"name": "TIGER Roads 2017"
},
"US-TIGER-Roads-2018": {
"description": "Yellow = Public domain map data from the US Census. Red = Data not found in OpenStreetMap",
"name": "TIGER Roads 2018"
},
"US_Forest_Service_roads_overlay": {
"description": "Highway: Green casing = unclassified. Brown casing = track. Surface: gravel = light brown fill, Asphalt = black, paved = gray, ground =white, concrete = blue, grass = green. Seasonal = white bars",
"name": "U.S. Forest Roads Overlay"
Expand All @@ -7578,12 +7577,6 @@
},
"name": "UrbIS-Ortho 2017"
},
"UrbISOrtho2018": {
"attribution": {
"text": "Realized by means of Brussels UrbIS®© - Distribution & Copyright CIRB"
},
"name": "UrbIS-Ortho 2018"
},
"UrbisAdmFR": {
"attribution": {
"text": "Realized by means of Brussels UrbIS®© - Distribution & Copyright CIRB"
Expand Down Expand Up @@ -7668,33 +7661,11 @@
"description": "Japan GSI Standard Map. Widely covered.",
"name": "Japan GSI Standard Map"
},
"helsingborg-orto": {
"hike_n_bike": {
"attribution": {
"text": "© Helsingborg municipality"
},
"description": "Orthophotos from the municipality of Helsingborg 2016, public domain",
"name": "Helsingborg Orthophoto"
},
"kalmar-orto-2014": {
"attribution": {
"text": "© Kalmar municipality"
},
"description": "Orthophotos for the north coast of the municipality of Kalmar 2014",
"name": "Kalmar North Orthophoto 2014"
},
"kalmar-orto-2016": {
"attribution": {
"text": "© Kalmar municipality"
},
"description": "Orthophotos for the south coast of the municipality of Kalmar 2016",
"name": "Kalmar South Orthophoto 2016"
},
"kalmar-orto-2018": {
"attribution": {
"text": "© Kalmar municipality"
"text": "© OpenStreetMap contributors"
},
"description": "Orthophotos for urban areas of the municipality of Kalmar 2018",
"name": "Kalmar Urban Orthophoto 2018"
"name": "Hike & Bike"
},
"kelkkareitit": {
"attribution": {
Expand All @@ -7717,20 +7688,6 @@
"description": "Mosaic of Swedish orthophotos from the period 1970–1980. Is under construction.",
"name": "Lantmäteriet Historic Orthophoto 1975"
},
"lantmateriet-topowebb": {
"attribution": {
"text": "© Lantmäteriet, CC0"
},
"description": "Topographic map of Sweden 1:50 000",
"name": "Lantmäteriet Topographic Map"
},
"linkoping-orto": {
"attribution": {
"text": "© Linköping municipality"
},
"description": "Orthophotos from the municipality of Linköping 2010, open data",
"name": "Linköping Orthophoto"
},
"mapbox_locator_overlay": {
"attribution": {
"text": "Terms & Feedback"
Expand Down Expand Up @@ -7795,13 +7752,6 @@
},
"name": "Stamen Terrain"
},
"stockholm-orto": {
"attribution": {
"text": "© Stockholm municipality, CC0"
},
"description": "Orthophotos from the municipality of Stockholm 2015, CC0 license",
"name": "Stockholm Orthophoto"
},
"tf-cycle": {
"attribution": {
"text": "Maps © Thunderforest, Data © OpenStreetMap contributors"
Expand Down
38 changes: 34 additions & 4 deletions modules/ui/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ export function uiField(context, presetField, entity, options) {
dispatch.call('change', field, t, onInput);
});

// if this field cares about the entity, pass it along
if (entity && field.impl.entity) {
if (entity) {
field.entityID = entity.id;
field.impl.entity(entity);
// if this field cares about the entity, pass it along
if (field.impl.entity) {
field.impl.entity(entity);
}
}
}

Expand Down Expand Up @@ -229,11 +231,39 @@ export function uiField(context, presetField, entity, options) {
}
};


// A shown field has a visible UI, a non-shown field is in the 'Add field' dropdown
field.isShown = function() {
return _show || isPresent();
};

// 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 (isPresent()) {
// always allow a field with a value to display
return true;
}

var prerequisiteTag = field.prerequisiteTag;
if (prerequisiteTag && field.entityID) {
if (prerequisiteTag.key) {
var value = context.graph().entity(field.entityID).tags[prerequisiteTag.key];
if (value) {
if (prerequisiteTag.valueNot) {
return prerequisiteTag.valueNot !== value;
}
if (prerequisiteTag.value) {
return prerequisiteTag.value === value;
}
return true;
} else {
return false;
}
}
}
return true;
};

field.focus = function() {
if (field.impl) {
Expand Down
13 changes: 11 additions & 2 deletions modules/ui/form_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ export function uiFormFields(context) {
}


formFields.tagsChanged = function() {};

function render(selection, klass) {
var shown = _fieldsArr.filter(function(field) { return field.isShown(); });
var notShown = _fieldsArr.filter(function(field) { return !field.isShown(); });

formFields.tagsChanged = function() {
render(selection, klass);
};

var allowedFields = _fieldsArr.filter(function(field) { return field.isAllowed(); });

var shown = allowedFields.filter(function(field) { return field.isShown(); });
var notShown = allowedFields.filter(function(field) { return !field.isShown(); });

var container = selection.selectAll('.form-fields-container')
.data([0]);
Expand Down
1 change: 1 addition & 0 deletions modules/ui/preset_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export function uiPresetEditor(context) {
presetEditor.tags = function(val) {
if (!arguments.length) return _tags;
_tags = val;
formFields.tagsChanged();
// Don't reset _fieldsArr here.
return presetEditor;
};
Expand Down