',
});
+
+//Note/TODO: the returned ajax object can be improved by allowing chaining on errors 403/400
function api_with_auth(settings, password) {
if (window.api_lock == 1)
return false;
@@ -357,7 +384,7 @@ function api_with_auth(settings, password) {
var on_success = settings.on_success || $.noop ;
var on_400 = settings.on_400 || $.noop ;
- $.ajax({
+ return $.ajax({
dataType: "json",
url: url,
method: "POST",
@@ -392,3 +419,9 @@ function api_with_auth(settings, password) {
window.api_lock = 0;
});
}
+
+String.prototype.pluralize = function(count, irreg_plural) {
+ if (Math.abs(count) >= 2)
+ return irreg_plural ? irreg_plural : this+'s' ;
+ return this ;
+}
diff --git a/kfet/static/kfet/js/kpsul.js b/kfet/static/kfet/js/kpsul.js
index e4623e44..e6fbd0c9 100644
--- a/kfet/static/kfet/js/kpsul.js
+++ b/kfet/static/kfet/js/kpsul.js
@@ -12,6 +12,11 @@ class KPsulManager {
this.account_manager = new AccountManager(this);
this.checkout_manager = new CheckoutManager(this);
this.article_manager = new ArticleManager(this);
+ this.history = new KHistory({
+ api_options: {'opesonly': true},
+ });
+
+ this._init_events();
}
reset(soft) {
@@ -23,6 +28,7 @@ class KPsulManager {
if (!soft) {
this.checkout_manager.reset();
this.article_manager.reset_data();
+ this.history.fetch();
}
}
@@ -37,6 +43,14 @@ class KPsulManager {
return this;
}
+ _init_events() {
+ var that = this ;
+ $(this.history).on("cancel_done", function(e) {
+ that.reset(true);
+ that.focus();
+ });
+ }
+
}
@@ -414,14 +428,19 @@ class ArticleManager {
this._$input = $('#article_autocomplete');
this._$nb = $('#article_number');
this._$stock = $('#article_stock');
- this.templates = {'category': '
',
- 'article' : '
'}
this.selected = new Article() ;
- this.list = new ArticleList() ;
- this.autocomplete = new ArticleAutocomplete(this);
+ this.data = new ArticleList() ;
+ var $container = $('#articles_data');
+ var templates = {
+ 'category': '
',
+ 'article' : '
'
+ } ;
+ this.display = new ForestDisplay($container, templates, this.data);
+ this.autocomplete = new ArticleAutocomplete(this, $container);
this._init_events();
+ OperationWebSocket.add_handler(data => this.update_data(data));
}
get nb() {
@@ -429,7 +448,7 @@ class ArticleManager {
}
display_list() {
- this.list.display(this._$container, this.templates) ;
+ this.display.render(this.data);
}
validate(article) {
@@ -449,25 +468,18 @@ class ArticleManager {
}
reset_data() {
- this._$container.html('');
- this.list.clear();
- this.list.fromAPI()
+ this.display.clear();
+ this.data.clear();
+ this.data.fromAPI()
.done( () => this.display_list() );
}
- get_article(id) {
- return this.list.find('article', id);
- }
-
update_data(data) {
for (let article_dict of data.articles) {
- var article = this.get_article(article_dict.id);
- // For now, article additions are disregarded
- if (article) {
- article.stock = article_dict.stock;
- this._$container.find('#article-'+article.id+' .stock')
- .text(article.stock);
+ var updated = this.data.update('article', article_dict.id, article_dict);
+ if (updated) {
+ this.display.update(updated);
}
}
}
@@ -496,7 +508,7 @@ class ArticleManager {
this._$container.on('click', '.article', function() {
var id = $(this).parent().attr('id').split('-')[1];
- var article = that.list.find('article', id);
+ var article = that.data.find('article', id);
if (article)
that.validate(article);
});
@@ -507,18 +519,26 @@ class ArticleManager {
that.reset();
that.focus();
}
- if (normalKeys.test(e.keyCode) || arrowKeys.test(e.KeyCode) || e.ctrlKey) {
+
+ if (normalKeys.test(e.keyCode) || arrowKeys.test(e.keyCode) || e.ctrlKey) {
if (e.ctrlKey && e.charCode == 65)
that._$nb.val('');
return true ;
}
+
if (that.constructor.check_nb(that.nb+e.key))
return true;
+
return false;
});
}
+ //Note : this function may not be needed after the whole rework
+ get_article(id) {
+ return this.data.find('article', id) ;
+ }
+
focus() {
if (this.is_empty())
this._$input.focus();
@@ -535,9 +555,9 @@ class ArticleManager {
class ArticleAutocomplete {
- constructor(article_manager) {
+ constructor(article_manager, $container) {
this.manager = article_manager;
- this._$container = article_manager._$container ;
+ this._$container = $container ;
this._$input = $('#article_autocomplete');
this.showAll() ;
@@ -549,11 +569,12 @@ class ArticleAutocomplete {
// 8:Backspace|9:Tab|13:Enter|46:DEL|112-117:F1-6|119-123:F8-F12
var normalKeys = /^(8|9|13|46|112|113|114|115|116|117|119|120|121|122|123)$/;
+ var arrowKeys = /^(37|38|39|40)$/;
this._$input
.on('keydown', function(e) {
var text = that._$input.val() ;
- if (normalKeys.test(e.keyCode) || e.ctrlKey) {
+ if (normalKeys.test(e.keyCode) || arrowKeys.test(e.keyCode) || e.ctrlKey) {
// For the backspace key, we suppose the cursor is at the very end
if(e.keyCode == 8) {
that.update(text.substring(0, text.length-1), true);
@@ -571,7 +592,7 @@ class ArticleAutocomplete {
update(prefix, backspace) {
this.resetMatch();
- var article_list = this.manager.list ;
+ var article_list = this.manager.data ;
var lower = prefix.toLowerCase() ;
var that = this ;
@@ -599,7 +620,7 @@ class ArticleAutocomplete {
updateDisplay() {
var that = this;
- this.manager.list.traverse('category', function(category) {
+ this.manager.data.traverse('category', function(category) {
var is_active = false;
for (let article of category.articles) {
if (that.matching.indexOf(article) != -1) {
@@ -634,7 +655,7 @@ class ArticleAutocomplete {
showAll() {
var that = this;
this.resetMatch();
- this.manager.list.traverse('article', function(article) {
+ this.manager.data.traverse('article', function(article) {
that.matching.push(article);
});
this.updateDisplay();
diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html
index b55f2b99..58f1f4a9 100644
--- a/kfet/templates/kfet/account_read.html
+++ b/kfet/templates/kfet/account_read.html
@@ -4,10 +4,16 @@
{% load l10n %}
{% block extra_head %}
+
+
+
+
+
+
{% if account.user == request.user %}
@@ -94,33 +100,11 @@ $(document).ready(function() {
diff --git a/kfet/templates/kfet/history.html b/kfet/templates/kfet/history.html
index 19ed0332..2075cf30 100644
--- a/kfet/templates/kfet/history.html
+++ b/kfet/templates/kfet/history.html
@@ -7,6 +7,7 @@
+
@@ -38,6 +39,9 @@
Comptes {{ filter_form.accounts }}