From 01295d464d1ffd8073fe475ae57ef0a1e782758e Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 16 Mar 2017 01:22:46 -0300 Subject: [PATCH] Adapt ArticleAutocomplete to new format --- kfet/static/kfet/js/kpsul.js | 66 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/kfet/static/kfet/js/kpsul.js b/kfet/static/kfet/js/kpsul.js index 9d5515b2..e3621dee 100644 --- a/kfet/static/kfet/js/kpsul.js +++ b/kfet/static/kfet/js/kpsul.js @@ -488,8 +488,6 @@ class ArticleAutocomplete { constructor(article_manager) { this.manager = article_manager; - this.matching = []; - this.active_categories = []; this._$container = article_manager._$container ; this._$input = $('#article_autocomplete'); @@ -523,31 +521,30 @@ class ArticleAutocomplete { update(prefix, backspace) { + this.resetMatch(); var article_list = this.manager.list ; var lower = prefix.toLowerCase() ; var that = this ; - this.matching = article_list.data['article'] - .filter(function(article) { - return article.name.toLowerCase() - .indexOf(lower) === 0 ; + + article_list.traverse(function(node) { + if (node.type === '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); + } }); - this.active_categories = article_list.data['category'] - .filter(function(category) { - return that.matching.find(function(article) { - return article.category === category ; - }); - }); - - if (this.matching.length == 1) { + if (this.matching['article'].length == 1) { if (!backspace) { - this.manager.validate(this.matching[0]) ; + this.manager.validate(this.matching['article'][0]) ; this.showAll() ; } else { this.manager.unset(); this.updateDisplay(); } - } else if (this.matching.length > 1) { + } else if (this.matching['article'].length > 1) { this.manager.unset(); this.updateDisplay() ; if (!backspace) @@ -556,26 +553,21 @@ class ArticleAutocomplete { } updateDisplay() { - var article_list = this.manager.list ; - for (let article of article_list.data['article']) { - if (this.matching.indexOf(article) > -1) { - this._$container.find('[data-article-id='+article.id+']').show(); - } else { - this._$container.find('[data-article-id='+article.id+']').hide(); - } - } + var that = this; - for (let category of article_list.data['category']) { - if (this.active_categories.indexOf(category) > -1) { - this._$container.find('[data-category-id='+category.id+']').show(); - } else { - this._$container.find('[data-category-id='+category.id+']').hide(); + this.manager.list.traverse(function(node) { + if (that.matching[node.type].indexOf(node.content) != -1) { + that._$container.find('#'+node.type+'-'+node.content.id) + .show(); + } else { + that._$container.find('#'+node.type+'-'+node.content.id) + .hide(); } - } + }); } updatePrefix() { - var lower = this.matching.map(function (article) { + var lower = this.matching['article'].map(function (article) { return article.name.toLowerCase() ; }); @@ -588,8 +580,16 @@ class ArticleAutocomplete { } showAll() { - this.matching = this.manager.list.data['article']; - this.active_categories = this.manager.list.data['category']; + var that = this; + this.resetMatch(); + this.manager.list.traverse(function(node) { + that.matching[node.type].push(node.content); + }); this.updateDisplay(); } + + resetMatch() { + this.matching = {'article' : [], + 'category': []}; + } }