diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 9d805fdc..730fab12 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -596,7 +596,7 @@ class ModelForest { get_or_create(data, direction) { var model = this.constructor.models[data.modelname]; - var existing = this.find(data.modelname, data.content.id); + var existing = this.find_node(data.modelname, data.content.id); if (existing) { return existing; } @@ -704,12 +704,40 @@ class ModelForest { return $container; } - traverse(callback) { + /** + * Find if node already exists in given tree + * @param {Models.TreeNode} + */ + find_node(modelname, id) { + var result = null; + function recurse(node) { - callback(node) ; + if (node.modelname === modelname && node.content.id === id) + result = node; for (let child of node.children) - callback(child); + recurse(child); + } + + for (let root of this.roots) + recurse(root); + + return result; + } + + /** + * Performs for each node (in a DFS order) the callback function + * on node.content and node.parent.content, if node has given modelname. + * @param {string} modelname + * @param {function} callback + */ + traverse(modelname, callback) { + function recurse(node) { + if (node.modelname === modelname) + callback(node.content, node.parent && node.parent.content || null) ; + + for (let child of node.children) + recurse(child); } for (let root of this.roots) @@ -718,20 +746,20 @@ class ModelForest { /** * Find instance in tree with given type and id - * @param {string} type + * @param {string} modelname * @param {number} id */ - find(type, id) { + find(modelname, id) { var result = null; - function callback(node) { - if (node.modelname === type && node.content.id == id) - result = node ; + + function callback(content) { + if (content.id == id) + result = content ; } - this.traverse(callback); - - return result ; + this.traverse(modelname, callback); + return result; } } diff --git a/kfet/static/kfet/js/kpsul.js b/kfet/static/kfet/js/kpsul.js index e6784f7e..af2e4f5f 100644 --- a/kfet/static/kfet/js/kpsul.js +++ b/kfet/static/kfet/js/kpsul.js @@ -456,7 +456,7 @@ class ArticleManager { } get_article(id) { - return this.list.find('article', id).content; + return this.list.find('article', id); } update_data(data) { @@ -498,7 +498,7 @@ class ArticleManager { var id = $(this).parent().attr('id').split('-')[1]; var article = that.list.find('article', id); if (article) - that.validate(article.content); + that.validate(article); }); this._$nb.on('keydown', function(e) { @@ -575,25 +575,23 @@ class ArticleAutocomplete { var lower = prefix.toLowerCase() ; var that = this ; - article_list.traverse(function(node) { - if (node.modelname === 'article' && - node.content.name.toLowerCase() - .startsWith(lower)) { - that.matching['article'].push(node.content); - if (that.matching['category'].indexOf(node.parent.content) == -1) - that.matching['category'].push(node.parent.content); + article_list.traverse('article', function(article, category) { + if (article.name.toLowerCase().startsWith(lower)) { + that.matching.push(article); + if (that.active_categories.indexOf(category) == -1) + that.active_categories.push(category); } }); - if (this.matching['article'].length == 1) { + if (this.matching.length == 1) { if (!backspace) { - this.manager.validate(this.matching['article'][0]) ; + this.manager.validate(this.matching[0]) ; this.showAll() ; } else { this.manager.unset(); this.updateDisplay(); } - } else if (this.matching['article'].length > 1) { + } else if (this.matching.length > 1) { this.manager.unset(); this.updateDisplay() ; if (!backspace) @@ -604,19 +602,23 @@ class ArticleAutocomplete { updateDisplay() { var that = this; - this.manager.list.traverse(function(node) { - if (that.matching[node.modelname].indexOf(node.content) != -1) { - that._$container.find('#'+node.modelname+'-'+node.content.id) - .show(); + this.manager.list.traverse('article', function(article, category) { + if (that.matching.indexOf(article) != -1) { + that._$container.find('#article-'+article.id).show(); } else { - that._$container.find('#'+node.modelname+'-'+node.content.id) - .hide(); + that._$container.find('#article-'+article.id).hide(); + } + + if (that.active_categories.indexOf(category) != -1) { + that._$container.find('#category-'+category.id).show(); + } else { + that._$container.find('#category-'+category.id).hide(); } }); } updatePrefix() { - var lower = this.matching['article'].map(function (article) { + var lower = this.matching.map(function (article) { return article.name.toLowerCase() ; }); @@ -631,14 +633,17 @@ class ArticleAutocomplete { showAll() { var that = this; this.resetMatch(); - this.manager.list.traverse(function(node) { - that.matching[node.modelname].push(node.content); + this.manager.list.traverse('article', function(article) { + that.matching.push(article); + }); + this.manager.list.traverse('category', function(category) { + that.active_categories.push(category); }); this.updateDisplay(); } resetMatch() { - this.matching = {'article' : [], - 'category': []}; + this.matching = []; + this.active_categories = []; } }