Sorter refactoring

This commit is contained in:
Gonzalo Bellver 2012-01-10 20:01:36 -03:00
parent 765162d2a5
commit d7817a6f2c
5 changed files with 70 additions and 70 deletions

View File

@ -0,0 +1,66 @@
/*
* 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.AbstractBasicSorter = new Class({
Extends: mindplot.nlayout.ChildrenSorterStrategy,
computeChildrenIdByHeights: function(treeSet, node) {
var result = {};
this._computeChildrenHeight(treeSet, node, result);
return result;
},
_computeChildrenHeight : function(treeSet, node, heightCache) {
var height = node.getSize().height + (mindplot.nlayout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2); // 2* Top and down padding;
var result;
var children = treeSet.getChildren(node);
if (children.length == 0) {
result = height;
} else {
var childrenHeight = 0;
children.forEach(function(child) {
childrenHeight += this._computeChildrenHeight(treeSet, child, heightCache);
}, this);
result = Math.max(height, childrenHeight);
}
if (heightCache) {
heightCache[node.getId()] = result;
}
return result;
},
_getSortedChildren:function(treeSet, node) {
var result = treeSet.getChildren(node);
result.sort(function(a, b) {
return a.getOrder() - b.getOrder()
});
return result;
},
verify:function(treeSet, node) {
// Check that all is consistent ...
var children = this._getSortedChildren(treeSet, node);
for (var i = 0; i < children.length; i++) {
$assert(children[i].getOrder() == i, "missing order elements");
}
}
});

View File

@ -1,5 +1,5 @@
mindplot.nlayout.BalancedSorter = new Class({
Extends: mindplot.nlayout.ChildrenSorterStrategy,
Extends: mindplot.nlayout.AbstractBasicSorter,
initialize: function() {
@ -91,23 +91,6 @@ mindplot.nlayout.BalancedSorter = new Class({
node.setOrder(0);
},
_getSortedChildren:function(treeSet, node) {
var result = treeSet.getChildren(node);
result.sort(function(a, b) {
return a.getOrder() - b.getOrder();
});
return result;
},
verify:function(treeSet, node) {
// Check that all is consistent ...
var children = this._getSortedChildren(treeSet, node);
for (var i = 0; i < children.length; i++) {
$assert(children[i].getOrder() == i, "missing order elements");
}
},
computeOffsets:function(treeSet, node) {
$assert(treeSet, "treeSet can no be null.");
$assert(node, "node can no be null.");

View File

@ -16,7 +16,7 @@
* limitations under the License.
*/
mindplot.nlayout.GridSorter = new Class({
Extends: mindplot.nlayout.SymmetricSorter,
Extends: mindplot.nlayout.AbstractBasicSorter,
computeOffsets: function(treeSet, node) {
$assert(treeSet, "treeSet can no be null.");

View File

@ -16,40 +16,11 @@
* limitations under the License.
*/
mindplot.nlayout.SymmetricSorter = new Class({
Extends: mindplot.nlayout.ChildrenSorterStrategy,
Extends: mindplot.nlayout.AbstractBasicSorter,
initialize:function() {
},
computeChildrenIdByHeights: function(treeSet, node) {
var result = {};
this._computeChildrenHeight(treeSet, node, result);
return result;
},
_computeChildrenHeight : function(treeSet, node, heightCache) {
var height = node.getSize().height + (mindplot.nlayout.SymmetricSorter.INTERNODE_VERTICAL_PADDING * 2); // 2* Top and down padding;
var result;
var children = treeSet.getChildren(node);
if (children.length == 0) {
result = height;
} else {
var childrenHeight = 0;
children.forEach(function(child) {
childrenHeight += this._computeChildrenHeight(treeSet, child, heightCache);
}, this);
result = Math.max(height, childrenHeight);
}
if (heightCache) {
heightCache[node.getId()] = result;
}
return result;
},
predict : function(parent, graph, position) {
// No children...
@ -107,23 +78,6 @@ mindplot.nlayout.SymmetricSorter = new Class({
node.setOrder(0);
},
_getSortedChildren:function(treeSet, node) {
var result = treeSet.getChildren(node);
result.sort(function(a, b) {
return a.getOrder() - b.getOrder()
});
return result;
},
verify:function(treeSet, node) {
// Check that all is consistent ...
var children = this._getSortedChildren(treeSet, node);
for (var i = 0; i < children.length; i++) {
$assert(children[i].getOrder() == i, "missing order elements");
}
},
computeOffsets:function(treeSet, node) {
$assert(treeSet, "treeSet can no be null.");
$assert(node, "node can no be null.");
@ -161,10 +115,6 @@ mindplot.nlayout.SymmetricSorter = new Class({
return result;
},
_getRootNode: function(treeSet) {
var roots = treeSet.getTreeRoots();
},
toString:function() {
return "Symmetric Sorter";
}

View File

@ -12,6 +12,7 @@
<script type='text/javascript' src='../../../main/javascript/nlayout/LayoutManager.js'></script>
<script type='text/javascript' src='../../../main/javascript/nlayout/ChildrenSorterStrategy.js'></script>
<script type='text/javascript' src='../../../main/javascript/nlayout/AbstractBasicSorter.js'></script>
<script type='text/javascript' src='../../../main/javascript/nlayout/SymmetricSorter.js'></script>
<script type='text/javascript' src='../../../main/javascript/nlayout/GridSorter.js'></script>
<script type='text/javascript' src='../../../main/javascript/nlayout/BalancedSorter.js'></script>