forked from DGNum/gestioCOF
Change traverse and find behavior
This commit is contained in:
parent
1761c5f1bd
commit
508e7ec23f
2 changed files with 68 additions and 35 deletions
|
@ -596,7 +596,7 @@ class ModelForest {
|
||||||
get_or_create(data, direction) {
|
get_or_create(data, direction) {
|
||||||
var model = this.constructor.models[data.modelname];
|
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) {
|
if (existing) {
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
@ -704,12 +704,40 @@ class ModelForest {
|
||||||
return $container;
|
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) {
|
function recurse(node) {
|
||||||
callback(node) ;
|
if (node.modelname === modelname && node.content.id === id)
|
||||||
|
result = node;
|
||||||
|
|
||||||
for (let child of node.children)
|
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)
|
for (let root of this.roots)
|
||||||
|
@ -718,20 +746,20 @@ class ModelForest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find instance in tree with given type and id
|
* Find instance in tree with given type and id
|
||||||
* @param {string} type
|
* @param {string} modelname
|
||||||
* @param {number} id
|
* @param {number} id
|
||||||
*/
|
*/
|
||||||
find(type, id) {
|
find(modelname, id) {
|
||||||
var result = null;
|
var result = null;
|
||||||
function callback(node) {
|
|
||||||
if (node.modelname === type && node.content.id == id)
|
function callback(content) {
|
||||||
result = node ;
|
if (content.id == id)
|
||||||
|
result = content ;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.traverse(callback);
|
this.traverse(modelname, callback);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -456,7 +456,7 @@ class ArticleManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
get_article(id) {
|
get_article(id) {
|
||||||
return this.list.find('article', id).content;
|
return this.list.find('article', id);
|
||||||
}
|
}
|
||||||
|
|
||||||
update_data(data) {
|
update_data(data) {
|
||||||
|
@ -498,7 +498,7 @@ class ArticleManager {
|
||||||
var id = $(this).parent().attr('id').split('-')[1];
|
var id = $(this).parent().attr('id').split('-')[1];
|
||||||
var article = that.list.find('article', id);
|
var article = that.list.find('article', id);
|
||||||
if (article)
|
if (article)
|
||||||
that.validate(article.content);
|
that.validate(article);
|
||||||
});
|
});
|
||||||
|
|
||||||
this._$nb.on('keydown', function(e) {
|
this._$nb.on('keydown', function(e) {
|
||||||
|
@ -575,25 +575,23 @@ class ArticleAutocomplete {
|
||||||
var lower = prefix.toLowerCase() ;
|
var lower = prefix.toLowerCase() ;
|
||||||
var that = this ;
|
var that = this ;
|
||||||
|
|
||||||
article_list.traverse(function(node) {
|
article_list.traverse('article', function(article, category) {
|
||||||
if (node.modelname === 'article' &&
|
if (article.name.toLowerCase().startsWith(lower)) {
|
||||||
node.content.name.toLowerCase()
|
that.matching.push(article);
|
||||||
.startsWith(lower)) {
|
if (that.active_categories.indexOf(category) == -1)
|
||||||
that.matching['article'].push(node.content);
|
that.active_categories.push(category);
|
||||||
if (that.matching['category'].indexOf(node.parent.content) == -1)
|
|
||||||
that.matching['category'].push(node.parent.content);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.matching['article'].length == 1) {
|
if (this.matching.length == 1) {
|
||||||
if (!backspace) {
|
if (!backspace) {
|
||||||
this.manager.validate(this.matching['article'][0]) ;
|
this.manager.validate(this.matching[0]) ;
|
||||||
this.showAll() ;
|
this.showAll() ;
|
||||||
} else {
|
} else {
|
||||||
this.manager.unset();
|
this.manager.unset();
|
||||||
this.updateDisplay();
|
this.updateDisplay();
|
||||||
}
|
}
|
||||||
} else if (this.matching['article'].length > 1) {
|
} else if (this.matching.length > 1) {
|
||||||
this.manager.unset();
|
this.manager.unset();
|
||||||
this.updateDisplay() ;
|
this.updateDisplay() ;
|
||||||
if (!backspace)
|
if (!backspace)
|
||||||
|
@ -604,19 +602,23 @@ class ArticleAutocomplete {
|
||||||
updateDisplay() {
|
updateDisplay() {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
this.manager.list.traverse(function(node) {
|
this.manager.list.traverse('article', function(article, category) {
|
||||||
if (that.matching[node.modelname].indexOf(node.content) != -1) {
|
if (that.matching.indexOf(article) != -1) {
|
||||||
that._$container.find('#'+node.modelname+'-'+node.content.id)
|
that._$container.find('#article-'+article.id).show();
|
||||||
.show();
|
|
||||||
} else {
|
} else {
|
||||||
that._$container.find('#'+node.modelname+'-'+node.content.id)
|
that._$container.find('#article-'+article.id).hide();
|
||||||
.hide();
|
}
|
||||||
|
|
||||||
|
if (that.active_categories.indexOf(category) != -1) {
|
||||||
|
that._$container.find('#category-'+category.id).show();
|
||||||
|
} else {
|
||||||
|
that._$container.find('#category-'+category.id).hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updatePrefix() {
|
updatePrefix() {
|
||||||
var lower = this.matching['article'].map(function (article) {
|
var lower = this.matching.map(function (article) {
|
||||||
return article.name.toLowerCase() ;
|
return article.name.toLowerCase() ;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -631,14 +633,17 @@ class ArticleAutocomplete {
|
||||||
showAll() {
|
showAll() {
|
||||||
var that = this;
|
var that = this;
|
||||||
this.resetMatch();
|
this.resetMatch();
|
||||||
this.manager.list.traverse(function(node) {
|
this.manager.list.traverse('article', function(article) {
|
||||||
that.matching[node.modelname].push(node.content);
|
that.matching.push(article);
|
||||||
|
});
|
||||||
|
this.manager.list.traverse('category', function(category) {
|
||||||
|
that.active_categories.push(category);
|
||||||
});
|
});
|
||||||
this.updateDisplay();
|
this.updateDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
resetMatch() {
|
resetMatch() {
|
||||||
this.matching = {'article' : [],
|
this.matching = [];
|
||||||
'category': []};
|
this.active_categories = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue