Merge branch 'Aufinal/refactor_articles' into Aufinal/refactor_history

This commit is contained in:
Ludovic Stephan 2017-03-17 12:34:48 -03:00
commit 53f89f53e0
3 changed files with 35 additions and 19 deletions

View file

@ -498,8 +498,8 @@ class ArticleCategory extends ModelObject {
* Comparison function between ArticleCategory model instances. * Comparison function between ArticleCategory model instances.
* @see {@link Models.ModelObject.compare|ModelObject.compare} * @see {@link Models.ModelObject.compare|ModelObject.compare}
*/ */
comparevalue() { static compare(a, b) {
return this.name ; return a.name.localeCompare(b.name) ;
} }
} }
@ -542,8 +542,8 @@ class Article extends ModelObject {
* Comparison function between Article model instances. * Comparison function between Article model instances.
* @see {@link Models.ModelObject.compare|ModelObject.compare} * @see {@link Models.ModelObject.compare|ModelObject.compare}
*/ */
comparevalue() { static compare(a, b) {
return this.name; return a.name.localeCompare(b.name);
} }
// Take care of 'price' type // Take care of 'price' type
@ -748,13 +748,6 @@ class TreeNode {
this.parent = null; this.parent = null;
this.children = []; this.children = [];
} }
static compare(a, b) {
var a_serial = a.content.comparevalue();
var b_serial = b.content.comparevalue();
return a_serial.localeCompare(b_serial)
}
} }
@ -771,6 +764,18 @@ class ModelForest {
*/ */
static get models() { return {}; } 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 * Creates empty instance and populates it with data if given
@ -799,6 +804,9 @@ class ModelForest {
var content = new this.constructor.models[data.type](data.content); var content = new this.constructor.models[data.type](data.content);
var node = new TreeNode(data.type, content); var node = new TreeNode(data.type, content);
if (data.child_sort)
node.child_sort = data.child_sort
if (direction <= 0) { if (direction <= 0) {
if (data.parent) { if (data.parent) {
var parent = this.get_or_create(data.parent, -1); var parent = this.get_or_create(data.parent, -1);
@ -855,8 +863,8 @@ class ModelForest {
var $rendered = node.content.display($(template), options); var $rendered = node.content.display($(template), options);
$container.append($rendered); $container.append($rendered);
//TODO: better sorting control //dirty
node.children.sort(TreeNode.compare); node.children.sort(this.constructor.compare.bind(null, this.constructor.models[node.child_sort]));
for (let child of node.children) { for (let child of node.children) {
var $child = this.render_element(child, templates, options); var $child = this.render_element(child, templates, options);
@ -888,8 +896,7 @@ class ModelForest {
* @param {Object} [options] Options for element render method * @param {Object} [options] Options for element render method
*/ */
display($container, templates, options) { 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) { for (let root of this.roots) {
$container.append(this.render_element(root, templates, options)); $container.append(this.render_element(root, templates, options));
} }
@ -910,9 +917,9 @@ class ModelForest {
} }
/** /**
* Find instance in data matching given properties. * Find instance in tree with given type and id
* @param {class} model * @param {string} type
* @param {Object} props Properties to match * @param {number} id
*/ */
find(type, id) { find(type, id) {
var result = null; var result = null;
@ -995,6 +1002,15 @@ class ArticleList extends APIModelForest {
static get url_model() { static get url_model() {
return Urls['kfet.kpsul.articles_data'](); return Urls['kfet.kpsul.articles_data']();
} }
/**
* Provides model to sort root objects
* {@see Models.ModelForest.constructor|ModelForest.constructor}
*/
constructor() {
super();
this.root_sort = ArticleCategory;
}
} }
/** /**

View file

@ -451,7 +451,6 @@ class ArticleManager {
this._$container.on('click', '.article', function() { this._$container.on('click', '.article', function() {
var id = $(this).parent().attr('id').split('-')[1]; var id = $(this).parent().attr('id').split('-')[1];
console.log(id);
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.content);

View file

@ -1492,6 +1492,7 @@ def kpsul_articles_data(request):
'id': article.category.id, 'id': article.category.id,
'name': article.category.name, 'name': article.category.name,
}, },
'child_sort': 'article',
} }
}) })
return JsonResponse(articlelist, safe=False) return JsonResponse(articlelist, safe=False)