diff --git a/kfet/static/kfet/js/history.js b/kfet/static/kfet/js/history.js index c087d56a..2869ea86 100644 --- a/kfet/static/kfet/js/history.js +++ b/kfet/static/kfet/js/history.js @@ -7,6 +7,20 @@ function KHistory(options = {}) { this.$container = $(this.container); + this.$container.selectable({ + filter: 'div.opegroup, div.ope', + selected: function (e, ui) { + $(ui.selected).each(function () { + if ($(this).hasClass('opegroup')) { + var opegroup = $(this).data('id'); + $(this).siblings('.ope').filter(function () { + return $(this).data('opegroup') == opegroup + }).addClass('ui-selected'); + } + }); + }, + }); + this.reset = function () { this.$container.html(''); }; @@ -189,6 +203,63 @@ function KHistory(options = {}) { $opegroup.find('.amount').text(amount); } + this.fetch = function (fetch_options) { + options = $.extend({}, this.fetch_options, fetch_options); + var that = this; + $.ajax({ + dataType: "json", + url: django_urls["kfet.history.json"](), + method: "POST", + data: options, + }).done(function (data) { + for (let opegroup of data['opegroups']) { + that.addOpeGroup(opegroup); + } + }); + } + + this.cancel_opes = function (opes, password = "") { + if (window.lock == 1) + return false + window.lock = 1; + $.ajax({ + dataType: "json", + url: django_urls["kfet.kpsul.cancel_operations"](), + method: "POST", + data: { + 'operations': opes + }, + beforeSend: function ($xhr) { + $xhr.setRequestHeader("X-CSRFToken", csrftoken); + if (password != '') + $xhr.setRequestHeader("KFetPassword", password); + }, + + }).done(function (data) { + window.lock = 0; + }).fail(function ($xhr) { + var data = $xhr.responseJSON; + switch ($xhr.status) { + case 403: + requestAuth(data, function (password) { + this.cancel(opes, password); + }); + break; + case 400: + displayErrors(getErrorsHtml(data)); + break; + } + window.lock = 0; + }); + } + + this.cancel_selected = function () { + var opes_to_cancel = this.$container.find('.ope.ui-selected').map(function () { + return $(this).data('id'); + }).toArray(); + if (opes_to_cancel.length > 0) + this.cancel(opes_to_cancel); + } } KHistory.default_options = { diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 171c7030..c745d598 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -189,7 +189,7 @@ $(document).ready(function() { // ----- // Lock to avoid multiple requests - lock = 0; + window.lock = 0; // Retrieve settings @@ -479,9 +479,9 @@ $(document).ready(function() { var operations = $('#operation_formset'); function performOperations(password = '') { - if (lock == 1) + if (window.lock == 1) return false; - lock = 1; + window.lock = 1; var data = operationGroup.serialize() + '&' + operations.serialize(); $.ajax({ dataType: "json", @@ -497,7 +497,7 @@ $(document).ready(function() { .done(function(data) { updatePreviousOp(); coolReset(); - lock = 0; + window.lock = 0; }) .fail(function($xhr) { var data = $xhr.responseJSON; @@ -513,7 +513,7 @@ $(document).ready(function() { } break; } - lock = 0; + window.lock = 0; }); } @@ -522,55 +522,6 @@ $(document).ready(function() { performOperations(); }); - // ----- - // Cancel operations - // ----- - - var cancelButton = $('#cancel_operations'); - var cancelForm = $('#cancel_form'); - - function cancelOperations(opes_array, password = '') { - if (lock == 1) - return false - lock = 1; - var data = { 'operations' : opes_array } - $.ajax({ - dataType: "json", - url : "{% url 'kfet.kpsul.cancel_operations' %}", - method : "POST", - data : data, - beforeSend: function ($xhr) { - $xhr.setRequestHeader("X-CSRFToken", csrftoken); - if (password != '') - $xhr.setRequestHeader("KFetPassword", password); - }, - - }) - .done(function(data) { - coolReset(); - lock = 0; - }) - .fail(function($xhr) { - var data = $xhr.responseJSON; - switch ($xhr.status) { - case 403: - requestAuth(data, function(password) { - cancelOperations(opes_array, password); - }, triInput); - break; - case 400: - displayErrors(getErrorsHtml(data)); - break; - } - lock = 0; - }); - } - - // Event listeners - cancelButton.on('click', function() { - cancelOperations(); - }); - // ----- // Articles data // ----- @@ -1189,24 +1140,12 @@ $(document).ready(function() { // History // ----- - khistory = new KHistory(); - - function getHistory() { - var data = { + khistory = new KHistory({ + fetch_options: { from: moment().subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss'), - }; - $.ajax({ - dataType: "json", - url : "{% url 'kfet.history.json' %}", - method : "POST", - data : data, - }) - .done(function(data) { - for (var i=0; i 0) - cancelOperations(opes_to_cancel); + khistory.cancel_selected() } }); @@ -1396,7 +1316,7 @@ $(document).ready(function() { khistory.reset(); resetSettings().done(function (){ getArticles(); - getHistory(); + khistory.fetch(); displayAddcost(); }); }