From a32ce33238a02031f9f870dbf2096235f1f02e68 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 30 Aug 2013 13:55:37 -0700 Subject: [PATCH] Add Way#affix --- js/id/core/way.js | 5 +++++ js/id/modes/add_line.js | 6 +++--- js/id/modes/draw_line.js | 6 +++--- js/id/operations/continue.js | 6 +++--- test/spec/core/way.js | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/js/id/core/way.js b/js/id/core/way.js index c0b5fb058..be1dd66fb 100644 --- a/js/id/core/way.js +++ b/js/id/core/way.js @@ -32,6 +32,11 @@ _.extend(iD.Way.prototype, { return this.nodes.indexOf(node) >= 0; }, + affix: function(node) { + if (this.nodes[0] === node) return 'prefix'; + if (this.nodes[this.nodes.length - 1] === node) return 'suffix'; + }, + isOneWay: function() { return this.tags.oneway === 'yes' || this.tags.oneway === '1' || diff --git a/js/id/modes/add_line.js b/js/id/modes/add_line.js index 537844701..8132dcb58 100644 --- a/js/id/modes/add_line.js +++ b/js/id/modes/add_line.js @@ -23,7 +23,7 @@ iD.modes.AddLine = function(context) { iD.actions.AddEntity(way), iD.actions.AddVertex(way.id, node.id)); - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + context.enter(iD.modes.DrawLine(context, way.id, graph)); } function startFromWay(loc, edge) { @@ -37,7 +37,7 @@ iD.modes.AddLine = function(context) { iD.actions.AddVertex(way.id, node.id), iD.actions.AddMidpoint({ loc: loc, edge: edge }, node)); - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + context.enter(iD.modes.DrawLine(context, way.id, graph)); } function startFromNode(node) { @@ -47,7 +47,7 @@ iD.modes.AddLine = function(context) { iD.actions.AddEntity(way), iD.actions.AddVertex(way.id, node.id)); - context.enter(iD.modes.DrawLine(context, way.id, 'forward', context.graph())); + context.enter(iD.modes.DrawLine(context, way.id, context.graph())); } mode.enter = function() { diff --git a/js/id/modes/draw_line.js b/js/id/modes/draw_line.js index beb29bba2..d3e52280f 100644 --- a/js/id/modes/draw_line.js +++ b/js/id/modes/draw_line.js @@ -1,4 +1,4 @@ -iD.modes.DrawLine = function(context, wayId, direction, baseGraph) { +iD.modes.DrawLine = function(context, wayId, baseGraph, affix) { var mode = { button: 'line', id: 'draw-line' @@ -8,8 +8,8 @@ iD.modes.DrawLine = function(context, wayId, direction, baseGraph) { mode.enter = function() { var way = context.entity(wayId), - index = (direction === 'forward') ? undefined : 0, - headId = (direction === 'forward') ? way.last() : way.first(); + index = (affix === 'prefix') ? 0 : undefined, + headId = (affix === 'prefix') ? way.first() : way.last(); behavior = iD.behavior.DrawWay(context, wayId, index, mode, baseGraph) .tail(t('modes.draw_line.tail')); diff --git a/js/id/operations/continue.js b/js/id/operations/continue.js index 34a1d61ba..df17ed03c 100644 --- a/js/id/operations/continue.js +++ b/js/id/operations/continue.js @@ -8,7 +8,7 @@ iD.operations.Continue = function(selectedIDs, context) { function candidateWays() { return graph.parentWays(vertex).filter(function(parent) { return parent.geometry(graph) === 'line' && - (parent.first() === vertex.id || parent.last() === vertex.id) && + parent.affix(vertex.id) && (geometries.line.length === 0 || geometries.line[0] === parent); }); } @@ -18,8 +18,8 @@ iD.operations.Continue = function(selectedIDs, context) { context.enter(iD.modes.DrawLine( context, candidate.id, - candidate.first() === vertex.id ? 'backward' : 'forward', - context.graph())); + context.graph(), + candidate.affix(vertex.id))); }; operation.available = function() { diff --git a/test/spec/core/way.js b/test/spec/core/way.js index 935221815..a4b68aeab 100644 --- a/test/spec/core/way.js +++ b/test/spec/core/way.js @@ -48,6 +48,21 @@ describe('iD.Way', function() { }); }); + describe("#affix", function () { + it("returns 'prefix' if the way starts with the given node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).affix('a')).to.equal('prefix'); + }); + + it("returns 'suffix' if the way ends with the given node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).affix('c')).to.equal('suffix'); + }); + + it("returns falsy if the way does not start or end with the given node", function () { + expect(iD.Way({nodes: ['a', 'b', 'c']}).affix('b')).not.to.be.ok; + expect(iD.Way({nodes: []}).affix('b')).not.to.be.ok; + }); + }); + describe("#extent", function () { it("returns the minimal extent containing all member nodes", function () { var node1 = iD.Node({loc: [0, 0]}),