SymmetricSorter predict optimization

This commit is contained in:
Gonzalo Bellver 2012-08-17 17:32:57 -03:00
parent ea75b33553
commit c432bef6b1

View File

@ -21,13 +21,26 @@ mindplot.layout.SymmetricSorter = new Class({
}, },
/**
* Predict the order and position of a dragged node.
*
* @param graph The tree set
* @param parent The parent of the node
* @param node The node
* @param position The position of the drag
* @param free Free drag or not
* @return {*}
*/
predict:function (graph, parent, node, position, free) { predict:function (graph, parent, node, position, free) {
var self = this;
var rootNode = graph.getRootNode(parent);
// If its a free node... // If its a free node...
if (free) { if (free) {
$assert($defined(position), "position cannot be null for predict in free positioning"); $assert($defined(position), "position cannot be null for predict in free positioning");
$assert($defined(node), "node cannot be null for predict in free positioning"); $assert($defined(node), "node cannot be null for predict in free positioning");
var rootNode = graph.getRootNode(parent);
var direction = this._getRelativeDirection(rootNode.getPosition(), parent.getPosition()); var direction = this._getRelativeDirection(rootNode.getPosition(), parent.getPosition());
var limitXPos = parent.getPosition().x + direction * (parent.getSize().width / 2 + node.getSize().width / 2 + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING); var limitXPos = parent.getPosition().x + direction * (parent.getSize().width / 2 + node.getSize().width / 2 + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING);
@ -38,67 +51,81 @@ mindplot.layout.SymmetricSorter = new Class({
return [0, {x:xPos, y:position.y}]; return [0, {x:xPos, y:position.y}];
} }
var rootNode = graph.getRootNode(parent); // Its not a dragged node (it is being added)
if (!node) {
var parentDirection = self._getRelativeDirection(rootNode.getPosition(), parent.getPosition());
var position = {
x:parent.getPosition().x + parentDirection * (parent.getSize().width + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING),
y:parent.getPosition().y
};
return [graph.getChildren(parent).length, position];
}
// If it is a dragged node... // If it is a dragged node...
if (node) {
$assert($defined(position), "position cannot be null for predict in dragging"); $assert($defined(position), "position cannot be null for predict in dragging");
var nodeDirection = this._getRelativeDirection(rootNode.getPosition(), node.getPosition()); var nodeDirection = this._getRelativeDirection(rootNode.getPosition(), node.getPosition());
var positionDirection = this._getRelativeDirection(rootNode.getPosition(), position); var positionDirection = this._getRelativeDirection(rootNode.getPosition(), position);
var siblings = graph.getSiblings(node); var siblings = graph.getSiblings(node);
// node has no siblings and its trying to reconnect to its own parent
var sameParent = parent == graph.getParent(node); var sameParent = parent == graph.getParent(node);
if (siblings.length == 0 && nodeDirection == positionDirection && sameParent) { if (siblings.length == 0 && nodeDirection == positionDirection && sameParent) {
return [node.getOrder() + 1, node.getPosition()]; return [node.getOrder(), node.getPosition()];
}
} }
// Regular node var parentChildren = graph.getChildren(parent);
var direction = parent.getPosition().x > rootNode.getPosition().x ? 1 : -1;
// No children... if (parentChildren.length == 0) {
var children = graph.getChildren(parent).filter(function (child) { // Fit as a child of the parent node...
return child != node; var position = {
}); x:parent.getPosition().x + positionDirection * (parent.getSize().width + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING),
if (children.length == 0) {
position = position || {x:parent.getPosition().x + direction, y:parent.getPosition().y};
var pos = {
x:parent.getPosition().x + direction * (parent.getSize().width + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING),
y:parent.getPosition().y y:parent.getPosition().y
}; };
return [0, pos]; return [0, position];
} } else {
// Try to fit within ... // Try to fit within ...
var result = null; var result = null;
var last = children.getLast(); var last = parentChildren.getLast();
position = position || {x:last.getPosition().x + direction, y:last.getPosition().y + 1}; for (var i=0; i<parentChildren.length; i++) {
children.each(function (child, index) { var parentChild = parentChildren[i];
var cpos = child.getPosition(); var nodeAfter = (i + 1) == parentChild.length ? null : parentChildren[i+1];
if (position.y > cpos.y) {
var yOffset = child == last ?
child.getSize().height + mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2 :
(children[index + 1].getPosition().y + children[index + 1].getSize().height / 2 - child.getPosition().y) / 2;
result = [child.getOrder() + 1, {x:cpos.x, y:cpos.y + yOffset}];
}
});
// Position wasn't below any node, so it must be inserted above // Fit at the bottom
if (!result) { if (!nodeAfter && position.y > parentChild.getPosition().y) {
var first = children[0]; var order = graph.getParent(node).getId() == parent.getId() ?
result = [0, { last.getOrder() : last.getOrder() + 1;
var position = {
x: parentChild.getPosition().x,
y: parentChild.getPosition().y + parentChild.getSize().height + mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2
}
return [order, position];
}
// Fit after this node
if (nodeAfter && position.y > parentChild.getPosition().y && position.y < nodeAfter.getPosition().y) {
if(nodeAfter.getId() == node.getId() || parentChild.getId() == node.getId()) {
return [node.getOrder(), node.getPosition()];
} else {
var order = position.y > node.getPosition().y ?
nodeAfter.getOrder() - 1 : parentChild.getOrder() + 1;
var position = {
x: parentChild.getPosition().x,
y: parentChild.getPosition().y + (nodeAfter.getPosition().y - parentChild.getPosition().y) / 2
}
return [order, position];
}
}
}
}
// Position wasn't below any node, so it must be fitted above the first
var first = parentChildren[0];
var position = {
x:first.getPosition().x, x:first.getPosition().x,
y:first.getPosition().y - first.getSize().height - mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2 y:first.getPosition().y - first.getSize().height - mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2
}]; };
return result; return [0, position];
}
// if the node is moving down, substract 1 from the order
if (node != null && parent == graph.getParent(node) && result[1].y > node.getPosition().y) {
result[0] = result[0] - 1;
}
return result;
}, },
insert:function (treeSet, parent, child, order) { insert:function (treeSet, parent, child, order) {