diff --git a/js/id/actions/add_midpoint.js b/js/id/actions/add_midpoint.js index c0cb97f59..95e3b5f3e 100644 --- a/js/id/actions/add_midpoint.js +++ b/js/id/actions/add_midpoint.js @@ -2,8 +2,19 @@ iD.actions.AddMidpoint = function(midpoint, node) { return function(graph) { graph = graph.replace(node.move(midpoint.loc)); - midpoint.ways.forEach(function(way) { - graph = graph.replace(graph.entity(way.id).addNode(node.id, way.index)); + var parents = _.intersection( + graph.parentWays(graph.entity(midpoint.edge[0])), + graph.parentWays(graph.entity(midpoint.edge[1]))); + + parents.forEach(function(way) { + for (var i = 0; i < way.nodes.length - 1; i++) { + if ((way.nodes[i] === midpoint.edge[0] && + way.nodes[i + 1] === midpoint.edge[1]) || + (way.nodes[i] === midpoint.edge[1] && + way.nodes[i + 1] === midpoint.edge[0])) { + graph = graph.replace(graph.entity(way.id).addNode(node.id, i + 1)); + } + } }); return graph; diff --git a/js/id/modes/select.js b/js/id/modes/select.js index ee218c743..d2995ea4f 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -139,31 +139,13 @@ iD.modes.Select = function(context, selection, initial) { if (datum instanceof iD.Way && !target.classed('fill')) { var choice = iD.geo.chooseIndex(datum, d3.mouse(context.surface().node()), context), - node = iD.Node({ loc: choice.loc }); + node = iD.Node(); var prev = datum.nodes[choice.index - 1], - next = datum.nodes[choice.index], - prevParents = context.graph().parentWays({ id: prev }), - ways = []; + next = datum.nodes[choice.index]; - - for (var i = 0; i < prevParents.length; i++) { - var p = prevParents[i]; - for (var k = 0; k < p.nodes.length; k++) { - if (p.nodes[k] === prev) { - if (p.nodes[k-1] === next) { - ways.push({ id: p.id, index: k}); - break; - } else if (p.nodes[k+1] === next) { - ways.push({ id: p.id, index: k+1}); - break; - } - } - } - } - - context.perform(iD.actions.AddEntity(node), - iD.actions.AddMidpoint({ ways: ways, loc: node.loc }, node), + context.perform( + iD.actions.AddMidpoint({loc: choice.loc, edge: [prev, next]}, node), t('operations.add.annotation.vertex')); d3.event.preventDefault(); diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index f672c55ed..0dfae7d13 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -72,9 +72,11 @@ iD.Map = function(context) { all = _.compact(_.values(complete)); filter = function(d) { if (d.type === 'midpoint') { - for (var i = 0; i < d.ways.length; i++) { - if (d.ways[i].id in complete) return true; - } + var a = graph.entity(d.edge[0]), + b = graph.entity(d.edge[1]); + return !a || !b || + _.intersection(graph.parentWays(a), all).length || + _.intersection(graph.parentWays(b), all).length; } else { return d.id in complete; } diff --git a/js/id/svg/midpoints.js b/js/id/svg/midpoints.js index 8dc6d09b9..a06cf3779 100644 --- a/js/id/svg/midpoints.js +++ b/js/id/svg/midpoints.js @@ -19,17 +19,14 @@ iD.svg.Midpoints = function(projection) { b = nodes[j + 1], id = [a.id, b.id].sort().join('-'); - if (midpoints[id]) { - midpoints[id].ways.push({id: entity.id, index: j + 1}); - - } else { + if (!midpoints[id]) { var loc = iD.geo.interp(a.loc, b.loc, 0.5); if (extent.intersects(loc) && iD.geo.dist(projection(a.loc), projection(b.loc)) > 40) { midpoints[id] = { type: 'midpoint', id: id, loc: loc, - ways: [{id: entity.id, index: j + 1}] + edge: [a.id, b.id] }; } } diff --git a/test/spec/actions/add_midpoint.js b/test/spec/actions/add_midpoint.js index 1f749217e..75fadbac0 100644 --- a/test/spec/actions/add_midpoint.js +++ b/test/spec/actions/add_midpoint.js @@ -1,22 +1,37 @@ describe("iD.actions.AddMidpoint", function () { it("adds the node at the midpoint location", function () { var node = iD.Node(), - midpoint = {loc: [1, 2], ways: []}, - graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph()); + a = iD.Node(), + b = iD.Node(), + midpoint = {loc: [1, 2], edge: [a.id, b.id]}, + graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b])); expect(graph.entity(node.id).loc).to.eql([1, 2]); }); - it("adds the node to all ways at the respective indexes", function () { + it("adds the node to a way that contains the given edge in forward order", function () { var node = iD.Node(), a = iD.Node(), b = iD.Node(), w1 = iD.Way(), w2 = iD.Way({nodes: [a.id, b.id]}), - midpoint = {loc: [1, 2], ways: [{id: w1.id, index: 0}, {id: w2.id, index: 1}]}, + midpoint = {loc: [1, 2], edge: [a.id, b.id]}, graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b, w1, w2])); - expect(graph.entity(w1.id).nodes).to.eql([node.id]); + expect(graph.entity(w1.id).nodes).to.eql([]); expect(graph.entity(w2.id).nodes).to.eql([a.id, node.id, b.id]); }); + + it("adds the node to a way that contains the given edge in reverse order", function () { + var node = iD.Node(), + a = iD.Node(), + b = iD.Node(), + w1 = iD.Way(), + w2 = iD.Way({nodes: [b.id, a.id]}), + midpoint = {loc: [1, 2], edge: [a.id, b.id]}, + graph = iD.actions.AddMidpoint(midpoint, node)(iD.Graph([a, b, w1, w2])); + + expect(graph.entity(w1.id).nodes).to.eql([]); + expect(graph.entity(w2.id).nodes).to.eql([b.id, node.id, a.id]); + }); });