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.OriginalLayout = new Class({
|
2011-12-11 17:13:43 +01:00
|
|
|
initialize: function(treeSet) {
|
|
|
|
this._treeSet = treeSet;
|
|
|
|
},
|
|
|
|
|
|
|
|
createNode:function(id, size, position, type) {
|
|
|
|
$assert($defined(id), "id can not be null");
|
|
|
|
$assert(size, "size can not be null");
|
|
|
|
$assert(position, "position can not be null");
|
|
|
|
$assert(type, "type can not be null");
|
|
|
|
|
2012-01-05 21:34:14 +01:00
|
|
|
var strategy = type === 'root' ?
|
2012-01-14 18:20:59 +01:00
|
|
|
mindplot.layout.OriginalLayout.BALANCED_SORTER :
|
|
|
|
mindplot.layout.OriginalLayout.SYMMETRIC_SORTER;
|
|
|
|
return new mindplot.layout.Node(id, size, position, strategy);
|
2011-12-11 17:13:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
connectNode: function(parentId, childId, order) {
|
|
|
|
|
|
|
|
var parent = this._treeSet.find(parentId);
|
|
|
|
var child = this._treeSet.find(childId);
|
|
|
|
|
|
|
|
// Insert the new node ...
|
|
|
|
var sorter = parent.getSorter();
|
|
|
|
sorter.insert(this._treeSet, parent, child, order);
|
|
|
|
|
2011-12-12 03:47:01 +01:00
|
|
|
// Connect the new node ...
|
|
|
|
this._treeSet.connect(parentId, childId);
|
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
// Fire a basic validation ...
|
|
|
|
sorter.verify(this._treeSet, parent);
|
|
|
|
},
|
|
|
|
|
2011-12-12 03:47:01 +01:00
|
|
|
disconnectNode: function(nodeId) {
|
|
|
|
var node = this._treeSet.find(nodeId);
|
2012-01-13 16:37:11 +01:00
|
|
|
var parent = this._treeSet.getParent(node);
|
|
|
|
$assert(parent, "Node already disconnected");
|
2011-12-12 03:47:01 +01:00
|
|
|
|
2012-01-18 23:15:01 +01:00
|
|
|
// Make it fixed
|
|
|
|
node.setFree(false);
|
|
|
|
node.setFreeDisplacement({x:0, y:0});
|
|
|
|
|
2011-12-12 03:47:01 +01:00
|
|
|
// Remove from children list.
|
2012-01-13 16:37:11 +01:00
|
|
|
var sorter = parent.getSorter();
|
2011-12-12 03:47:01 +01:00
|
|
|
sorter.detach(this._treeSet, node);
|
|
|
|
|
|
|
|
// Disconnect the new node ...
|
|
|
|
this._treeSet.disconnect(nodeId);
|
|
|
|
|
|
|
|
// Fire a basic validation ...
|
|
|
|
sorter.verify(this._treeSet, node);
|
|
|
|
},
|
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
layout: function() {
|
|
|
|
var roots = this._treeSet.getTreeRoots();
|
|
|
|
roots.forEach(function(node) {
|
|
|
|
|
|
|
|
// Calculate all node heights ...
|
|
|
|
var sorter = node.getSorter();
|
|
|
|
|
|
|
|
var heightById = sorter.computeChildrenIdByHeights(this._treeSet, node);
|
|
|
|
|
|
|
|
this._layoutChildren(node, heightById);
|
2012-01-16 22:37:01 +01:00
|
|
|
|
2012-01-18 17:34:30 +01:00
|
|
|
this._fixOverlapping(node, heightById);
|
2011-12-12 03:47:01 +01:00
|
|
|
}, this);
|
2011-12-11 17:13:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
_layoutChildren: function(node, heightById) {
|
|
|
|
|
|
|
|
var nodeId = node.getId();
|
|
|
|
var children = this._treeSet.getChildren(node);
|
2012-01-12 19:51:50 +01:00
|
|
|
var parent = this._treeSet.getParent(node);
|
2012-01-15 22:08:31 +01:00
|
|
|
var childrenOrderMoved = children.some(function(child) { return child.hasOrderChanged(); });
|
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
// If ether any of the nodes has been changed of position or the height of the children is not
|
|
|
|
// the same, children nodes must be repositioned ....
|
2011-12-12 03:47:01 +01:00
|
|
|
var newBranchHeight = heightById[nodeId];
|
2012-01-12 19:51:50 +01:00
|
|
|
|
|
|
|
var parentHeightChanged = $defined(parent) ? parent._heightChanged : false;
|
2011-12-12 03:47:01 +01:00
|
|
|
var heightChanged = node._branchHeight != newBranchHeight;
|
2012-01-12 19:51:50 +01:00
|
|
|
node._heightChanged = heightChanged || parentHeightChanged;
|
2011-12-11 17:13:43 +01:00
|
|
|
|
2012-01-17 21:45:51 +01:00
|
|
|
if (childrenOrderMoved || heightChanged || parentHeightChanged) {
|
2011-12-11 17:13:43 +01:00
|
|
|
var sorter = node.getSorter();
|
|
|
|
var offsetById = sorter.computeOffsets(this._treeSet, node);
|
|
|
|
var parentPosition = node.getPosition();
|
|
|
|
|
|
|
|
children.forEach(function(child) {
|
2012-01-18 17:34:30 +01:00
|
|
|
var childHeightChanged = heightById[child.getId()] != child._branchHeight;
|
2011-12-11 17:13:43 +01:00
|
|
|
var offset = offsetById[child.getId()];
|
2012-01-18 17:34:30 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-31 18:52:13 +01:00
|
|
|
var parentX = parentPosition.x;
|
|
|
|
var parentY = parentPosition.y;
|
|
|
|
|
2012-01-18 17:34:30 +01:00
|
|
|
var newPos = {x:parentX + offset.x, y:parentY + offset.y};
|
2012-01-02 21:38:02 +01:00
|
|
|
|
2011-12-11 17:13:43 +01:00
|
|
|
this._treeSet.updateBranchPosition(child, newPos);
|
|
|
|
}.bind(this));
|
2011-12-12 03:47:01 +01:00
|
|
|
|
|
|
|
node._branchHeight = newBranchHeight;
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continue reordering the children nodes ...
|
|
|
|
children.forEach(function(child) {
|
|
|
|
this._layoutChildren(child, heightById);
|
2011-12-12 03:47:01 +01:00
|
|
|
}, this);
|
2012-01-16 22:37:01 +01:00
|
|
|
},
|
|
|
|
|
2012-01-18 17:34:30 +01:00
|
|
|
_fixOverlapping: function(node, heightById) {
|
2012-01-16 22:37:01 +01:00
|
|
|
var children = this._treeSet.getChildren(node);
|
|
|
|
|
2012-01-18 17:34:30 +01:00
|
|
|
if ((node.isFree() && node.hasFreeDisplacementChanged())) {
|
|
|
|
this._shiftBranches(node);
|
2012-01-17 21:45:51 +01:00
|
|
|
}
|
2012-01-16 22:37:01 +01:00
|
|
|
|
|
|
|
children.forEach(function(child) {
|
2012-01-18 17:34:30 +01:00
|
|
|
this._fixOverlapping(child, heightById);
|
2012-01-16 22:37:01 +01:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2012-01-18 17:34:30 +01:00
|
|
|
_shiftBranches: function(node) {
|
|
|
|
this._treeSet.shiftBranchPosition(node, node.getFreeDisplacement().x, node.getFreeDisplacement().y);
|
|
|
|
var branchesToShift = this._treeSet.getBranchesInVerticalDirection(node, node.getFreeDisplacement().y);
|
|
|
|
branchesToShift.forEach(function(branch) {
|
|
|
|
this._treeSet.shiftBranchPosition(branch, 0, node.getFreeDisplacement().y);
|
|
|
|
},this);
|
|
|
|
},
|
|
|
|
|
2012-01-16 22:37:01 +01:00
|
|
|
_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;
|
2011-12-11 17:13:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-01-14 18:20:59 +01:00
|
|
|
mindplot.layout.OriginalLayout.SYMMETRIC_SORTER = new mindplot.layout.SymmetricSorter();
|
|
|
|
mindplot.layout.OriginalLayout.BALANCED_SORTER = new mindplot.layout.BalancedSorter();
|
2011-12-11 17:13:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|