Added support for predict when dragging a symmetric sorted node

This commit is contained in:
Gonzalo Bellver 2012-02-02 12:15:59 -03:00
parent d632a079b1
commit a0b838a7a6
5 changed files with 76 additions and 10 deletions

View File

@ -58,6 +58,11 @@ mindplot.layout.AbstractBasicSorter = new Class({
return a.getOrder() - b.getOrder()
});
return result;
},
_getRelativeDirection: function(reference, position) {
var offset = position.x - reference.x;
return offset > 0 ? 1 : (offset < 0 ? -1 : 0);
}
});

View File

@ -42,11 +42,13 @@ mindplot.layout.BalancedSorter = new Class({
// If it is a dragged node...
if (node) {
$assert($defined(position), "position cannot be null for predict in dragging");
var nodeDirection = this._getRelativeDirection(rootNode.getPosition(), node.getPosition());
var positionDirection = this._getRelativeDirection(rootNode.getPosition(), position);
var siblings = graph.getSiblings(node);
if (siblings.length == 0 && nodeDirection == positionDirection) {
var sameParent = parent == graph.getParent(node);
if (siblings.length == 0 && nodeDirection == positionDirection && sameParent) {
return [node.getOrder(), node.getPosition()];
}
}
@ -183,7 +185,6 @@ mindplot.layout.BalancedSorter = new Class({
// Check that all is consistent ...
var children = this._getChildrenForOrder(node, treeSet, node.getOrder());
// All odd ordered nodes should be "continuous" by themselves
// All even numbered nodes should be "continuous" by themselves
var factor = node.getOrder() % 2 == 0 ? 2 : 1;
@ -221,13 +222,7 @@ mindplot.layout.BalancedSorter = new Class({
_getVerticalPadding: function() {
return mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING;
},
_getRelativeDirection: function(reference, position) {
var offset = position.x - reference.x;
return offset > 0 ? 1 : (offset < 0 ? -1 : 0);
}
});
mindplot.layout.BalancedSorter.INTERNODE_VERTICAL_PADDING = 5;

View File

@ -37,12 +37,26 @@ mindplot.layout.SymmetricSorter = new Class({
return [0, {x: xPos, y:position.y}];
}
// Regular node
var rootNode = graph.getRootNode(parent);
// If it is a dragged node...
if (node) {
$assert($defined(position), "position cannot be null for predict in dragging");
var nodeDirection = this._getRelativeDirection(rootNode.getPosition(), node.getPosition());
var positionDirection = this._getRelativeDirection(rootNode.getPosition(), position);
var siblings = graph.getSiblings(node);
var sameParent = parent == graph.getParent(node);
if (siblings.length == 0 && nodeDirection == positionDirection && sameParent) {
return [node.getOrder(), node.getPosition()];
}
}
// Regular node
var direction = parent.getPosition().x > rootNode.getPosition().x ? 1 : -1;
// No children...
var children = graph.getChildren(parent);
var children = graph.getChildren(parent).filter(function(child) { return child != node; });
if (children.length == 0) {
position = position || {x:parent.getPosition().x + direction, y:parent.getPosition().y};
var pos = {

View File

@ -133,6 +133,9 @@
<div id="testSymmetricPredict3"></div>
<div id="testSymmetricPredict4"></div>
<div id="testSymmetricPredict5"></div>
<h3>testSymmetricDragPredict:</h3>
<div id="testSymmetricDragPredict1"></div>
</div>
<div id="freeTest" style="display: none;">

View File

@ -23,6 +23,7 @@ mindplot.layout.SymmetricTestSuite = new Class({
this.testSymmetry();
this.testSymmetricPredict();
this.testSymmetricDragPredict();
},
testSymmetry: function() {
@ -213,5 +214,53 @@ mindplot.layout.SymmetricTestSuite = new Class({
$assert(prediction5d.position.y == manager.find(10).getPosition().y &&
prediction5d.position.x < manager.find(10).getPosition().x, "Prediction is incorrectly positioned");
$assert(prediction5d.order == 0, "Prediction order should be 0");
console.log("OK!\n\n");
},
testSymmetricDragPredict: function() {
console.log("testSymmetricDragPredict:");
var position = {x:0,y:0};
var manager = new mindplot.layout.LayoutManager(0, mindplot.layout.TestSuite.ROOT_NODE_SIZE);
manager.addNode(1, mindplot.layout.TestSuite.NODE_SIZE, position).connectNode(0, 1, 1);
manager.addNode(2, mindplot.layout.TestSuite.NODE_SIZE, position).connectNode(1, 2, 0);
manager.layout();
// Graph 1
var graph1 = manager.plot("testSymmetricDragPredict1", {width:1000, height:400});
var prediction1a = manager.predict(1, 2, {x:-250, y:-20});
this._plotPrediction(graph1, prediction1a);
$assert(prediction1a.position.x == manager.find(2).getPosition().x &&
prediction1a.position.y == manager.find(2).getPosition().y, "Prediction position should be the same as node 2");
$assert(prediction1a.order == manager.find(2).getOrder(), "Predicition order should be the same as node 2");
var prediction1b = manager.predict(1, 2, {x:-250, y:20});
this._plotPrediction(graph1, prediction1b);
$assert(prediction1b.position.x == manager.find(2).getPosition().x &&
prediction1b.position.y == manager.find(2).getPosition().y, "Prediction position should be the same as node 2");
$assert(prediction1b.order == manager.find(2).getOrder(), "Predicition order should be the same as node 2");
var prediction1c = manager.predict(0, 2, {x:-100, y:-20});
this._plotPrediction(graph1, prediction1c);
$assert(prediction1c.position.x == manager.find(1).getPosition().x &&
prediction1c.position.y < manager.find(1).getPosition().y, "Prediction is incorrectly positioned");
$assert(prediction1c.order == 1, "Prediction order should be 1");
var prediction1d = manager.predict(0, 2, {x:-100, y:20});
this._plotPrediction(graph1, prediction1d);
$assert(prediction1d.position.x == manager.find(1).getPosition().x &&
prediction1d.position.y > manager.find(1).getPosition().y, "Prediction is incorrectly positioned");
$assert(prediction1d.order == 3, "Prediction order should be 3");
var prediction1e = manager.predict(1, 2, {x:-250, y:0});
this._plotPrediction(graph1, prediction1e);
$assert(prediction1e.position.x == manager.find(2).getPosition().x &&
prediction1e.position.y == manager.find(2).getPosition().y, "Prediction position should be the same as node 2");
$assert(prediction1e.order == manager.find(2).getOrder(), "Predicition order should be the same as node 2");
console.log("OK!\n\n");
}
});