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

173 lines
5.0 KiB
JavaScript
Raw Normal View History

mindplot.nlayout.RootedTreeSet = new Class({
initialize:function() {
this._rootNodes = [];
},
setRoot:function(root) {
$assert(root, 'root can not be null');
this._rootNodes.push(this._decodate(root));
},
getTreeRoots:function() {
return this._rootNodes;
},
_decodate:function(node) {
node._children = [];
return node;
},
add: function(node) {
$assert(node, 'node can not be null');
$assert(!node._children, 'node already added');
this._rootNodes.push(this._decodate(node));
},
2011-12-12 03:47:01 +01:00
remove: function(nodeId) {
$assert($defined(nodeId), 'nodeId can not be null');
var node = this.find(nodeId);
this._rootNodes.erase(node);
},
connect: function(parentId, childId) {
$assert($defined(parentId), 'parent can not be null');
$assert($defined(childId), 'child can not be null');
2011-12-12 03:47:01 +01:00
var parent = this.find(parentId);
var child = this.find(childId, true);
$assert(!child._parent, 'node already connected. Id:' + child.getId() + ",previous:" + child._parent);
parent._children.push(child);
child._parent = parent;
this._rootNodes.erase(child);
},
disconnect: function(nodeId) {
2011-12-12 03:47:01 +01:00
$assert($defined(nodeId), 'nodeId can not be null');
var node = this.find(nodeId);
$assert(node._parent, "Node is not connected");
node._parent._children.erase(node);
2011-12-12 03:47:01 +01:00
this._rootNodes.push(node);
node._parent = null;
},
find:function(id, validate) {
$assert($defined(id), 'id can not be null');
var graphs = this._rootNodes;
var result = null;
for (var i = 0; i < graphs.length; i++) {
var node = graphs[i];
result = this._find(id, node);
if (result) {
break;
}
}
2011-12-12 03:47:01 +01:00
$assert($defined(validate) ? result : true, 'node could not be found id:' + id);
return result;
},
_find:function(id, parent) {
if (parent.getId() == id) {
return parent;
}
var result = null;
var children = parent._children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
result = this._find(id, child);
if (result)
break;
}
return result;
},
getChildren:function(node) {
$assert(node, 'node can not be null');
return node._children;
},
2011-12-12 03:47:01 +01:00
getParent:function(node) {
$assert(node, 'node can not be null');
return node._parent;
},
dump: function() {
var branches = this._rootNodes;
var result = "";
for (var i = 0; i < branches.length; i++) {
var branch = branches[i];
result += this._dump(branch, "");
}
return result;
},
_dump:function(node, indent) {
var result = indent + node + "\n";
var children = this.getChildren(node);
for (var i = 0; i < children.length; i++) {
var child = children[i];
result += this._dump(child, indent + " ");
}
return result;
},
2011-12-30 19:28:03 +01:00
plot: function(canvas) {
var branches = this._rootNodes;
for (var i=0; i<branches.length; i++) {
var branch = branches[i];
this._plot(canvas, branch);
}
},
_plot: function(canvas, node, root) {
var children = this.getChildren(node);
var cx = node.getPosition().x + canvas.width/2 - node.getSize().width/2;
var cy = node.getPosition().y + canvas.height/2 - node.getSize().height/2;
var rect = canvas.rect(cx, cy, node.getSize().width, node.getSize().height);
var order = node.getOrder() == null ? "r" : node.getOrder();
var text = canvas.text(node.getPosition().x + canvas.width/2, node.getPosition().y + canvas.height/2, node.getId() + "[" + order + "]");
text.attr('fill', '#FFF');
2011-12-30 19:28:03 +01:00
var fillColor = this._rootNodes.contains(node) ? "#000" : "#c00";
rect.attr('fill', fillColor);
for (var i=0; i<children.length; i++) {
var child = children[i];
this._plot(canvas, child);
}
},
updateBranchPosition : function(node, position) {
var oldPos = node.getPosition();
node.setPosition(position);
var xOffset = oldPos.x - position.x;
var yOffset = oldPos.y - position.y;
var children = this.getChildren(node);
children.forEach(function(child) {
this._shiftBranchPosition(child, xOffset, yOffset);
}.bind(this));
},
_shiftBranchPosition : function(node, xOffset, yOffset) {
var position = node.getPosition();
node.setPosition({x:position.x + xOffset, y:position.y + yOffset});
var children = this.getChildren(node);
children.forEach(function(child) {
this._shiftBranchPosition(child, xOffset, yOffset);
}.bind(this));
}
});