Adapt add_to_container + small improvements
This commit is contained in:
parent
23d19545a7
commit
9ba13a81ee
1 changed files with 27 additions and 15 deletions
|
@ -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.<br>
|
||||
|
@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue