WIP: Aureplop/kpsul js refactor #501
1 changed files with 27 additions and 15 deletions
|
@ -568,6 +568,19 @@ class ModelForest {
|
||||||
this.from(datalist || []);
|
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
|
* Fetches an object from the instance data, or creates it if
|
||||||
* it does not exist yet.<br>
|
* it does not exist yet.<br>
|
||||||
|
@ -642,7 +655,7 @@ class ModelForest {
|
||||||
*/
|
*/
|
||||||
render_element(node, templates, options) {
|
render_element(node, templates, options) {
|
||||||
var modelname = node.constructor.verbose_name;
|
var modelname = node.constructor.verbose_name;
|
||||||
var struct = this.constructor.structure;
|
var struct_data = this.constructor.structure[modelname];
|
||||||
|
|
||||||
var template = templates[modelname];
|
var template = templates[modelname];
|
||||||
var options = options || {} ;
|
var options = options || {} ;
|
||||||
|
@ -653,12 +666,11 @@ class ModelForest {
|
||||||
var $rendered = node.display($(template), options);
|
var $rendered = node.display($(template), options);
|
||||||
$container.append($rendered);
|
$container.append($rendered);
|
||||||
|
|
||||||
//dirty
|
var children = this.get_children(node);
|
||||||
var child_name = struct[modelname].children;
|
|
||||||
|
|
||||||
if (child_name) {
|
if (children) {
|
||||||
node[child_name].sort(struct[modelname].child_sort.compare);
|
children.sort(struct_data.child_sort);
|
||||||
for (let child of node[child_name]) {
|
for (let child of children) {
|
||||||
var $child = this.render_element(child, templates, options);
|
var $child = this.render_element(child, templates, options);
|
||||||
$container.append($child);
|
$container.append($child);
|
||||||
}
|
}
|
||||||
|
@ -667,14 +679,14 @@ class ModelForest {
|
||||||
return $container;
|
return $container;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO adapt
|
|
||||||
add_to_container($container, node, templates, options) {
|
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;
|
var first_missing = node;
|
||||||
|
|
||||||
while (existing && !($container.find('#'+existing.modelname+'-'+existing.id))) {
|
while (existing && !($container.find('#'+existing.modelname+'-'+existing.id))) {
|
||||||
first_missing = existing ;
|
first_missing = existing;
|
||||||
existing = existing.parent;
|
existing = this.get_parent(existing);
|
||||||
}
|
}
|
||||||
|
|
||||||
var $to_insert = render_element(first_missing, templates, options);
|
var $to_insert = render_element(first_missing, templates, options);
|
||||||
|
@ -705,15 +717,15 @@ class ModelForest {
|
||||||
* @param {function} callback
|
* @param {function} callback
|
||||||
*/
|
*/
|
||||||
traverse(modelname, callback) {
|
traverse(modelname, callback) {
|
||||||
var struct = this.constructor.structure;
|
var that = this;
|
||||||
function recurse(node) {
|
function recurse(node) {
|
||||||
if (node.constructor.verbose_name === modelname) {
|
if (node.constructor.verbose_name === modelname) {
|
||||||
callback(node);
|
callback(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
var child_name = struct[node.constructor.verbose_name].children;
|
var children = that.get_children(node);
|
||||||
if (child_name) {
|
if (children) {
|
||||||
for (let child of node[child_name])
|
for (let child of children)
|
||||||
recurse(child);
|
recurse(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -792,7 +804,7 @@ class ArticleList extends APIModelForest {
|
||||||
'category': {
|
'category': {
|
||||||
'model': ArticleCategory,
|
'model': ArticleCategory,
|
||||||
'children': 'articles',
|
'children': 'articles',
|
||||||
'child_sort': Article,
|
'child_sort': Article.compare,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue