WIP: Aureplop/kpsul js refactor #501
3 changed files with 35 additions and 19 deletions
|
@ -498,8 +498,8 @@ class ArticleCategory extends ModelObject {
|
|||
* Comparison function between ArticleCategory model instances.
|
||||
* @see {@link Models.ModelObject.compare|ModelObject.compare}
|
||||
*/
|
||||
comparevalue() {
|
||||
return this.name ;
|
||||
static compare(a, b) {
|
||||
return a.name.localeCompare(b.name) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -542,8 +542,8 @@ class Article extends ModelObject {
|
|||
* Comparison function between Article model instances.
|
||||
* @see {@link Models.ModelObject.compare|ModelObject.compare}
|
||||
*/
|
||||
comparevalue() {
|
||||
return this.name;
|
||||
static compare(a, b) {
|
||||
return a.name.localeCompare(b.name);
|
||||
}
|
||||
|
||||
// Take care of 'price' type
|
||||
|
@ -565,13 +565,6 @@ class TreeNode {
|
|||
this.parent = null;
|
||||
this.children = [];
|
||||
}
|
||||
|
||||
static compare(a, b) {
|
||||
var a_serial = a.content.comparevalue();
|
||||
var b_serial = b.content.comparevalue();
|
||||
|
||||
return a_serial.localeCompare(b_serial)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -588,6 +581,18 @@ class ModelForest {
|
|||
*/
|
||||
static get models() { return {}; }
|
||||
|
||||
/**
|
||||
* Comparison function for nodes
|
||||
* @abstract
|
||||
* @param {class} model Model to use for comparison
|
||||
* @param {Models.TreeNode} a
|
||||
* @param {Models.TreeNode} b
|
||||
* @see {@link Models.ModelObject.compare|ModelObject.compare}
|
||||
*/
|
||||
static compare(model, a, b) {
|
||||
return model.compare(a.content, b.content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates empty instance and populates it with data if given
|
||||
|
@ -616,6 +621,9 @@ class ModelForest {
|
|||
var content = new this.constructor.models[data.type](data.content);
|
||||
var node = new TreeNode(data.type, content);
|
||||
|
||||
if (data.child_sort)
|
||||
node.child_sort = data.child_sort
|
||||
|
||||
if (direction <= 0) {
|
||||
if (data.parent) {
|
||||
var parent = this.get_or_create(data.parent, -1);
|
||||
|
@ -672,8 +680,8 @@ class ModelForest {
|
|||
var $rendered = node.content.display($(template), options);
|
||||
$container.append($rendered);
|
||||
|
||||
//TODO: better sorting control
|
||||
node.children.sort(TreeNode.compare);
|
||||
//dirty
|
||||
node.children.sort(this.constructor.compare.bind(null, this.constructor.models[node.child_sort]));
|
||||
|
||||
for (let child of node.children) {
|
||||
var $child = this.render_element(child, templates, options);
|
||||
|
@ -705,8 +713,7 @@ class ModelForest {
|
|||
* @param {Object} [options] Options for element render method
|
||||
*/
|
||||
display($container, templates, options) {
|
||||
this.roots.sort(TreeNode.compare);
|
||||
|
||||
this.roots.sort(this.constructor.compare.bind(null, this.root_sort));
|
||||
for (let root of this.roots) {
|
||||
$container.append(this.render_element(root, templates, options));
|
||||
}
|
||||
|
@ -727,9 +734,9 @@ class ModelForest {
|
|||
}
|
||||
|
||||
/**
|
||||
* Find instance in data matching given properties.
|
||||
* @param {class} model
|
||||
* @param {Object} props Properties to match
|
||||
* Find instance in tree with given type and id
|
||||
* @param {string} type
|
||||
* @param {number} id
|
||||
*/
|
||||
find(type, id) {
|
||||
var result = null;
|
||||
|
@ -813,6 +820,15 @@ class ArticleList extends APIModelForest {
|
|||
static get url_model() {
|
||||
return Urls['kfet.kpsul.articles_data']();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides model to sort root objects
|
||||
* {@see Models.ModelForest.constructor|ModelForest.constructor}
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
this.root_sort = ArticleCategory;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -451,7 +451,6 @@ class ArticleManager {
|
|||
|
||||
this._$container.on('click', '.article', function() {
|
||||
var id = $(this).parent().attr('id').split('-')[1];
|
||||
console.log(id);
|
||||
var article = that.list.find('article', id);
|
||||
if (article)
|
||||
that.validate(article.content);
|
||||
|
|
|
@ -1362,6 +1362,7 @@ def kpsul_articles_data(request):
|
|||
'id': article.category.id,
|
||||
'name': article.category.name,
|
||||
},
|
||||
'child_sort': 'article',
|
||||
}
|
||||
})
|
||||
return JsonResponse(articlelist, safe=False)
|
||||
|
|
Loading…
Reference in a new issue