From 4083a0e29eb4ada2e65958d7e569fb6269b7b256 Mon Sep 17 00:00:00 2001 From: Kushan Joshi <0o3ko0@gmail.com> Date: Tue, 7 Jun 2016 20:24:57 +0530 Subject: [PATCH] adding a lanes() to return lane data --- data/presets.yaml | 2 -- data/presets/fields.json | 5 ++--- data/presets/fields/lanes.json | 5 ++--- data/presets/schema/field.json | 3 ++- dist/locales/en.json | 3 +-- index.html | 1 + js/id/ui/preset/lanes.js | 39 +++++++++++++++++++++++++++++++++ modules/core/way.js | 40 ++++++++++++++++++++++++++++++++++ modules/ui/core/preset.js | 4 ++++ 9 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 js/id/ui/preset/lanes.js diff --git a/data/presets.yaml b/data/presets.yaml index 27e6a124d..094732cd6 100644 --- a/data/presets.yaml +++ b/data/presets.yaml @@ -472,8 +472,6 @@ en: lanes: # 'lanes=*' label: Lanes - # lanes field placeholder - placeholder: '1, 2, 3...' layer: # 'layer=*' label: Layer diff --git a/data/presets/fields.json b/data/presets/fields.json index 203c632a5..31377a3b2 100644 --- a/data/presets/fields.json +++ b/data/presets/fields.json @@ -647,9 +647,8 @@ }, "lanes": { "key": "lanes", - "type": "number", - "label": "Lanes", - "placeholder": "1, 2, 3..." + "type": "lanes", + "label": "Lanes" }, "layer": { "key": "layer", diff --git a/data/presets/fields/lanes.json b/data/presets/fields/lanes.json index 8b4a10628..2f074ec28 100644 --- a/data/presets/fields/lanes.json +++ b/data/presets/fields/lanes.json @@ -1,6 +1,5 @@ { "key": "lanes", - "type": "number", - "label": "Lanes", - "placeholder":"1, 2, 3..." + "type": "lanes", + "label": "Lanes" } diff --git a/data/presets/schema/field.json b/data/presets/schema/field.json index 7b73733ee..660fec199 100644 --- a/data/presets/schema/field.json +++ b/data/presets/schema/field.json @@ -66,7 +66,8 @@ "localized", "wikipedia", "typeCombo", - "restrictions" + "restrictions", + "lanes" ], "required": true }, diff --git a/dist/locales/en.json b/dist/locales/en.json index e6ecfcfb0..457f4af9a 100644 --- a/dist/locales/en.json +++ b/dist/locales/en.json @@ -1046,8 +1046,7 @@ "label": "Type" }, "lanes": { - "label": "Lanes", - "placeholder": "1, 2, 3..." + "label": "Lanes" }, "layer": { "label": "Layer" diff --git a/index.html b/index.html index 88b53dfe9..71549149d 100644 --- a/index.html +++ b/index.html @@ -48,6 +48,7 @@ + diff --git a/js/id/ui/preset/lanes.js b/js/id/ui/preset/lanes.js new file mode 100644 index 000000000..8068a573f --- /dev/null +++ b/js/id/ui/preset/lanes.js @@ -0,0 +1,39 @@ +iD.ui.preset.lanes = function(field, context) { + var dispatch = d3.dispatch('change'), + wayID, + laneData; + + + function lanes(selection) { + // if form field is hidden or has detached from dom, clean up. + if (!d3.select('.inspector-wrap.inspector-hidden').empty() || !selection.node().parentNode) { + selection.call(lanes.off); + return; + } + + var wrap = selection.selectAll('.preset-input-wrap') + .data([0]); + + var enter = wrap.enter() + .append('div') + .attr('class', 'preset-input-wrap'); + + enter + .append('div') + .attr('class', 'lane-help'); + + laneData = context.entity(wayID).lanes(); + } + + lanes.entity = function(_) { + if (!wayID || wayID !== _.id) { + wayID = _.id; + } + }; + + lanes.tags = function() {}; + lanes.focus = function() {}; + lanes.off = function() {}; + + return d3.rebind(lanes, dispatch, 'on'); +}; diff --git a/modules/core/way.js b/modules/core/way.js index dfcb455c9..15cd7fde1 100644 --- a/modules/core/way.js +++ b/modules/core/way.js @@ -100,6 +100,46 @@ _.extend(Way.prototype, { return false; }, + lanes: function() { + if (!this.tags.highway) return null; + + var defaultLanes = {}, tagged = {}; + + switch (this.tags.highway) { + case 'service': + case 'track': + case 'path': + defaultLanes.count = this.isOneWay() ? 1 : 2; + break; + case 'trunk': + case 'motorway': + defaultLanes.count = this.isOneWay() ? 2 : 4; + break; + default: + defaultLanes.count = this.isOneWay() ? 1 : 2; + break; + } + + if (!this.isOneWay()) { + defaultLanes.forward = defaultLanes.count/2; + defaultLanes.backward = defaultLanes.count/2; + } else { + tagged.oneway = 'yes'; + } + + tagged.lanes = {}; + if (this.tags.lanes) tagged.lanes.count = this.tags.lanes; + if (this.tags['lanes:forward']) tagged.lanes.forward = this.tags['lanes:forward']; + if (this.tags['lanes:backward']) tagged.lanes.backward = this.tags['lanes:backward']; + + return { + defaults: { + lanes: defaultLanes + }, + tagged: tagged + }; + }, + isClosed: function() { return this.nodes.length > 0 && this.first() === this.last(); }, diff --git a/modules/ui/core/preset.js b/modules/ui/core/preset.js index cbda008c3..92f738ec8 100644 --- a/modules/ui/core/preset.js +++ b/modules/ui/core/preset.js @@ -88,6 +88,10 @@ export function preset(context) { fields.push(UIField(context.presets().field('restrictions'), entity, true)); } + if (entity.lanes(context.graph())) { + fields.push(UIField(context.presets().field('lanes'), entity, true)); + } + context.presets().universal().forEach(function(field) { if (preset.fields.indexOf(field) < 0) { fields.push(UIField(field, entity));