Add traverse function to ModelTree

This commit is contained in:
Ludovic Stephan 2017-03-16 01:21:50 -03:00
parent 770c185bd0
commit 2ce96bce1b

View file

@ -622,7 +622,7 @@ class ModelForest {
}
}
if (direction >= 0) {
if (direction >= 0 && data.children) {
for (let child_data of data.children) {
var child = this.get_or_create(child_data, 1);
child.parent = node;
@ -669,7 +669,7 @@ class ModelForest {
$container.append($rendered);
//TODO: better sorting control
node.children.sort(ModelObject.compare);
node.children.sort(TreeNode.compare);
for (let child of node.children) {
var $child = this.render_element(child, templates, options);
@ -701,40 +701,43 @@ class ModelForest {
* @param {Object} [options] Options for element render method
*/
display($container, templates, options) {
this.roots.sort(ModelObject.compare);
this.roots.sort(TreeNode.compare);
for (let root of roots) {
for (let root of this.roots) {
$container.append(this.render_element(root, templates, options));
}
return $container;
}
traverse(callback) {
function recurse(node) {
callback(node) ;
for (let child of node.children)
callback(child);
}
for (let root of this.roots)
recurse(root);
}
/**
* Find instance in data matching given properties.
* @param {class} model
* @param {Object} props Properties to match
*/
find(type, id) {
function recurse(node) {
if (node.type === type && node.data.id === id)
return node ;
for (let child of node.children) {
if (result = recurse(node))
return result;
var result = null;
function callback(node) {
if (node.type === type && node.content.id == id)
result = node ;
}
return null;
}
this.traverse(callback);
for (let root of this.roots) {
if (result = recurse(root))
return result;
}
return result ;
return null ;
}
}