Merge branch 'Aufinal/refactor_articles' into Aufinal/refactor_history

This commit is contained in:
Ludovic Stephan 2017-03-20 00:30:40 -03:00
commit fe965875f7
5 changed files with 31 additions and 25 deletions

View file

@ -304,6 +304,10 @@ input[type=number]::-webkit-outer-spin-button {
font-size:14px; font-size:14px;
} }
#articles_data .article[data_stock="low"] {
background:rgba(236,100,0,0.3);
}
#articles_data span { #articles_data span {
height:25px; height:25px;
line-height:25px; line-height:25px;

View file

@ -852,7 +852,7 @@ class Transfer extends Operation {
class TreeNode { class TreeNode {
constructor(type, content) { constructor(type, content) {
this.type = type; this.modelname = type;
this.content = content; this.content = content;
this.parent = null; this.parent = null;
this.children = []; this.children = [];
@ -903,15 +903,15 @@ class ModelForest {
* @param {number} direction * @param {number} direction
*/ */
get_or_create(data, direction) { get_or_create(data, direction) {
var model = this.constructor.models[data.type]; var model = this.constructor.models[data.modelname];
var existing = this.find(data.type, 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.type](data.content); var content = new this.constructor.models[data.modelname](data.content);
var node = new TreeNode(data.type, content); var node = new TreeNode(data.modelname, content);
if (data.child_sort) if (data.child_sort)
node.child_sort = this.constructor.models[data.child_sort]; node.child_sort = this.constructor.models[data.child_sort];
@ -965,11 +965,11 @@ 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.type]; var template = templates[node.modelname];
var options = options || {} ; var options = options || {} ;
var $container = $('<div></div>'); var $container = $('<div></div>');
$container.attr('id', node.type+'-'+node.content.id); $container.attr('id', node.modelname+'-'+node.content.id);
var $rendered = node.content.display($(template), options); var $rendered = node.content.display($(template), options);
$container.append($rendered); $container.append($rendered);
@ -997,14 +997,14 @@ class ModelForest {
var existing = node.parent ; var existing = node.parent ;
var first_missing = node; var first_missing = node;
while (existing && !($container.find('#'+existing.type+'-'+existing.content.id).length)) { while (existing && !($container.find('#'+existing.modelname+'-'+existing.id).length)) {
first_missing = existing ; first_missing = existing ;
existing = existing.parent; existing = existing.parent;
} }
var $to_insert = this.render_element(first_missing, templates, options); var $to_insert = this.render_element(first_missing, templates, options);
if (existing) { if (existing) {
$container.find('#'+existing.type+'-'+existing.content.id+'>:first-child').after($to_insert); $container.find('#'+existing.modelname+'-'+existing.content.id+'>:first-child').after($to_insert);
} else { } else {
$container.prepend($to_insert); $container.prepend($to_insert);
} }
@ -1045,7 +1045,7 @@ class ModelForest {
find(type, id) { find(type, id) {
var result = null; var result = null;
function callback(node) { function callback(node) {
if (node.type === type && node.content.id == id) if (node.modelname === type && node.content.id == id)
result = node ; result = node ;
} }
@ -1446,6 +1446,10 @@ class ArticleFormatter extends Formatter {
return Article.props; return Article.props;
} }
static get attrs() {
return ['data_stock'];
}
/** /**
* <tt>s.price</tt> converted to UKF. * <tt>s.price</tt> converted to UKF.
*/ */
@ -1464,7 +1468,7 @@ class ArticleFormatter extends Formatter {
* Value of data_stock attribute based on s.stock. * Value of data_stock attribute based on s.stock.
*/ */
static attr_data_stock(s) { static attr_data_stock(s) {
if (s.stock >= 5) { return this._data_stock.ok; } if (s.stock > 5) { return this._data_stock.ok; }
else if (s.stock >= -5) { return this._data_stock.low; } else if (s.stock >= -5) { return this._data_stock.low; }
else /* s.stock < -5 */{ return this._data_stock.neg; } else /* s.stock < -5 */{ return this._data_stock.neg; }
} }

View file

@ -416,9 +416,10 @@ class ArticleManager {
this.list.fromAPI({}, this.display_list.bind(this), $.noop) ; this.list.fromAPI({}, this.display_list.bind(this), $.noop) ;
} }
//TODO: filter articles before ?
update_data(data) { update_data(data) {
for (let article_dict of data) { for (let article_dict of data.articles) {
article = this.list.find('article', article_dict['id']); var article = this.list.find('article', article_dict['id']);
// For now, article additions are disregarded // For now, article additions are disregarded
if (article) { if (article) {
@ -528,7 +529,7 @@ class ArticleAutocomplete {
var that = this ; var that = this ;
article_list.traverse(function(node) { article_list.traverse(function(node) {
if (node.type === 'article' && if (node.modelname === 'article' &&
node.content.name.toLowerCase() node.content.name.toLowerCase()
.startsWith(lower)) { .startsWith(lower)) {
that.matching['article'].push(node.content); that.matching['article'].push(node.content);
@ -557,11 +558,11 @@ class ArticleAutocomplete {
var that = this; var that = this;
this.manager.list.traverse(function(node) { this.manager.list.traverse(function(node) {
if (that.matching[node.type].indexOf(node.content) != -1) { if (that.matching[node.modelname].indexOf(node.content) != -1) {
that._$container.find('#'+node.type+'-'+node.content.id) that._$container.find('#'+node.modelname+'-'+node.content.id)
.show(); .show();
} else { } else {
that._$container.find('#'+node.type+'-'+node.content.id) that._$container.find('#'+node.modelname+'-'+node.content.id)
.hide(); .hide();
} }
}); });
@ -584,7 +585,7 @@ class ArticleAutocomplete {
var that = this; var that = this;
this.resetMatch(); this.resetMatch();
this.manager.list.traverse(function(node) { this.manager.list.traverse(function(node) {
that.matching[node.type].push(node.content); that.matching[node.modelname].push(node.content);
}); });
this.updateDisplay(); this.updateDisplay();
} }

View file

@ -648,11 +648,8 @@ $(document).ready(function() {
displayCheckoutData(); displayCheckoutData();
} }
} }
/* for (var i=0; i<data['articles'].length; i++) { kpsul.article_manager.update_data(data);
var article = data['articles'][i];
articles_container.find('#data-article-'+article['id']+' .stock')
.text(article['stock']);
}*/
if (data['addcost']) { if (data['addcost']) {
Config.set('addcost_for', data['addcost']['for']); Config.set('addcost_for', data['addcost']['for']);
Config.set('addcost_amount', data['addcost']['amount']); Config.set('addcost_amount', data['addcost']['amount']);

View file

@ -1546,7 +1546,7 @@ def kpsul_articles_data(request):
for article in articles: for article in articles:
articlelist.append({ articlelist.append({
'type': 'article', 'modelname': 'article',
'content': { 'content': {
'id': article.id, 'id': article.id,
'name': article.name, 'name': article.name,
@ -1554,7 +1554,7 @@ def kpsul_articles_data(request):
'stock': article.stock, 'stock': article.stock,
}, },
'parent': { 'parent': {
'type': 'category', 'modelname': 'category',
'content': { 'content': {
'id': article.category.id, 'id': article.category.id,
'name': article.category.name, 'name': article.category.name,