wisemapping-open-source/mindplot/src/main/javascript/nlayout/OriginalLayout.js

129 lines
4.6 KiB
JavaScript
Raw Normal View History

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.
*/
mindplot.nlayout.OriginalLayout = new Class({
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' ?
mindplot.nlayout.OriginalLayout.BALANCED_SORTER :
mindplot.nlayout.OriginalLayout.SYMMETRIC_SORTER;
return new mindplot.nlayout.Node(id, size, position, strategy);
},
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);
// 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);
$assert(this._treeSet.getParent(node), "Node already disconnected");
2011-12-12 03:47:01 +01:00
// Remove from children list.
var sorter = node.getSorter();
sorter.detach(this._treeSet, node);
// Disconnect the new node ...
this._treeSet.disconnect(nodeId);
// Fire a basic validation ...
sorter.verify(this._treeSet, node);
},
layout: function() {
var roots = this._treeSet.getTreeRoots();
roots.forEach(function(node) {
// Calculate all node heights ...
var sorter = node.getSorter();
// @Todo: This must not be implemented in this way.Each sorter could have different notion of heights ...
var heightById = sorter.computeChildrenIdByHeights(this._treeSet, node);
this._layoutChildren(node, heightById);
2011-12-12 03:47:01 +01:00
}, this);
},
_layoutChildren: function(node, heightById) {
var nodeId = node.getId();
var children = this._treeSet.getChildren(node);
var childrenOrderMoved = children.some(function(child) {
return child.hasOrderChanged();
});
// 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];
var heightChanged = node._branchHeight != newBranchHeight;
if (childrenOrderMoved || heightChanged) {
var sorter = node.getSorter();
var offsetById = sorter.computeOffsets(this._treeSet, node);
var parentPosition = node.getPosition();
children.forEach(function(child) {
var offset = offsetById[child.getId()];
var parentX = parentPosition.x;
var parentY = parentPosition.y;
var verticalOffset = (node.getSize().height / 2);
2012-01-02 21:38:44 +01:00
//TODO(gb): Not sure why verticalOffset is added again, since it is considered in the sorter.
2012-01-02 21:38:02 +01:00
var newPos = {x:parentX + offset.x,y:parentY + offset.y};
// var newPos = {x:parentX + offset.x,y:parentY + offset.y + verticalOffset};
this._treeSet.updateBranchPosition(child, newPos);
}.bind(this));
2011-12-12 03:47:01 +01:00
node._branchHeight = newBranchHeight;
}
// Continue reordering the children nodes ...
children.forEach(function(child) {
this._layoutChildren(child, heightById);
2011-12-12 03:47:01 +01:00
}, this);
}
});
mindplot.nlayout.OriginalLayout.SYMMETRIC_SORTER = new mindplot.nlayout.SymmetricSorter();
2012-01-02 21:10:14 +01:00
mindplot.nlayout.OriginalLayout.GRID_SORTER = new mindplot.nlayout.GridSorter();
2012-01-05 21:34:14 +01:00
mindplot.nlayout.OriginalLayout.BALANCED_SORTER = new mindplot.nlayout.BalancedSorter();