2012-01-10 03:50:52 +01: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 18:20:59 +01:00
|
|
|
mindplot.layout.SymmetricSorter = new Class({
|
|
|
|
Extends: mindplot.layout.AbstractBasicSorter,
|
2011-12-11 17:13:43 +01:00
|
|
|
initialize:function() {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
predict : function(parent, graph, position) {
|
2012-01-13 20:26:37 +01:00
|
|
|
var direction = parent.getPosition().x > 0 ? 1 : -1;
|
2011-12-11 17:13:43 +01:00
|
|
|
|
|
|
|
// No children...
|
|
|
|
var children = graph.getChildren(parent);
|
|
|
|
if (children.length == 0) {
|
2012-01-13 20:26:37 +01:00
|
|
|
position = position || {x:parent.getPosition().x + direction, y:parent.getPosition().y};
|
2012-01-12 23:58:18 +01:00
|
|
|
var position = {
|
2012-01-14 18:20:59 +01:00
|
|
|
x: parent.getPosition().x + direction * (parent.getSize().width + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING),
|
2012-01-12 23:58:18 +01:00
|
|
|
y:parent.getPosition().y
|
|
|
|
}
|
|
|
|
return [0, position];
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fit within ...
|
|
|
|
var result = null;
|
2012-01-12 23:58:18 +01:00
|
|
|
var last = children.getLast();
|
2012-01-13 20:26:37 +01:00
|
|
|
position = position || {x:last.getPosition().x + direction, y:last.getPosition().y + 1};
|
2012-01-12 23:58:18 +01:00
|
|
|
children.each(function(child, index) {
|
2011-12-11 17:13:43 +01:00
|
|
|
var cpos = child.getPosition();
|
|
|
|
if (position.y > cpos.y) {
|
2012-01-12 23:58:18 +01:00
|
|
|
yOffset = child == last ?
|
2012-01-14 18:20:59 +01:00
|
|
|
child.getSize().height + mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2 :
|
2012-01-14 00:06:14 +01:00
|
|
|
(children[index + 1].getPosition().y + children[index + 1].getSize().height/2 - child.getPosition().y)/2;
|
2012-01-12 23:58:18 +01:00
|
|
|
result = [child.getOrder() + 1,{x:cpos.x, y:cpos.y + yOffset}];
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-12 23:58:18 +01:00
|
|
|
// Position wasn't below any node, so it must be inserted above
|
|
|
|
if (!result) {
|
|
|
|
var first = children[0];
|
|
|
|
result = [0, {
|
|
|
|
x:first.getPosition().x,
|
2012-01-14 18:20:59 +01:00
|
|
|
y:first.getPosition().y - first.getSize().height - mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2
|
2012-01-12 23:58:18 +01:00
|
|
|
}];
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
insert: function(treeSet, parent, child, order) {
|
2011-12-12 03:47:01 +01:00
|
|
|
var children = this._getSortedChildren(treeSet, parent);
|
2011-12-11 17:13:43 +01:00
|
|
|
$assert(order <= children.length, "Order must be continues and can not have holes. Order:" + order);
|
|
|
|
|
|
|
|
// Shift all the elements in one .
|
|
|
|
for (var i = order; i < children.length; i++) {
|
|
|
|
var node = children[i];
|
2011-12-12 03:47:01 +01:00
|
|
|
node.setOrder(i + 1);
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
child.setOrder(order);
|
|
|
|
},
|
|
|
|
|
2011-12-12 03:47:01 +01:00
|
|
|
detach:function(treeSet, node) {
|
|
|
|
var parent = treeSet.getParent(node);
|
|
|
|
var children = this._getSortedChildren(treeSet, parent);
|
|
|
|
var order = node.getOrder();
|
|
|
|
$assert(children[order] === node, "Node seems not to be in the right position");
|
|
|
|
|
|
|
|
// Shift all the nodes ...
|
|
|
|
for (var i = node.getOrder() + 1; i < children.length; i++) {
|
|
|
|
var child = children[i];
|
|
|
|
child.setOrder(child.getOrder() - 1);
|
|
|
|
}
|
|
|
|
node.setOrder(0);
|
|
|
|
},
|
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
computeOffsets:function(treeSet, node) {
|
|
|
|
$assert(treeSet, "treeSet can no be null.");
|
|
|
|
$assert(node, "node can no be null.");
|
|
|
|
|
2011-12-12 03:47:01 +01:00
|
|
|
var children = this._getSortedChildren(treeSet, node);
|
2011-12-11 17:13:43 +01:00
|
|
|
|
|
|
|
// Compute heights ...
|
2012-01-11 23:47:00 +01:00
|
|
|
var heights = children.map(
|
|
|
|
function(child) {
|
2012-01-14 00:06:14 +01:00
|
|
|
return {id:child.getId(), order:child.getOrder(), position: child.getPosition(), width: child.getSize().width, height: this._computeChildrenHeight(treeSet, child)};
|
2012-01-11 23:47:00 +01:00
|
|
|
}, this).reverse();
|
2011-12-30 21:48:11 +01:00
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
// Compute the center of the branch ...
|
|
|
|
var totalHeight = 0;
|
|
|
|
heights.forEach(function(elem) {
|
|
|
|
totalHeight += elem.height;
|
|
|
|
});
|
|
|
|
var ysum = totalHeight / 2;
|
|
|
|
|
|
|
|
// Calculate the offsets ...
|
|
|
|
var result = {};
|
|
|
|
for (var i = 0; i < heights.length; i++) {
|
|
|
|
ysum = ysum - heights[i].height;
|
2012-01-10 13:44:33 +01:00
|
|
|
var parent = treeSet.getParent(treeSet.find(heights[i].id));
|
2012-01-12 19:51:50 +01:00
|
|
|
|
|
|
|
//TODO(gb): actually compare to branch's root node position
|
2012-01-10 13:44:33 +01:00
|
|
|
var direction = parent.getPosition().x > 0 ? 1 : -1;
|
|
|
|
|
2012-01-11 23:47:00 +01:00
|
|
|
var yOffset = ysum + heights[i].height / 2;
|
2012-01-14 18:20:59 +01:00
|
|
|
var xOffset = direction * (heights[i].width/2 + node.getSize().width/2 + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING);
|
2011-12-11 17:13:43 +01: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;
|
2011-12-30 21:48:11 +01:00
|
|
|
},
|
|
|
|
|
2012-01-11 23:47:00 +01:00
|
|
|
verify:function(treeSet, node) {
|
|
|
|
// Check that all is consistent ...
|
|
|
|
var children = this._getSortedChildren(treeSet, node);
|
|
|
|
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
|
|
|
$assert(children[i].getOrder() == i, "missing order elements");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-12-30 21:48:11 +01:00
|
|
|
toString:function() {
|
|
|
|
return "Symmetric Sorter";
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-14 18:20:59 +01:00
|
|
|
mindplot.layout.SymmetricSorter.INTERNODE_VERTICAL_PADDING = 5;
|
|
|
|
mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING = 30;
|
2011-12-11 17:13:43 +01:00
|
|
|
|
|
|
|
|