From a7de396aa36ee1cdf7212b485dde622547e49f56 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 17 Mar 2017 12:33:43 -0300 Subject: [PATCH] Better comparison control --- kfet/static/kfet/js/kfet.api.js | 52 +++++++++++++++++++++------------ kfet/static/kfet/js/kpsul.js | 1 - kfet/views.py | 1 + 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/kfet/static/kfet/js/kfet.api.js b/kfet/static/kfet/js/kfet.api.js index 8a3e0951..a80b7b9c 100644 --- a/kfet/static/kfet/js/kfet.api.js +++ b/kfet/static/kfet/js/kfet.api.js @@ -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; + } } diff --git a/kfet/static/kfet/js/kpsul.js b/kfet/static/kfet/js/kpsul.js index b2b4c5fa..1a6ca4b2 100644 --- a/kfet/static/kfet/js/kpsul.js +++ b/kfet/static/kfet/js/kpsul.js @@ -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); diff --git a/kfet/views.py b/kfet/views.py index 1c6042e4..1fd9322c 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -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)