Skip to content

Commit

Permalink
Use holes in depths to ensure that removals don't invalidate indicies…
Browse files Browse the repository at this point in the history
…. `assignDepthsToEles()` cleans out holes.

Fix depth adjustment in breadthfirst layout #1992
  • Loading branch information
maxkfranz committed Oct 23, 2017
1 parent 7aa88ac commit 66ca5a9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/extensions/layout/breadthfirst.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let defaults = {
animateFilter: function ( node, i ){ return true; }, // a function that determines whether the node should be animated. All nodes animated by default on animate enabled. Non-animated nodes are positioned immediately when the layout starts
ready: undefined, // callback on layoutready
stop: undefined, // callback on layoutstop
transform: function (node, position ){ return position; } // transform a given node position. Useful for changing flow direction in discrete layouts
transform: function (node, position ){ return position; } // transform a given node position. Useful for changing flow direction in discrete layouts
};

function BreadthFirstLayout( options ){
Expand Down Expand Up @@ -193,9 +193,14 @@ BreadthFirstLayout.prototype.run = function(){
for( let i = 0; i < depths.length; i++ ){
let eles = depths[ i ];

for( let j = 0; j < eles.length; j++ ){
for( let j = eles.length - 1; j >= 0; j-- ){
let ele = eles[ j ];

if( ele == null ){
eles.splice( j, 1 );
continue;
}

ele._private.scratch.breadthfirst = {
depth: i,
index: j
Expand Down Expand Up @@ -254,7 +259,7 @@ BreadthFirstLayout.prototype.run = function(){
let intEle = info.intEle;
let intInfo = intEle._private.scratch.breadthfirst;

depths[ info.depth ].splice( info.index, 1 ); // remove from old depth & index
depths[ info.depth ][ info.index ] = null; // remove from old depth & index (create hole to be cleaned)

// add to end of new depth
let newDepth = intInfo.depth + 1;
Expand Down

0 comments on commit 66ca5a9

Please sign in to comment.