diff --git a/modules/actions/add_member.js b/modules/actions/add_member.js index 3d7654d36..d5f5b26b7 100644 --- a/modules/actions/add_member.js +++ b/modules/actions/add_member.js @@ -1,18 +1,21 @@ import { osmJoinWays } from '../osm'; -export function actionAddMember(relationId, member, memberIndex) { - return function(graph) { +export function actionAddMember(relationId, member, memberIndex, insertHint) { + + var action = function(graph) { var relation = graph.entity(relationId); var numAdded = 0; // If we weren't passed a memberIndex, // try to perform sensible inserts based on how the ways join together - if (isNaN(memberIndex) && member.type === 'way') { + if ((isNaN(memberIndex) || insertHint) && member.type === 'way') { var members = relation.indexedMembers(); - members.push(member); + if (!insertHint) { + members.push(member); // just push and let osmJoinWays sort it out + } - var joined = osmJoinWays(members, graph); + var joined = osmJoinWays(members, graph, insertHint); for (var i = 0; i < joined.length; i++) { var segment = joined[i]; @@ -40,4 +43,7 @@ export function actionAddMember(relationId, member, memberIndex) { return graph.replace(relation); }; + + + return action; } diff --git a/modules/actions/split.js b/modules/actions/split.js index e0bd8b20f..a9d0bd77f 100644 --- a/modules/actions/split.js +++ b/modules/actions/split.js @@ -134,14 +134,12 @@ export function actionSplit(nodeId, newWayIds) { role: relation.memberById(wayA.id).role }; - // how many times should this member be inserted? - // var matches = relation.members.filter(function(member) { - // return member.id === wayA.id; - // }); + var insertHint = { + item: member, + nextTo: wayA.id + }; - // matches.forEach(function() { - graph = actionAddMember(relation.id, member)(graph); - // }); + graph = actionAddMember(relation.id, member, undefined, insertHint)(graph); } }); diff --git a/modules/osm/multipolygon.js b/modules/osm/multipolygon.js index c8412fecf..0990e3e30 100644 --- a/modules/osm/multipolygon.js +++ b/modules/osm/multipolygon.js @@ -88,12 +88,12 @@ export function osmSimpleMultipolygonOuterMember(entity, graph) { // Incomplete members (those for which `graph.hasEntity(element.id)` returns // false) and non-way members are ignored. // -// `tryInsert` is an optional object. -// If supplied, insert the given way/member after an existing way/member: -// `{ item: wayOrMember, afterID: id }` +// `insertHint` is an optional object. +// If supplied, insert the given way/member next to an existing way/member: +// `{ item: wayOrMember, nextTo: id }` // (This feature is used by `actionSplit`) // -export function osmJoinWays(toJoin, graph, tryInsert) { +export function osmJoinWays(toJoin, graph, insertHint) { function resolve(member) { return graph.childNodes(graph.entity(member.id)); } @@ -132,8 +132,8 @@ export function osmJoinWays(toJoin, graph, tryInsert) { // If it is time to attempt an insert, try that item first. // Otherwise, search for a next item in `toJoin` var toCheck; - if (!isInserting && tryInsert && tryInsert.afterID === currWays[currWays.length - 1].id) { - toCheck = [tryInsert.item]; + if (!isInserting && insertHint && insertHint.nextTo === currWays[currWays.length - 1].id) { + toCheck = [insertHint.item]; isInserting = true; } else { toCheck = toJoin.slice(); diff --git a/test/spec/actions/add_member.js b/test/spec/actions/add_member.js index 95e9d98a6..863045849 100644 --- a/test/spec/actions/add_member.js +++ b/test/spec/actions/add_member.js @@ -101,7 +101,7 @@ describe('iD.actionAddMember', function() { expect(members(graph)).to.eql(['-', '=', '~']); }); - it('inserts the member multiple times if the way exists multiple times (middle)', function() { + it('inserts the member multiple times if hint provided (middle)', function() { // Before: a ---> b .. c ~~~> d <~~~ c .. b <--- a // After: a ---> b ===> c ~~~> d <~~~ c <=== b <--- a var graph = iD.coreGraph([ @@ -120,11 +120,13 @@ describe('iD.actionAddMember', function() { ]}) ]); - graph = iD.actionAddMember('r', {id: '=', type: 'way'})(graph); + var member = { id: '=', type: 'way' }; + var hint = { item: member, nextTo: '-' }; + graph = iD.actionAddMember('r', member, undefined, hint)(graph); expect(members(graph)).to.eql(['-', '=', '~', '~', '=', '-']); }); - it('inserts the member multiple times if the way exists multiple times (beginning/end)', function() { + it('inserts the member multiple times if hint provided (beginning/end)', function() { // Before: b ===> c ~~~> d <~~~ c <=== b // After: a ---> b ===> c ~~~> d <~~~ c <=== b <--- a var graph = iD.coreGraph([ @@ -143,7 +145,9 @@ describe('iD.actionAddMember', function() { ]}) ]); - graph = iD.actionAddMember('r', {id: '-', type: 'way'})(graph); + var member = { id: '-', type: 'way' }; + var hint = { item: member, nextTo: '=' }; + graph = iD.actionAddMember('r', member, undefined, hint)(graph); expect(members(graph)).to.eql(['-', '=', '~', '~', '=', '-']); });