From c6fa09ec7d97554b0a2f892a143614104fc5ca5b Mon Sep 17 00:00:00 2001 From: Gonzalo Bellver Date: Wed, 18 Jan 2012 13:34:30 -0300 Subject: [PATCH] Fixed bug where adding a new node borke free positioning --- .../main/javascript/layout/OriginalLayout.js | 43 ++++++++++++------- .../src/test/javascript/static/TestSuite.js | 14 +++++- .../src/test/javascript/static/layout.html | 2 + 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/mindplot/src/main/javascript/layout/OriginalLayout.js b/mindplot/src/main/javascript/layout/OriginalLayout.js index ddf1c1a9..17ccc17d 100644 --- a/mindplot/src/main/javascript/layout/OriginalLayout.js +++ b/mindplot/src/main/javascript/layout/OriginalLayout.js @@ -75,7 +75,7 @@ mindplot.layout.OriginalLayout = new Class({ this._layoutChildren(node, heightById); - this._fixOverlapping(node); + this._fixOverlapping(node, heightById); }, this); }, @@ -97,21 +97,30 @@ mindplot.layout.OriginalLayout = new Class({ var heightChanged = node._branchHeight != newBranchHeight; node._heightChanged = heightChanged || parentHeightChanged; - - if (childrenOrderMoved || heightChanged || parentHeightChanged) { var sorter = node.getSorter(); var offsetById = sorter.computeOffsets(this._treeSet, node); var parentPosition = node.getPosition(); children.forEach(function(child) { - var freeDisplacement = child.getFreeDisplacement(); - var freeDisplacement = {x:0, y:0}; + var childHeightChanged = heightById[child.getId()] != child._branchHeight; var offset = offsetById[child.getId()]; + + if (child.isFree()) { + if (heightChanged && (parentHeightChanged || childHeightChanged)) { + offset.x += child.getFreeDisplacement().x; + this._shiftBranches(child); + } else { + offset.x += child.getFreeDisplacement().x; + offset.y += child.getFreeDisplacement().y; + this._shiftBranches(child); + } + } + var parentX = parentPosition.x; var parentY = parentPosition.y; - var newPos = {x:parentX + freeDisplacement.x + offset.x, y:parentY + freeDisplacement.y + offset.y}; + var newPos = {x:parentX + offset.x, y:parentY + offset.y}; this._treeSet.updateBranchPosition(child, newPos); }.bind(this)); @@ -125,24 +134,26 @@ mindplot.layout.OriginalLayout = new Class({ }, this); }, - _fixOverlapping: function(node) { + _fixOverlapping: function(node, heightById) { var children = this._treeSet.getChildren(node); - if (node.hasFreeDisplacementChanged()) { - var yOffset = node.getFreeDisplacement().y; - var branchesToShift = this._treeSet.getBranchesInVerticalDirection(node, yOffset); - this._treeSet.shiftBranchPosition(node, node.getFreeDisplacement().x, node.getFreeDisplacement().y); - - branchesToShift.forEach(function(branch) { - this._treeSet.shiftBranchPosition(branch, 0, yOffset); - },this); + if ((node.isFree() && node.hasFreeDisplacementChanged())) { + this._shiftBranches(node); } children.forEach(function(child) { - this._fixOverlapping(child); + this._fixOverlapping(child, heightById); }, this); }, + _shiftBranches: function(node) { + this._treeSet.shiftBranchPosition(node, node.getFreeDisplacement().x, node.getFreeDisplacement().y); + var branchesToShift = this._treeSet.getBranchesInVerticalDirection(node, node.getFreeDisplacement().y); + branchesToShift.forEach(function(branch) { + this._treeSet.shiftBranchPosition(branch, 0, node.getFreeDisplacement().y); + },this); + }, + _nodesCollide: function(nodeA, nodeB) { var nodeAVertex = nodeA.getVertex(); var nodeBVertex = nodeB.getVertex(); diff --git a/mindplot/src/test/javascript/static/TestSuite.js b/mindplot/src/test/javascript/static/TestSuite.js index 2f9f3f42..f30a0142 100644 --- a/mindplot/src/test/javascript/static/TestSuite.js +++ b/mindplot/src/test/javascript/static/TestSuite.js @@ -712,7 +712,7 @@ mindplot.layout.TestSuite = new Class({ manager.layout(true); manager.plot("testFreePosition5", {width:1400, height:600}); - //TODO(gb): fix this. when new node is connected, free layout is lost + console.log("add node 23 to 12:"); manager.addNode(23, mindplot.layout.TestSuite.NODE_SIZE, position); manager.connectNode(12,23,3); manager.layout(true); @@ -722,6 +722,18 @@ mindplot.layout.TestSuite = new Class({ manager.moveNode(4, {x:-300, y:190}); manager.layout(true); manager.plot("testFreePosition7", {width:1400, height:600}); + + console.log("add node 24 to 3:"); + manager.addNode(24, mindplot.layout.TestSuite.NODE_SIZE, position); + manager.connectNode(3,24,3); + manager.layout(true); + manager.plot("testFreePosition8", {width:1400, height:600}); + + console.log("add node 25 to 17:"); + manager.addNode(25, mindplot.layout.TestSuite.NODE_SIZE, position); + manager.connectNode(17,25,0); + manager.layout(true); + manager.plot("testFreePosition9", {width:1400, height:600}); } }); diff --git a/mindplot/src/test/javascript/static/layout.html b/mindplot/src/test/javascript/static/layout.html index e35e8f7e..e97c6d3a 100644 --- a/mindplot/src/test/javascript/static/layout.html +++ b/mindplot/src/test/javascript/static/layout.html @@ -118,6 +118,8 @@
+
+
\ No newline at end of file