Merge branch 'Aufinal/clean_modelforest' into 'aureplop/kpsul_js_refactor'

Change ModelForest inner structure
- Intermediary model for nodes removed
- More descriptive ModelForest structure in constructor.structure, child_sort not defined in Python function anymore.
- More intuitive access to parent and children of nodes (e.g. article.category, category.articles...)

See merge request !215
This commit is contained in:
Aurélien Delobelle 2017-04-09 21:28:16 +02:00
commit 7fc07ac603
3 changed files with 112 additions and 112 deletions

View file

@ -450,22 +450,28 @@ class ArticleCategory extends ModelObject {
/** /**
* Properties associated to a category * Properties associated to a category
* @default <tt>['id', 'name']</tt> * @default <tt>['id', 'name', 'has_addcost', 'article']</tt>
* @see {@link Models.ModelObject.props|ModelObject.props} * @see {@link Models.ModelObject.props|ModelObject.props}
*/ */
static get props() { static get props() {
return ['id', 'name', 'has_addcost']; return ['id', 'name', 'has_addcost', 'articles'];
} }
/** /**
* Default values for ArticleCategory model instances. * Default values for ArticleCategory model instances.
* @default <tt>{ 'id': 0, 'name': '' }</tt> * @default <tt>{ 'id': 0, 'name': '', 'has_addcost': true, 'articles': [] }</tt>
* @see {@link Models.ModelObject.default_data|ModelObject.default_data} * @see {@link Models.ModelObject.default_data|ModelObject.default_data}
*/ */
static get default_data() { static get default_data() {
return {'id': 0, 'name': '', 'has_addcost': true}; return {'id': 0, 'name': '', 'has_addcost': true, 'articles': []};
} }
/**
* Verbose name for ArticleCategory model
* @default <tt>'category'</tt>
*/
static get verbose_name() { return 'category'; }
/** /**
* @default {@link Formatters.ArticleCategoryFormatter} * @default {@link Formatters.ArticleCategoryFormatter}
*/ */
@ -490,7 +496,7 @@ class ArticleCategory extends ModelObject {
class Article extends ModelObject { class Article extends ModelObject {
/** /**
* Properties associated to an article * Properties associated to an article
* @default <tt>['id', 'name']</tt> * @default <tt>['id', 'name', 'price', 'stock', 'category']</tt>
* @see {@link Models.ModelObject.props|ModelObject.props} * @see {@link Models.ModelObject.props|ModelObject.props}
*/ */
static get props() { static get props() {
@ -510,6 +516,12 @@ class Article extends ModelObject {
}; };
} }
/**
* Verbose name for Article model
* @default <tt>'article'</tt>
*/
static get verbose_name() { return 'article'; }
/** /**
* @default {@link Formatters.ArticleFormatter} * @default {@link Formatters.ArticleFormatter}
*/ */
@ -535,22 +547,6 @@ class Article extends ModelObject {
} }
} }
/**
* Node for ModelForest object
* @memberof Models
*/
class TreeNode {
constructor(type, content) {
this.modelname = type;
this.content = content;
this.parent = null;
this.children = [];
}
}
/** /**
* Simple {@link Models.ModelObject} forest. * Simple {@link Models.ModelObject} forest.
* @memberof Models * @memberof Models
@ -558,24 +554,11 @@ class TreeNode {
class ModelForest { class ModelForest {
/** /**
* Dictionary associating types to classes * Abstract structure of the forest
* @abstract * @abstract
* @type {Object} * @type {Object}
*/ */
static get models() { return {}; } static get structure() { 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
@ -585,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>
@ -594,34 +590,38 @@ class ModelForest {
* @param {number} direction * @param {number} direction
*/ */
get_or_create(data, direction) { get_or_create(data, direction) {
var model = this.constructor.models[data.modelname]; var struct_data = this.constructor.structure[data.modelname];
var model = struct_data.model;
var existing = this.find_node(data.modelname, data.content.id); var existing = this.find(data.modelname, data.content.id);
if (existing) { if (existing) {
return existing; return existing;
} }
var content = new this.constructor.models[data.modelname](data.content); var node = new model(data.content);
var node = new TreeNode(data.modelname, content);
if (data.child_sort)
node.child_sort = data.child_sort
if (direction <= 0) { if (direction <= 0) {
if (data.parent) { var parent_name = struct_data.parent;
var parent = this.get_or_create(data.parent, -1); var parent_data = data.parent;
node.parent = parent; var parent_struct = this.constructor.structure[parent_name];
parent.children.push(node); if (parent_data) {
var parent_node = this.get_or_create(parent_data, -1);
node[parent_name] = parent_node;
parent_node[parent_struct.children].push(node);
} else { } else {
this.roots.push(node); this.roots.push(node);
} }
} }
if (direction >= 0 && data.children) { if (direction >= 0) {
for (let child_data of data.children) { var child_name = struct_data.children;
var child = this.get_or_create(child_data, 1); var child_struct = this.constructor.structure[child_name];
child.parent = node; if (data.children && data.children.length) {
node.children.push(child); for (let child_data of data.children) {
var child = this.get_or_create(child_data, 1);
child[child_struct.parent] = node;
node[child_name].push(child);
}
} }
} }
@ -654,33 +654,43 @@ class ModelForest {
* @param {Object} [options] Options for element render method * @param {Object} [options] Options for element render method
*/ */
render_element(node, templates, options) { render_element(node, templates, options) {
var template = templates[node.modelname]; var modelname = node.constructor.verbose_name;
var struct_data = this.constructor.structure[modelname];
var template = templates[modelname];
var options = options || {} ; var options = options || {} ;
var $container = $('<div></div>'); var $container = $('<div></div>');
$container.attr('id', node.modelname+'-'+node.content.id); $container.attr('id', modelname+'-'+node.id);
var $rendered = node.content.display($(template), options); var $rendered = node.display($(template), options);
$container.append($rendered); $container.append($rendered);
//dirty var children = this.get_children(node);
node.children.sort(this.constructor.compare.bind(null, this.constructor.models[node.child_sort]));
for (let child of node.children) { if (children && children.length) {
var $child = this.render_element(child, templates, options); if (struct_data.child_sort)
$container.append($child); children.sort(struct_data.child_sort);
else
children.sort(children[0].constructor.compare);
for (let child of children) {
var $child = this.render_element(child, templates, options);
$container.append($child);
}
} }
return $container; return $container;
} }
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);
@ -696,7 +706,11 @@ 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(this.constructor.compare.bind(null, this.root_sort)); if (this.constructor.root_sort)
this.roots.sort(this.constructor.root_sort);
else
this.roots.sort(this.roots[0].constructor.compare);
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));
} }
@ -704,27 +718,6 @@ class ModelForest {
return $container; return $container;
} }
/**
* Find if node already exists in given tree
* @param {Models.TreeNode}
*/
find_node(modelname, id) {
var result = null;
function recurse(node) {
if (node.modelname === modelname && node.content.id === id)
result = node;
for (let child of node.children)
recurse(child);
}
for (let root of this.roots)
recurse(root);
return result;
}
/** /**
* Performs for each node (in a DFS order) the callback function * Performs for each node (in a DFS order) the callback function
* on node.content and node.parent.content, if node has given modelname. * on node.content and node.parent.content, if node has given modelname.
@ -732,19 +725,27 @@ class ModelForest {
* @param {function} callback * @param {function} callback
*/ */
traverse(modelname, callback) { traverse(modelname, callback) {
var that = this;
function recurse(node) { function recurse(node) {
if (node.modelname === modelname) { if (node.constructor.verbose_name === modelname) {
var parent = node.parent && node.parent.content || null; if (callback(node)) {
var children = node.children.map( (child) => child.content); return true;
callback(node.content, children, parent); }
} }
for (let child of node.children) var children = that.get_children(node);
recurse(child); if (children) {
for (let child of children)
if (recurse(child))
return true;
}
return false;
} }
for (let root of this.roots) for (let root of this.roots)
recurse(root); if (recurse(root))
return ;
} }
/** /**
@ -755,9 +756,11 @@ class ModelForest {
find(modelname, id) { find(modelname, id) {
var result = null; var result = null;
function callback(content) { function callback(node) {
if (content.id == id) if (node.id == id) {
result = content ; result = node ;
return true;
}
} }
this.traverse(modelname, callback); this.traverse(modelname, callback);
@ -807,12 +810,19 @@ class ArticleList extends APIModelForest {
/** /**
* Default structure for ArticleList instances * Default structure for ArticleList instances
* @abstract * @abstract
* @default <tt>{'article': Article,
'category': ArticleCategory}</tt>
*/ */
static get models() { static get structure() {
return {'article': Article, return {
'category': ArticleCategory}; 'article': {
'model': Article,
'parent': 'category',
},
'category': {
'model': ArticleCategory,
'children': 'articles',
},
};
} }
/** /**
@ -824,15 +834,6 @@ 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

@ -599,9 +599,9 @@ class ArticleAutocomplete {
updateDisplay() { updateDisplay() {
var that = this; var that = this;
this.manager.list.traverse('category', function(category, articles) { this.manager.list.traverse('category', function(category) {
var is_active = false; var is_active = false;
for (let article of articles) { for (let article of category.articles) {
if (that.matching.indexOf(article) != -1) { if (that.matching.indexOf(article) != -1) {
is_active = true; is_active = true;
that._$container.find('#article-'+article.id).show(); that._$container.find('#article-'+article.id).show();

View file

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