From 78c1de5c67d2063a5d00a2d6dcce55e0b0ca00b0 Mon Sep 17 00:00:00 2001 From: Gonzalo Bellver Date: Fri, 13 Jan 2012 16:26:37 -0300 Subject: [PATCH] Predict functions now support null position --- .../main/javascript/nlayout/BalancedSorter.js | 19 +++- .../main/javascript/nlayout/LayoutManager.js | 6 +- .../javascript/nlayout/SymmetricSorter.js | 4 +- .../src/test/javascript/static/TestSuite.js | 91 +++++++++++-------- .../src/test/javascript/static/layout.html | 7 -- 5 files changed, 77 insertions(+), 50 deletions(-) diff --git a/mindplot/src/main/javascript/nlayout/BalancedSorter.js b/mindplot/src/main/javascript/nlayout/BalancedSorter.js index 5d255367..6fd47012 100644 --- a/mindplot/src/main/javascript/nlayout/BalancedSorter.js +++ b/mindplot/src/main/javascript/nlayout/BalancedSorter.js @@ -52,12 +52,25 @@ mindplot.nlayout.BalancedSorter = new Class({ }, predict : function(parent, graph, position) { + + if (!position) { + var right = this._getChildrenForOrder(parent, graph, 0); + var left = this._getChildrenForOrder(parent, graph, 1); + } // Filter nodes on one side.. - var children = this._getChildrenForSide(parent, graph, position); + var order = position ? (position.x > 0 ? 0 : 1) : ((right.length - left.length) > 0 ? 1 : 0); + var children = this._getChildrenForOrder(parent, graph, order); + + // No children? + if (children.length == 0) { + return [0, {x:parent.getPosition().x + parent.getSize().width + mindplot.nlayout.BalancedSorter.INTERNODE_HORIZONTAL_PADDING * 2, y:parent.getPosition().y}]; + } + // Try to fit within ... var result = null; var last = children.getLast(); + position = position || {x: last.getPosition().x, y:last.getPosition().y + 1}; children.each(function(child, index) { var cpos = child.getPosition(); if (position.y > cpos.y) { @@ -165,6 +178,7 @@ mindplot.nlayout.BalancedSorter = new Class({ }, _getChildrenForSide: function(parent, graph, position) { + position = position || {x: parent.getPosition().x + 1, y:parent.getPosition().y + 1}; return graph.getChildren(parent).filter(function(child) { return position.x > 0 ? child.getPosition().x > 0 : child.getPosition().x < 0; }); @@ -184,7 +198,8 @@ mindplot.nlayout.BalancedSorter = new Class({ // All even numbered nodes should be "continuous" by themselves var factor = node.getOrder() % 2 == 0 ? 2 : 1; for (var i = 0; i < children.length; i++) { - $assert(children[i].getOrder() == (i*factor), "missing order elements"); + var order = i == 0 && factor == 1 ? 1 : (factor * i); + $assert(children[i].getOrder() == order, "Missing order elements. Missing order: " + (i*factor)); } }, diff --git a/mindplot/src/main/javascript/nlayout/LayoutManager.js b/mindplot/src/main/javascript/nlayout/LayoutManager.js index e1f236ee..4cee92dd 100644 --- a/mindplot/src/main/javascript/nlayout/LayoutManager.js +++ b/mindplot/src/main/javascript/nlayout/LayoutManager.js @@ -57,11 +57,15 @@ mindplot.nlayout.LayoutManager = new Class({ $assert($defined(order), "order can not be null"); this._layout.connectNode(parentId, childId, order); + + return this; }, disconnectNode: function(id) { $assert($defined(id), "id can not be null"); this._layout.disconnectNode(id); + + return this; }, addNode:function(id, size, position) { @@ -82,11 +86,11 @@ mindplot.nlayout.LayoutManager = new Class({ // Remove the all the branch ... this._treeSet.remove(id); + return this; }, predict: function(parentId, position) { $assert($defined(parentId), "parentId can not be null"); - $assert(position, "childId can not be null"); var parent = this._treeSet.find(parentId); var sorter = parent.getSorter(); diff --git a/mindplot/src/main/javascript/nlayout/SymmetricSorter.js b/mindplot/src/main/javascript/nlayout/SymmetricSorter.js index 40c533b4..ff811d79 100644 --- a/mindplot/src/main/javascript/nlayout/SymmetricSorter.js +++ b/mindplot/src/main/javascript/nlayout/SymmetricSorter.js @@ -22,11 +22,12 @@ mindplot.nlayout.SymmetricSorter = new Class({ }, predict : function(parent, graph, position) { + var direction = parent.getPosition().x > 0 ? 1 : -1; // No children... var children = graph.getChildren(parent); - var direction = parent.getPosition().x > 0 ? 1 : -1; if (children.length == 0) { + position = position || {x:parent.getPosition().x + direction, y:parent.getPosition().y}; var position = { x: parent.getPosition().x + direction * (parent.getSize().width + mindplot.nlayout.SymmetricSorter.INTERNODE_HORIZONTAL_PADDING), y:parent.getPosition().y @@ -37,6 +38,7 @@ mindplot.nlayout.SymmetricSorter = new Class({ // Try to fit within ... var result = null; var last = children.getLast(); + position = position || {x:last.getPosition().x + direction, y:last.getPosition().y + 1}; children.each(function(child, index) { var cpos = child.getPosition(); if (position.y > cpos.y) { diff --git a/mindplot/src/test/javascript/static/TestSuite.js b/mindplot/src/test/javascript/static/TestSuite.js index e3233dda..d9a3da3d 100644 --- a/mindplot/src/test/javascript/static/TestSuite.js +++ b/mindplot/src/test/javascript/static/TestSuite.js @@ -476,33 +476,37 @@ mindplot.nlayout.TestSuite = new Class({ manager.layout(); console.log("\tAdded as child of node 9 and dropped at (-280, 45):"); - var predict1 = manager.plot("testSymmetricPredict1", {width:1000, height:400}); - this._plotPrediction(predict1, manager.predict(9, {x:-280, y:45})); + var graph1 = manager.plot("testSymmetricPredict1", {width:1000, height:400}); + this._plotPrediction(graph1, manager.predict(9, {x:-280, y:45})); console.log("\tAdded as child of node 1 and dropped at (155, -90):"); - var predict2 = manager.plot("testSymmetricPredict2", {width:1000, height:400}); - this._plotPrediction(predict2, manager.predict(1, {x:-155, y:-90})); + this._plotPrediction(graph1, manager.predict(1, {x:-155, y:-90})); console.log("\tAdded as child of node 5 and dropped at (375, 15):"); - var predict3 = manager.plot("testSymmetricPredict3", {width:1000, height:400}); - this._plotPrediction(predict3, manager.predict(5, {x:375, y:15})); + var graph2 = manager.plot("testSymmetricPredict2", {width:1000, height:400}); + this._plotPrediction(graph2, manager.predict(5, {x:375, y:15})); console.log("\tAdded as child of node 5 and dropped at (375, 45):"); - var predict4 = manager.plot("testSymmetricPredict4", {width:1000, height:400}); - this._plotPrediction(predict4, manager.predict(5, {x:375, y:45})); + this._plotPrediction(graph2, manager.predict(5, {x:375, y:45})); console.log("\tAdded as child of node 5 and dropped at (375, 45):"); - var predict5 = manager.plot("testSymmetricPredict5", {width:1000, height:400}); - this._plotPrediction(predict5, manager.predict(5, {x:375, y:65})); - console.log("\tAdded as child of node 3 and dropped at (280, 45):"); - var predict6 = manager.plot("testSymmetricPredict6", {width:1000, height:400}); - this._plotPrediction(predict6, manager.predict(3, {x:280, y:45})); - console.log("\tAdded as child of node 3 and dropped at (255, 110):"); - var predict7 = manager.plot("testSymmetricPredict7", {width:1000, height:400}); - this._plotPrediction(predict7, manager.predict(3, {x:255, y:110})); - console.log("\tAdded as child of node 2 and dropped at (-260, 0):"); - var predict8 = manager.plot("testSymmetricPredict8", {width:1000, height:400}); - this._plotPrediction(predict8, manager.predict(2, {x:-260, y:0})); + this._plotPrediction(graph2, manager.predict(5, {x:375, y:65})); console.log("\tAdded as child of node 5 and dropped at (380, -30):"); - var predict9 = manager.plot("testSymmetricPredict9", {width:1000, height:400}); - this._plotPrediction(predict9, manager.predict(5, {x:380, y:-30})); + this._plotPrediction(graph2, manager.predict(5, {x:380, y:-30})); + + console.log("\tAdded as child of node 3 and dropped at (280, 45):"); + var graph3 = manager.plot("testSymmetricPredict3", {width:1000, height:400}); + this._plotPrediction(graph3, manager.predict(3, {x:280, y:45})); + console.log("\tAdded as child of node 3 and dropped at (255, 110):"); + this._plotPrediction(graph3, manager.predict(3, {x:255, y:110})); + console.log("\tAdded as child of node 2 and dropped at (-260, 0):"); + + var graph4 = manager.plot("testSymmetricPredict4", {width:1000, height:400}); + this._plotPrediction(graph4, manager.predict(2, {x:-260, y:0})); + + console.log("\tPredict nodes added with no position:"); + var graph5 = manager.plot("testSymmetricPredict5", {width:1000, height:400}); + this._plotPrediction(graph5, manager.predict(1, null)); + this._plotPrediction(graph5, manager.predict(2, null)); + this._plotPrediction(graph5, manager.predict(3, null)); + this._plotPrediction(graph5, manager.predict(10, null)); }, testBalancedPredict: function() { @@ -535,34 +539,43 @@ mindplot.nlayout.TestSuite = new Class({ manager.layout(); console.log("\tAdded as child of node 0 and dropped at (165, -70):"); - var predict1 = manager.plot("testBalancedPredict1", {width:1000, height:400}); - this._plotPrediction(predict1, manager.predict(0, {x:165, y:-70})); + var graph1 = manager.plot("testBalancedPredict1", {width:1000, height:400}); + this._plotPrediction(graph1, manager.predict(0, {x:165, y:-70})); console.log("\tAdded as child of node 0 and dropped at (165, -10):"); - var predict2 = manager.plot("testBalancedPredict2", {width:1000, height:400}); - this._plotPrediction(predict2, manager.predict(0, {x:165, y:-10})); + this._plotPrediction(graph1, manager.predict(0, {x:165, y:-10})); console.log("\tAdded as child of node 0 and dropped at (145, 15):"); - var predict3 = manager.plot("testBalancedPredict3", {width:1000, height:400}); - this._plotPrediction(predict3, manager.predict(0, {x:145, y:15})); + this._plotPrediction(graph1, manager.predict(0, {x:145, y:15})); console.log("\tAdded as child of node 0 and dropped at (145, 70):"); - var predict4 = manager.plot("testBalancedPredict4", {width:1000, height:400}); - this._plotPrediction(predict4, manager.predict(0, {x:145, y:70})); + this._plotPrediction(graph1, manager.predict(0, {x:145, y:70})); console.log("\tAdded as child of node 0 and dropped at (-145, -50):"); - var predict5 = manager.plot("testBalancedPredict5", {width:1000, height:400}); - this._plotPrediction(predict5, manager.predict(0, {x:-145, y:-50})); + var graph2 = manager.plot("testBalancedPredict2", {width:1000, height:400}); + this._plotPrediction(graph2, manager.predict(0, {x:-145, y:-50})); console.log("\tAdded as child of node 0 and dropped at (-145, -10):"); - var predict6 = manager.plot("testBalancedPredict6", {width:1000, height:400}); - this._plotPrediction(predict6, manager.predict(0, {x:-145, y:-10})); + this._plotPrediction(graph2, manager.predict(0, {x:-145, y:-10})); console.log("\tAdded as child of node 0 and dropped at (-145, 40):"); - var predict7 = manager.plot("testBalancedPredict7", {width:1000, height:400}); - this._plotPrediction(predict7, manager.predict(0, {x:-145, y:400})); + this._plotPrediction(graph2, manager.predict(0, {x:-145, y:400})); console.log("\tAdded as child of node 0 and dropped at (0, 40):"); - var predict8 = manager.plot("testBalancedPredict8", {width:1000, height:400}); - this._plotPrediction(predict8, manager.predict(0, {x:0, y:40})); + var graph3 = manager.plot("testBalancedPredict3", {width:1000, height:400}); + this._plotPrediction(graph3, manager.predict(0, {x:0, y:40})); console.log("\tAdded as child of node 0 and dropped at (0, 0):"); - var predict9 = manager.plot("testBalancedPredict9", {width:1000, height:400}); - this._plotPrediction(predict9, manager.predict(0, {x:0, y:0})); + this._plotPrediction(graph3, manager.predict(0, {x:0, y:0})); + + console.log("\tPredict nodes added with no position:"); + var graph4 = manager.plot("testBalancedPredict4", {width:1000, height:400}); + this._plotPrediction(graph4, manager.predict(0, null)); + + console.log("\tPredict nodes added with no position:"); + var graph5 = manager.plot("testBalancedPredict5", {width:1000, height:400}); + this._plotPrediction(graph5, manager.predict(0, null)); + + console.log("\tPredict nodes added only a root node:"); + manager.removeNode(1).removeNode(2).removeNode(3).removeNode(4).removeNode(5); + manager.layout(); + var graph6 = manager.plot("testBalancedPredict6", {width:1000, height:400}); + this._plotPrediction(graph6, manager.predict(0, null)); + this._plotPrediction(graph6, manager.predict(0, {x: 40, y: 100})); }, _plotPrediction: function(canvas, prediction) { diff --git a/mindplot/src/test/javascript/static/layout.html b/mindplot/src/test/javascript/static/layout.html index 6f92b3cc..d20840ae 100644 --- a/mindplot/src/test/javascript/static/layout.html +++ b/mindplot/src/test/javascript/static/layout.html @@ -94,10 +94,6 @@
-
-
-
-

testBalancedPredict:

@@ -106,9 +102,6 @@
-
-
-
\ No newline at end of file