2012-01-11 19:47:00 -03:00
|
|
|
/*
|
|
|
|
* Copyright [2011] [wisemapping]
|
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-01-14 14:20:59 -03:00
|
|
|
mindplot.layout.BalancedSorter = new Class({
|
|
|
|
Extends: mindplot.layout.AbstractBasicSorter,
|
2012-01-05 17:34:14 -03:00
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2012-02-01 12:15:19 -03:00
|
|
|
predict : function(graph, parent, node, position, free) {
|
|
|
|
// If its a free node...
|
|
|
|
if (free) {
|
|
|
|
$assert($defined(position), "position cannot be null for predict in free positioning");
|
2012-02-02 12:58:25 -03:00
|
|
|
$assert($defined(node), "node cannot be null for predict in free positioning");
|
2012-02-01 12:15:19 -03:00
|
|
|
|
|
|
|
var rootNode = graph.getRootNode(parent);
|
2012-02-02 12:58:25 -03:00
|
|
|
var direction = this._getRelativeDirection(rootNode.getPosition(), node.getPosition());
|
|
|
|
|
|
|
|
var limitXPos = parent.getPosition().x + direction * (parent.getSize().width/2 + node.getSize().width/2 + mindplot.layout.BalancedSorter.INTERNODE_HORIZONTAL_PADDING);
|
2012-02-01 12:15:19 -03:00
|
|
|
|
|
|
|
var xPos = direction > 0 ?
|
2012-02-02 12:58:25 -03:00
|
|
|
(position.x >= limitXPos ? position.x : limitXPos) :
|
|
|
|
(position.x <= limitXPos ? position.x : limitXPos) ;
|
2012-02-01 12:15:19 -03:00
|
|
|
|
2012-02-02 12:41:53 -03:00
|
|
|
return [0, {x: xPos, y:position.y}];
|
2012-02-01 12:15:19 -03:00
|
|
|
}
|
|
|
|
|
2012-01-19 12:20:43 -03:00
|
|
|
var rootNode = graph.getRootNode(parent);
|
2012-01-13 16:26:37 -03:00
|
|
|
|
2012-02-02 11:30:12 -03:00
|
|
|
// If it is a dragged node...
|
|
|
|
if (node) {
|
2012-02-02 12:15:59 -03:00
|
|
|
$assert($defined(position), "position cannot be null for predict in dragging");
|
2012-02-02 11:30:12 -03:00
|
|
|
var nodeDirection = this._getRelativeDirection(rootNode.getPosition(), node.getPosition());
|
|
|
|
var positionDirection = this._getRelativeDirection(rootNode.getPosition(), position);
|
|
|
|
var siblings = graph.getSiblings(node);
|
|
|
|
|
2012-02-02 12:15:59 -03:00
|
|
|
var sameParent = parent == graph.getParent(node);
|
|
|
|
if (siblings.length == 0 && nodeDirection == positionDirection && sameParent) {
|
2012-02-02 11:30:12 -03:00
|
|
|
return [node.getOrder(), node.getPosition()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 16:26:37 -03:00
|
|
|
if (!position) {
|
|
|
|
var right = this._getChildrenForOrder(parent, graph, 0);
|
|
|
|
var left = this._getChildrenForOrder(parent, graph, 1);
|
|
|
|
}
|
2012-01-13 12:37:11 -03:00
|
|
|
// Filter nodes on one side..
|
2012-01-19 12:20:43 -03:00
|
|
|
var order = position ? (position.x > rootNode.getPosition().x ? 0 : 1) : ((right.length - left.length) > 0 ? 1 : 0);
|
|
|
|
var direction = order%2 == 0 ? 1 : -1;
|
2012-02-02 11:30:12 -03:00
|
|
|
|
|
|
|
// Exclude the dragged node (if set)
|
|
|
|
var children = this._getChildrenForOrder(parent, graph, order).filter(function(child) {
|
|
|
|
return child != node;
|
|
|
|
});
|
2012-01-13 16:26:37 -03:00
|
|
|
|
|
|
|
// No children?
|
|
|
|
if (children.length == 0) {
|
2012-01-19 12:20:43 -03:00
|
|
|
return [order, {x:parent.getPosition().x + direction * (parent.getSize().width + mindplot.layout.BalancedSorter.INTERNODE_HORIZONTAL_PADDING * 2), y:parent.getPosition().y}];
|
2012-01-13 16:26:37 -03:00
|
|
|
}
|
|
|
|
|
2012-01-05 17:34:14 -03:00
|
|
|
// Try to fit within ...
|
|
|
|
var result = null;
|
2012-01-13 12:37:11 -03:00
|
|
|
var last = children.getLast();
|
2012-01-13 16:26:37 -03:00
|
|
|
position = position || {x: last.getPosition().x, y:last.getPosition().y + 1};
|
2012-01-13 12:37:11 -03:00
|
|
|
children.each(function(child, index) {
|
2012-01-05 17:34:14 -03:00
|
|
|
var cpos = child.getPosition();
|
|
|
|
if (position.y > cpos.y) {
|
2012-01-13 12:37:11 -03:00
|
|
|
yOffset = child == last ?
|
2012-01-14 14:20:59 -03:00
|
|
|
child.getSize().height + mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING * 2 :
|
2012-01-17 00:26:29 -03:00
|
|
|
(children[index + 1].getPosition().y - child.getPosition().y) / 2;
|
2012-01-13 12:37:11 -03:00
|
|
|
result = [child.getOrder() + 2,{x:cpos.x, y:cpos.y + yOffset}];
|
2012-01-12 13:44:35 -03:00
|
|
|
}
|
2012-01-05 17:34:14 -03:00
|
|
|
});
|
|
|
|
|
2012-01-13 12:37:11 -03:00
|
|
|
// Position wasn't below any node, so it must be inserted above
|
|
|
|
if (!result) {
|
|
|
|
var first = children[0];
|
|
|
|
result = [position.x > 0 ? 0 : 1, {
|
|
|
|
x:first.getPosition().x,
|
2012-01-14 14:20:59 -03:00
|
|
|
y:first.getPosition().y - first.getSize().height - mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING * 2
|
2012-01-13 12:37:11 -03:00
|
|
|
}];
|
|
|
|
}
|
2012-01-05 17:34:14 -03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
insert: function(treeSet, parent, child, order) {
|
2012-01-13 12:37:11 -03:00
|
|
|
var children = this._getChildrenForOrder(parent, treeSet, order);
|
2012-01-05 17:34:14 -03:00
|
|
|
|
2012-02-06 11:12:27 -03:00
|
|
|
// If no children, return 0 or 1 depending on the side
|
|
|
|
if (children.length == 0) {
|
|
|
|
child.setOrder(order % 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-13 12:37:11 -03:00
|
|
|
// Shift all the elements by two, so side is the same.
|
|
|
|
// In case of balanced sorter, order don't need to be continuous...
|
|
|
|
var max = 0;
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
2012-01-05 17:34:14 -03:00
|
|
|
var node = children[i];
|
2012-01-13 12:37:11 -03:00
|
|
|
max = Math.max(max, node.getOrder());
|
|
|
|
if (node.getOrder() >= order) {
|
|
|
|
max = Math.max(max, node.getOrder() + 2);
|
|
|
|
node.setOrder(node.getOrder() + 2);
|
2012-01-11 19:47:00 -03:00
|
|
|
}
|
2012-01-05 17:34:14 -03:00
|
|
|
}
|
2012-01-13 12:37:11 -03:00
|
|
|
|
2012-01-17 00:26:29 -03:00
|
|
|
var newOrder = order > (max + 1) ? (max + 2) : order;
|
2012-01-13 12:37:11 -03:00
|
|
|
child.setOrder(newOrder);
|
2012-01-05 17:34:14 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
detach:function(treeSet, node) {
|
|
|
|
var parent = treeSet.getParent(node);
|
2012-01-13 12:37:11 -03:00
|
|
|
// Filter nodes on one side..
|
|
|
|
var children = this._getChildrenForOrder(parent, treeSet, node.getOrder());
|
|
|
|
|
|
|
|
children.each(function(child, index) {
|
|
|
|
if (child.getOrder() > node.getOrder()) {
|
|
|
|
child.setOrder(child.getOrder() - 2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
node.setOrder(node.getOrder() % 2 == 0 ? 0 : 1);
|
2012-01-05 17:34:14 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
computeOffsets:function(treeSet, node) {
|
|
|
|
$assert(treeSet, "treeSet can no be null.");
|
|
|
|
$assert(node, "node can no be null.");
|
|
|
|
|
|
|
|
var children = this._getSortedChildren(treeSet, node);
|
|
|
|
|
|
|
|
// Compute heights ...
|
2012-01-11 19:47:00 -03:00
|
|
|
var heights = children.map(
|
|
|
|
function(child) {
|
2012-01-13 20:06:14 -03:00
|
|
|
return {id:child.getId(), order:child.getOrder(), width: child.getSize().width, height:this._computeChildrenHeight(treeSet, child)};
|
2012-01-11 19:47:00 -03:00
|
|
|
}, this).reverse();
|
2012-01-05 17:34:14 -03:00
|
|
|
|
|
|
|
|
|
|
|
// Compute the center of the branch ...
|
|
|
|
var totalPHeight = 0;
|
|
|
|
var totalNHeight = 0;
|
2012-01-10 09:44:33 -03:00
|
|
|
|
2012-01-05 17:34:14 -03:00
|
|
|
heights.forEach(function(elem) {
|
|
|
|
if (elem.order % 2 == 0) {
|
|
|
|
totalPHeight += elem.height;
|
|
|
|
} else {
|
|
|
|
totalNHeight += elem.height;
|
|
|
|
}
|
|
|
|
});
|
2012-01-11 19:47:00 -03:00
|
|
|
var psum = totalPHeight / 2;
|
|
|
|
var nsum = totalNHeight / 2;
|
2012-01-05 17:34:14 -03:00
|
|
|
var ysum = 0;
|
|
|
|
|
|
|
|
// Calculate the offsets ...
|
|
|
|
var result = {};
|
|
|
|
for (var i = 0; i < heights.length; i++) {
|
|
|
|
var direction = heights[i].order % 2 ? -1 : 1;
|
|
|
|
|
|
|
|
if (direction > 0) {
|
|
|
|
psum = psum - heights[i].height;
|
|
|
|
ysum = psum;
|
|
|
|
} else {
|
|
|
|
nsum = nsum - heights[i].height;
|
|
|
|
ysum = nsum;
|
|
|
|
}
|
|
|
|
|
2012-01-11 19:47:00 -03:00
|
|
|
var yOffset = ysum + heights[i].height / 2;
|
2012-01-17 00:26:29 -03:00
|
|
|
var xOffset = direction * (node.getSize().width / 2 + heights[i].width / 2 + + mindplot.layout.BalancedSorter.INTERNODE_HORIZONTAL_PADDING);
|
2012-01-05 17:34:14 -03:00
|
|
|
|
|
|
|
$assert(!isNaN(xOffset), "xOffset can not be null");
|
|
|
|
$assert(!isNaN(yOffset), "yOffset can not be null");
|
|
|
|
|
|
|
|
result[heights[i].id] = {x:xOffset,y:yOffset};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2012-01-11 19:47:00 -03:00
|
|
|
verify:function(treeSet, node) {
|
2012-01-13 12:37:11 -03:00
|
|
|
// Check that all is consistent ...
|
|
|
|
var children = this._getChildrenForOrder(node, treeSet, node.getOrder());
|
|
|
|
|
|
|
|
// All odd ordered nodes should be "continuous" by themselves
|
|
|
|
// All even numbered nodes should be "continuous" by themselves
|
|
|
|
var factor = node.getOrder() % 2 == 0 ? 2 : 1;
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
2012-01-13 16:26:37 -03:00
|
|
|
var order = i == 0 && factor == 1 ? 1 : (factor * i);
|
2012-01-17 00:26:29 -03:00
|
|
|
$assert(children[i].getOrder() == order, "Missing order elements. Missing order: " + (i * factor));
|
2012-01-13 12:37:11 -03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-01-26 13:07:45 -03:00
|
|
|
getDirection: function(treeSet, node) {
|
|
|
|
return node.getOrder() % 2 == 0 ? 1 : -1;
|
|
|
|
},
|
|
|
|
|
2012-01-13 12:37:11 -03:00
|
|
|
toString:function() {
|
|
|
|
return "Balanced Sorter";
|
2012-01-17 00:26:29 -03:00
|
|
|
},
|
|
|
|
|
2012-02-02 11:30:12 -03:00
|
|
|
_getOrderForPosition: function(rootNode, position) {
|
|
|
|
return position.x > rootNode.getPosition().x ? 0 : 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getChildrenForSide: function(parent, graph, position) {
|
|
|
|
position = position || {x: parent.getPosition().x + 1, y:parent.getPosition().y + 1};
|
|
|
|
var rootPosition = graph.getRootNode(parent).getPosition();
|
|
|
|
return graph.getChildren(parent).filter(function(child) {
|
|
|
|
return position.x > rootPosition.x ? child.getPosition().x > rootPosition.x : child.getPosition().x < rootPosition.x;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getChildrenForOrder: function (parent, graph, order) {
|
|
|
|
return this._getSortedChildren(graph, parent).filter(function(child) {
|
|
|
|
return child.getOrder() % 2 == order % 2;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_getVerticalPadding: function() {
|
2012-01-17 00:26:29 -03:00
|
|
|
return mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING;
|
2012-01-05 17:34:14 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-14 14:20:59 -03:00
|
|
|
mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING = 5;
|
|
|
|
mindplot.layout.BalancedSorter.INTERNODE_HORIZONTAL_PADDING = 30;
|