From 9ba13a81ee1ce6dc7c626ececc70586abf451ef2 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 6 Apr 2017 00:10:39 -0300 Subject: [PATCH] Adapt add_to_container + small improvements --- kfet/static/kfet/js/kfet.api.js | 42 +++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 0f6cd91b..889e2152 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -568,6 +568,19 @@ class ModelForest { this.from(datalist || []); } + /** + * Shortcut functions to get parent and children of a given node + */ + get_parent(node) { + var parent_name = this.constructor.structure[node.constructor.verbose_name].parent; + return node[parent_name]; + } + + get_children(node) { + var child_name = this.constructor.structure[node.constructor.verbose_name].children; + return node[child_name]; + } + /** * Fetches an object from the instance data, or creates it if * it does not exist yet.
@@ -642,7 +655,7 @@ class ModelForest { */ render_element(node, templates, options) { var modelname = node.constructor.verbose_name; - var struct = this.constructor.structure; + var struct_data = this.constructor.structure[modelname]; var template = templates[modelname]; var options = options || {} ; @@ -653,12 +666,11 @@ class ModelForest { var $rendered = node.display($(template), options); $container.append($rendered); - //dirty - var child_name = struct[modelname].children; + var children = this.get_children(node); - if (child_name) { - node[child_name].sort(struct[modelname].child_sort.compare); - for (let child of node[child_name]) { + if (children) { + children.sort(struct_data.child_sort); + for (let child of children) { var $child = this.render_element(child, templates, options); $container.append($child); } @@ -667,14 +679,14 @@ class ModelForest { return $container; } - //TODO adapt add_to_container($container, node, templates, options) { - var existing = node.parent ; + var struct = this.constructor.structure; + var existing = this.get_parent(node) ; var first_missing = node; while (existing && !($container.find('#'+existing.modelname+'-'+existing.id))) { - first_missing = existing ; - existing = existing.parent; + first_missing = existing; + existing = this.get_parent(existing); } var $to_insert = render_element(first_missing, templates, options); @@ -705,15 +717,15 @@ class ModelForest { * @param {function} callback */ traverse(modelname, callback) { - var struct = this.constructor.structure; + var that = this; function recurse(node) { if (node.constructor.verbose_name === modelname) { callback(node); } - var child_name = struct[node.constructor.verbose_name].children; - if (child_name) { - for (let child of node[child_name]) + var children = that.get_children(node); + if (children) { + for (let child of children) recurse(child); } } @@ -792,7 +804,7 @@ class ArticleList extends APIModelForest { 'category': { 'model': ArticleCategory, 'children': 'articles', - 'child_sort': Article, + 'child_sort': Article.compare, }, };