From d59320ee4b59fd48c3632dc6ad22110d38a2a9b7 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 8 Feb 2013 12:03:48 -0800 Subject: [PATCH] Extract Way#replaceNode --- js/id/actions/connect.js | 8 +------- js/id/graph/way.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/js/id/actions/connect.js b/js/id/actions/connect.js index 68b2bf079..73c1af09a 100644 --- a/js/id/actions/connect.js +++ b/js/id/actions/connect.js @@ -20,13 +20,7 @@ iD.actions.Connect = function(nodeIds) { var node = graph.entity(nodeIds[i]), index; graph.parentWays(node).forEach(function (parent) { - while (true) { - index = parent.nodes.indexOf(node.id); - if (index < 0) - break; - parent = parent.updateNode(survivor.id, index); - } - graph = graph.replace(parent); + graph = graph.replace(parent.replaceNode(node.id, survivor.id)); }); graph.parentRelations(node).forEach(function (parent) { diff --git a/js/id/graph/way.js b/js/id/graph/way.js index 1830e61a4..5229040ab 100644 --- a/js/id/graph/way.js +++ b/js/id/graph/way.js @@ -75,6 +75,19 @@ _.extend(iD.Way.prototype, { return this.update({nodes: nodes}); }, + replaceNode: function(needle, replacement) { + if (this.nodes.indexOf(needle) < 0) + return this; + + var nodes = this.nodes.slice(); + for (var i = 0; i < nodes.length; i++) { + if (nodes[i] === needle) { + nodes[i] = replacement; + } + } + return this.update({nodes: nodes}); + }, + removeNode: function(id) { var nodes = _.without(this.nodes, id);