From 2ce96bce1be54ea057283cb5015c71114ea0302e Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 16 Mar 2017 01:21:50 -0300 Subject: [PATCH] Add traverse function to ModelTree --- kfet/static/kfet/js/kfet.api.js | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 1339f284..409c61a5 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -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; - } - - return null; + var result = null; + function callback(node) { + if (node.type === type && node.content.id == id) + result = node ; } - for (let root of this.roots) { - if (result = recurse(root)) - return result; - } + this.traverse(callback); + + return result ; - return null ; } }