forked from DGNum/gestioCOF
Add traverse function to ModelTree
This commit is contained in:
parent
770c185bd0
commit
2ce96bce1b
1 changed files with 23 additions and 20 deletions
|
@ -622,7 +622,7 @@ class ModelForest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (direction >= 0) {
|
if (direction >= 0 && data.children) {
|
||||||
for (let child_data of data.children) {
|
for (let child_data of data.children) {
|
||||||
var child = this.get_or_create(child_data, 1);
|
var child = this.get_or_create(child_data, 1);
|
||||||
child.parent = node;
|
child.parent = node;
|
||||||
|
@ -669,7 +669,7 @@ class ModelForest {
|
||||||
$container.append($rendered);
|
$container.append($rendered);
|
||||||
|
|
||||||
//TODO: better sorting control
|
//TODO: better sorting control
|
||||||
node.children.sort(ModelObject.compare);
|
node.children.sort(TreeNode.compare);
|
||||||
|
|
||||||
for (let child of node.children) {
|
for (let child of node.children) {
|
||||||
var $child = this.render_element(child, templates, options);
|
var $child = this.render_element(child, templates, options);
|
||||||
|
@ -701,40 +701,43 @@ class ModelForest {
|
||||||
* @param {Object} [options] Options for element render method
|
* @param {Object} [options] Options for element render method
|
||||||
*/
|
*/
|
||||||
display($container, templates, options) {
|
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));
|
$container.append(this.render_element(root, templates, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $container;
|
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.
|
* Find instance in data matching given properties.
|
||||||
* @param {class} model
|
* @param {class} model
|
||||||
* @param {Object} props Properties to match
|
* @param {Object} props Properties to match
|
||||||
*/
|
*/
|
||||||
find(type, id) {
|
find(type, id) {
|
||||||
|
var result = null;
|
||||||
function recurse(node) {
|
function callback(node) {
|
||||||
if (node.type === type && node.data.id === id)
|
if (node.type === type && node.content.id == id)
|
||||||
return node ;
|
result = node ;
|
||||||
|
|
||||||
for (let child of node.children) {
|
|
||||||
if (result = recurse(node))
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let root of this.roots) {
|
this.traverse(callback);
|
||||||
if (result = recurse(root))
|
|
||||||
return result;
|
return result ;
|
||||||
}
|
|
||||||
|
|
||||||
return null ;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue