Adapt ArticleAutocomplete to new format

This commit is contained in:
Ludovic Stephan 2017-03-16 01:22:46 -03:00
parent 2ce96bce1b
commit 01295d464d

View file

@ -488,8 +488,6 @@ class ArticleAutocomplete {
constructor(article_manager) { constructor(article_manager) {
this.manager = article_manager; this.manager = article_manager;
this.matching = [];
this.active_categories = [];
this._$container = article_manager._$container ; this._$container = article_manager._$container ;
this._$input = $('#article_autocomplete'); this._$input = $('#article_autocomplete');
@ -523,31 +521,30 @@ class ArticleAutocomplete {
update(prefix, backspace) { update(prefix, backspace) {
this.resetMatch();
var article_list = this.manager.list ; var article_list = this.manager.list ;
var lower = prefix.toLowerCase() ; var lower = prefix.toLowerCase() ;
var that = this ; var that = this ;
this.matching = article_list.data['article']
.filter(function(article) { article_list.traverse(function(node) {
return article.name.toLowerCase() if (node.type === 'article' &&
.indexOf(lower) === 0 ; 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'] if (this.matching['article'].length == 1) {
.filter(function(category) {
return that.matching.find(function(article) {
return article.category === category ;
});
});
if (this.matching.length == 1) {
if (!backspace) { if (!backspace) {
this.manager.validate(this.matching[0]) ; this.manager.validate(this.matching['article'][0]) ;
this.showAll() ; this.showAll() ;
} else { } else {
this.manager.unset(); this.manager.unset();
this.updateDisplay(); this.updateDisplay();
} }
} else if (this.matching.length > 1) { } else if (this.matching['article'].length > 1) {
this.manager.unset(); this.manager.unset();
this.updateDisplay() ; this.updateDisplay() ;
if (!backspace) if (!backspace)
@ -556,26 +553,21 @@ class ArticleAutocomplete {
} }
updateDisplay() { updateDisplay() {
var article_list = this.manager.list ; var that = this;
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();
}
}
for (let category of article_list.data['category']) { this.manager.list.traverse(function(node) {
if (this.active_categories.indexOf(category) > -1) { if (that.matching[node.type].indexOf(node.content) != -1) {
this._$container.find('[data-category-id='+category.id+']').show(); that._$container.find('#'+node.type+'-'+node.content.id)
.show();
} else { } else {
this._$container.find('[data-category-id='+category.id+']').hide(); that._$container.find('#'+node.type+'-'+node.content.id)
.hide();
} }
} });
} }
updatePrefix() { updatePrefix() {
var lower = this.matching.map(function (article) { var lower = this.matching['article'].map(function (article) {
return article.name.toLowerCase() ; return article.name.toLowerCase() ;
}); });
@ -588,8 +580,16 @@ class ArticleAutocomplete {
} }
showAll() { showAll() {
this.matching = this.manager.list.data['article']; var that = this;
this.active_categories = this.manager.list.data['category']; this.resetMatch();
this.manager.list.traverse(function(node) {
that.matching[node.type].push(node.content);
});
this.updateDisplay(); this.updateDisplay();
} }
resetMatch() {
this.matching = {'article' : [],
'category': []};
}
} }