diff --git a/mindplot/src/main/javascript/layout/OriginalLayout.js b/mindplot/src/main/javascript/layout/OriginalLayout.js index f627a5d4..a37295f4 100644 --- a/mindplot/src/main/javascript/layout/OriginalLayout.js +++ b/mindplot/src/main/javascript/layout/OriginalLayout.js @@ -105,25 +105,15 @@ mindplot.layout.OriginalLayout = new Class({ var parentPosition = node.getPosition(); children.forEach(function(child) { - 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); - } - } + offset.x += child.getFreeDisplacement().x; + offset.y += child.getFreeDisplacement().y; var parentX = parentPosition.x; var parentY = parentPosition.y; var newPos = {x:parentX + offset.x, y:parentY + offset.y}; - this._treeSet.updateBranchPosition(child, newPos); }.bind(this)); @@ -139,8 +129,8 @@ mindplot.layout.OriginalLayout = new Class({ _fixOverlapping: function(node, heightById) { var children = this._treeSet.getChildren(node); - if ((node.isFree() && node.hasFreeDisplacementChanged())) { - this._shiftBranches(node); + if (node.isFree()) { + this._shiftBranches(node, heightById); } children.forEach(function(child) { @@ -148,19 +138,25 @@ mindplot.layout.OriginalLayout = new Class({ }, this); }, - _shiftBranches: function(node) { - this._treeSet.shiftBranchPosition(node, node.getFreeDisplacement().x, node.getFreeDisplacement().y); + _shiftBranches: function(node, heightById) { var branchesToShift = this._treeSet.getBranchesInVerticalDirection(node, node.getFreeDisplacement().y); + + var last = node; branchesToShift.forEach(function(branch) { - this._treeSet.shiftBranchPosition(branch, 0, node.getFreeDisplacement().y); + if (this._branchesOverlap(branch, last, heightById)) { + this._treeSet.shiftBranchPosition(branch, 0, node.getFreeDisplacement().y); + } + last = branch; },this); }, - _nodesCollide: function(nodeA, nodeB) { - var nodeAVertex = nodeA.getVertex(); - var nodeBVertex = nodeB.getVertex(); - return nodeAVertex.a.x < nodeBVertex.b.x && nodeAVertex.b.x > nodeBVertex.a.x && - nodeAVertex.a.y < nodeBVertex.b.y && nodeAVertex.b.y > nodeBVertex.a.y; + _branchesOverlap: function(branchA, branchB, heightById) { + var topA = branchA.getPosition().y - heightById[branchA.getId()]/2; + var bottomA = branchA.getPosition().y + heightById[branchA.getId()]/2; + var topB = branchB.getPosition().y - heightById[branchB.getId()]/2; + var bottomB = branchB.getPosition().y + heightById[branchB.getId()]/2; + + return !(topA >= bottomB || bottomA <= topB); } }); diff --git a/mindplot/src/main/javascript/layout/RootedTreeSet.js b/mindplot/src/main/javascript/layout/RootedTreeSet.js index ca3876ac..4af7438a 100644 --- a/mindplot/src/main/javascript/layout/RootedTreeSet.js +++ b/mindplot/src/main/javascript/layout/RootedTreeSet.js @@ -235,10 +235,7 @@ mindplot.layout.RootedTreeSet = new Class({ getBranchesInVerticalDirection: function(node, yOffset) { // siblings with lower or higher order, depending on the direction of the offset var siblings = this.getSiblings(node).filter(function(sibling) { - if (yOffset < 0) - return sibling.getOrder() < node.getOrder(); - else - return sibling.getOrder() > node.getOrder(); + return yOffset < 0 ? sibling.getOrder() < node.getOrder() : sibling.getOrder() > node.getOrder(); }); // direct descendants of the root that do not contain the node and are on the same side