From e19f86a285148b4320de5fcc55e525efe70888fc Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 18 Jan 2013 14:43:25 -0800 Subject: [PATCH] Add newly split way to member relations --- js/id/actions/split_way.js | 20 ++++++++- test/spec/actions/split_way.js | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/js/id/actions/split_way.js b/js/id/actions/split_way.js index 9f33bdf73..de399df80 100644 --- a/js/id/actions/split_way.js +++ b/js/id/actions/split_way.js @@ -33,8 +33,26 @@ iD.actions.SplitWay = function(nodeId, newWayId) { graph = iD.actions.UpdateRelationMember( relation.id, {id: newWay.id}, - relation.memberById(way.id).index)(graph); + relation.memberById(way.id).index + )(graph); } + } else { + var role = relation.memberById(way.id).role, + last = newWay.last(), + i = relation.memberById(way.id).index, + j; + + for (j = 0; j < relation.members.length; j++) { + if (relation.members[j].type === 'way' && graph.entity(relation.members[j].id).contains(last)) { + break; + } + } + + graph = iD.actions.AddRelationMember( + relation.id, + {id: newWay.id, type: 'way', role: role}, + i <= j ? i + 1 : i + )(graph); } }); diff --git a/test/spec/actions/split_way.js b/test/spec/actions/split_way.js index b12708f9c..e83d5ee1a 100644 --- a/test/spec/actions/split_way.js +++ b/test/spec/actions/split_way.js @@ -37,6 +37,82 @@ describe("iD.actions.SplitWay", function () { expect(graph.entity('=').tags).to.equal(tags); }); + it("adds the new way to parent relations (no connections)", function () { + // Situation: + // a ---- b ---- c + // Relation: [----] + // + // Split at b. + // + // Expected result: + // a ---- b ==== c + // Relation: [----, ====] + // + var graph = iD.Graph({ + 'a': iD.Node({id: 'a'}), + 'b': iD.Node({id: 'b'}), + 'c': iD.Node({id: 'c'}), + '-': iD.Way({id: '-', nodes: ['a', 'b', 'c']}), + 'r': iD.Relation({id: 'r', members: [{id: '-', type: 'way'}]}) + }); + + graph = iD.actions.SplitWay('b', '=')(graph); + + expect(_.pluck(graph.entity('r').members, 'id')).to.eql(['-', '=']); + }); + + it("adds the new way to parent relations (forward order)", function () { + // Situation: + // a ---- b ---- c ~~~~ d + // Relation: [----, ~~~~] + // + // Split at b. + // + // Expected result: + // a ---- b ==== c ~~~~ d + // Relation: [----, ====, ~~~~] + // + var graph = iD.Graph({ + 'a': iD.Node({id: 'a'}), + 'b': iD.Node({id: 'b'}), + 'c': iD.Node({id: 'c'}), + 'd': iD.Node({id: 'd'}), + '-': iD.Way({id: '-', nodes: ['a', 'b', 'c']}), + '~': iD.Way({id: '~', nodes: ['c', 'd']}), + 'r': iD.Relation({id: 'r', members: [{id: '-', type: 'way'}, {id: '~', type: 'way'}]}) + }); + + graph = iD.actions.SplitWay('b', '=')(graph); + + expect(_.pluck(graph.entity('r').members, 'id')).to.eql(['-', '=', '~']); + }); + + it("adds the new way to parent relations (reverse order)", function () { + // Situation: + // a ---- b ---- c ~~~~ d + // Relation: [~~~~, ----] + // + // Split at b. + // + // Expected result: + // a ---- b ==== c ~~~~ d + // Relation: [~~~~, ====, ----] + // + var graph = iD.Graph({ + 'a': iD.Node({id: 'a'}), + 'b': iD.Node({id: 'b'}), + 'c': iD.Node({id: 'c'}), + 'd': iD.Node({id: 'd'}), + '-': iD.Way({id: '-', nodes: ['a', 'b', 'c']}), + '~': iD.Way({id: '~', nodes: ['c', 'd']}), + 'r': iD.Relation({id: 'r', members: [{id: '~', type: 'way'}, {id: '-', type: 'way'}]}) + }); + + graph = iD.actions.SplitWay('b', '=')(graph); + + expect(_.pluck(graph.entity('r').members, 'id')).to.eql(['~', '=', '-']); + }); + ['restriction', 'restriction:bus'].forEach(function (type) { it("updates a restriction's 'from' role", function () { // Situation: