Fix issue where removing node B from an ABA way would leave two As. Needs reasonably...
[potlatch2.git] / net / systemeD / halcyon / connection / actions / RemoveNodeFromWayAction.as
index 760866a..cd3007b 100644 (file)
@@ -15,11 +15,19 @@ package net.systemeD.halcyon.connection.actions {
             
         public override function doAction():uint {
             nodeRemovedFrom = [];
-                       var i:int;
+                       var i:int, node2:Node;
                        while ((i=nodeList.indexOf(node))>-1) {
+                               // remove the node from the way
                                nodeList.splice(i,1);
-                               nodeRemovedFrom.push(i);
-               entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, Way(entity), i));
+                               nodeRemovedFrom.push([node,i]);
+                               entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, Way(entity), i));
+
+                               // remove any repeated nodes that have occurred as a result (i.e. removing B from ABA)
+                               while (i>0 && nodeList[i-1]==nodeList[i]) {
+                                       node2=nodeList.splice(i,1)[0];
+                                       nodeRemovedFrom.push([node2,i]);
+                                       entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node2, Way(entity), i));
+                               }
                        }
                        
                        if ( nodeRemovedFrom.length > 0 ) {
@@ -36,9 +44,11 @@ package net.systemeD.halcyon.connection.actions {
             node.addParent(entity);
             
             for (var i:int = nodeRemovedFrom.length - 1; i >= 0; i--) {
-                var index:int = nodeRemovedFrom[i];
-                nodeList.splice(index, 0, node);
-               entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, Way(entity), index));
+                var removal:Array = nodeRemovedFrom[i];
+                var reinstate:Node = removal[0];
+                var index:int = removal[1];
+                nodeList.splice(index, 0, reinstate);
+                entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, reinstate, Way(entity), index));
             }
             
             entity.deleted = nodeList.length == 0;