Root nodes support any position

This commit is contained in:
Gonzalo Bellver 2012-01-15 15:41:03 -03:00
parent 3eea7b9e88
commit 75af2aac87
5 changed files with 24 additions and 12 deletions

View File

@ -179,8 +179,9 @@ mindplot.layout.BalancedSorter = new Class({
_getChildrenForSide: function(parent, graph, position) { _getChildrenForSide: function(parent, graph, position) {
position = position || {x: parent.getPosition().x + 1, y:parent.getPosition().y + 1}; 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 graph.getChildren(parent).filter(function(child) {
return position.x > 0 ? child.getPosition().x > 0 : child.getPosition().x < 0; return position.x > rootPosition.x ? child.getPosition().x > rootPosition.x : child.getPosition().x < rootPosition.x;
}); });
}, },

View File

@ -20,11 +20,12 @@ mindplot.layout.LayoutManager = new Class({
initialize: function(rootNodeId, rootSize) { initialize: function(rootNodeId, rootSize) {
$assert($defined(rootNodeId), "rootNodeId can not be null"); $assert($defined(rootNodeId), "rootNodeId can not be null");
$assert(rootSize, "rootSize can not be null"); $assert(rootSize, "rootSize can not be null");
var position = position || {x:0, y:0};
this._treeSet = new mindplot.layout.RootedTreeSet(); this._treeSet = new mindplot.layout.RootedTreeSet();
this._layout = new mindplot.layout.OriginalLayout(this._treeSet); this._layout = new mindplot.layout.OriginalLayout(this._treeSet);
var rootNode = this._layout.createNode(rootNodeId, rootSize, {x:0,y:0}, 'root'); var rootNode = this._layout.createNode(rootNodeId, rootSize, position, 'root');
this._treeSet.setRoot(rootNode); this._treeSet.setRoot(rootNode);
this._events = []; this._events = [];
}, },

View File

@ -112,6 +112,16 @@ mindplot.layout.RootedTreeSet = new Class({
return node._children; return node._children;
}, },
getRootNode: function(node) {
$assert(node, "node cannot be null");
var parent = this.getParent(node);
if ($defined(parent)) {
return this.getRootNode(parent);
}
return node;
},
getAncestors: function(node) { getAncestors: function(node) {
$assert(node, 'node cannot be null'); $assert(node, 'node cannot be null');
return this._getAncestors(this.getParent(node), []); return this._getAncestors(this.getParent(node), []);

View File

@ -22,7 +22,8 @@ mindplot.layout.SymmetricSorter = new Class({
}, },
predict : function(parent, graph, position) { predict : function(parent, graph, position) {
var direction = parent.getPosition().x > 0 ? 1 : -1; var rootNode = graph.getRootNode(parent);
var direction = parent.getPosition().x > rootNode.getPosition().x ? 1 : -1;
// No children... // No children...
var children = graph.getChildren(parent); var children = graph.getChildren(parent);
@ -112,8 +113,8 @@ mindplot.layout.SymmetricSorter = new Class({
ysum = ysum - heights[i].height; ysum = ysum - heights[i].height;
var parent = treeSet.getParent(treeSet.find(heights[i].id)); var parent = treeSet.getParent(treeSet.find(heights[i].id));
//TODO(gb): actually compare to branch's root node position var rootNode = treeSet.getRootNode(treeSet.find(heights[i].id));
var direction = parent.getPosition().x > 0 ? 1 : -1; var direction = parent.getPosition().x > rootNode.getPosition().x ? 1 : -1;
var yOffset = ysum + heights[i].height / 2; var yOffset = ysum + heights[i].height / 2;
var xOffset = direction * (heights[i].width/2 + node.getSize().width/2 + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING); var xOffset = direction * (heights[i].width/2 + node.getSize().width/2 + mindplot.layout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING);

View File

@ -182,13 +182,13 @@ mindplot.layout.TestSuite = new Class({
manager.layout(); manager.layout();
manager.plot("testBalanced13", {width:1000, height:400}); manager.plot("testBalanced13", {width:1000, height:400});
$assert(manager.find(1).getPosition().x > 0, "even order nodes must be at right of central topic"); $assert(manager.find(1).getPosition().x > manager.find(0).getPosition().x, "even order nodes must be at right of central topic");
$assert(manager.find(3).getPosition().x > 0, "even order nodes must be at right of central topic"); $assert(manager.find(3).getPosition().x > manager.find(0).getPosition().x, "even order nodes must be at right of central topic");
$assert(manager.find(5).getPosition().x > 0, "even order nodes must be at right of central topic"); $assert(manager.find(5).getPosition().x > manager.find(0).getPosition().x, "even order nodes must be at right of central topic");
$assert(manager.find(2).getPosition().x < 0, "odd order nodes must be at right of central topic"); $assert(manager.find(2).getPosition().x < manager.find(0).getPosition().x, "odd order nodes must be at right of central topic");
$assert(manager.find(4).getPosition().x < 0, "odd order nodes must be at right of central topic"); $assert(manager.find(4).getPosition().x < manager.find(0).getPosition().x, "odd order nodes must be at right of central topic");
$assert(manager.find(6).getPosition().x < 0, "odd order nodes must be at right of central topic"); $assert(manager.find(6).getPosition().x < manager.find(0).getPosition().x, "odd order nodes must be at right of central topic");
$assert(manager.find(7).getPosition().x > manager.find(3).getPosition().x, "children of 1st level even order nodes must be to the right"); $assert(manager.find(7).getPosition().x > manager.find(3).getPosition().x, "children of 1st level even order nodes must be to the right");
$assert(manager.find(8).getPosition().x > manager.find(7).getPosition().x, "children of 1st level even order nodes must be to the right"); $assert(manager.find(8).getPosition().x > manager.find(7).getPosition().x, "children of 1st level even order nodes must be to the right");
@ -580,7 +580,6 @@ mindplot.layout.TestSuite = new Class({
}, },
_plotPrediction: function(canvas, prediction) { _plotPrediction: function(canvas, prediction) {
console.log(prediction); //TODO(gb): Remove trace!!!
var position = prediction.position; var position = prediction.position;
var order = prediction.order; var order = prediction.order;
console.log("\t\tprediction {order:" + order + ", position: (" + position.x + "," + position.y + ")}"); console.log("\t\tprediction {order:" + order + ", position: (" + position.x + "," + position.y + ")}");