Déplace la logique de l'historique dans history.js

On change le lock en `window.lock` pour y avoir accès partout
This commit is contained in:
Ludovic Stephan 2019-12-23 12:54:30 +01:00
parent 9b2c4c1f98
commit 36d6a4a1cd
2 changed files with 83 additions and 92 deletions

View file

@ -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 = {

View file

@ -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<data['opegroups'].length; i++) {
khistory.addOpeGroup(data['opegroups'][i]);
}
});
}
opesonly: true,
},
});
var previousop_container = $('#previous_op');
@ -1302,29 +1241,10 @@ $(document).ready(function() {
// Cancel from history
// -----
khistory.$container.selectable({
filter: 'div.opegroup, div.ope',
selected: function(e, ui) {
$(ui.selected).each(function() {
if ($(this).hasClass('opegroup')) {
var opegroup = $(this).data('opegroup');
$(this).siblings('.ope').filter(function() {
return $(this).data('opegroup') == opegroup
}).addClass('ui-selected');
}
});
},
});
$(document).on('keydown', function (e) {
if (e.keyCode == 46) {
// DEL (Suppr)
var opes_to_cancel = [];
khistory.$container.find('.ope.ui-selected').each(function () {
opes_to_cancel.push($(this).data('ope'));
});
if (opes_to_cancel.length > 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();
});
}