Free position layout fixes

This commit is contained in:
Gonzalo Bellver 2012-01-26 11:53:46 -03:00
parent 42f2763ec0
commit 402226afbc
2 changed files with 19 additions and 26 deletions

View File

@ -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);
}
});

View File

@ -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