Adapt kpsul to new functions

This commit is contained in:
Ludovic Stephan 2017-03-26 00:20:47 -03:00
parent 6be2f086df
commit 87943ea7b9

View file

@ -182,7 +182,7 @@ $(document).ready(function() {
// ----- // -----
// Lock to avoid multiple requests // Lock to avoid multiple requests
var lock = 0; window.lock = 0;
// ----- // -----
// Auth // Auth
@ -190,26 +190,18 @@ $(document).ready(function() {
function askComment(callback) { function askComment(callback) {
var comment = $('#id_comment').val(); var comment = $('#id_comment').val();
$.confirm({ var commentDialog = new UserDialog({
title: 'Commentaire requis', title: 'Commentaire requis',
content: '<input type="text" name="comment_opegroup" value="'+comment+'" autofocus>', content: '<input type="text" name="comment_opegroup" value="'+comment+'" autofocus>',
backgroundDismiss: true,
animation: 'top', });
closeAnimation: 'bottom',
keyboardEnabled: true, function confirm_callback(comment) {
confirm: function() { $('#id_comment').val(comment);
$('#id_comment').val(this.$content.find('input').val());
callback(); callback();
}, }
onOpen: function() {
var that = this; commentDialog.open(confirm_callback, '', articleSelect);
this.$content.find('input').on('keydown', function(e) {
if (e.keyCode == 13)
that.$confirmButton.click();
});
},
onClose: function() { this._lastFocused = articleSelect; }
});
} }
function resetComment() { function resetComment() {
@ -239,42 +231,22 @@ $(document).ready(function() {
var operationGroup = $('#operationgroup_form'); var operationGroup = $('#operationgroup_form');
var operations = $('#operation_formset'); var operations = $('#operation_formset');
function performOperations(password = '') { function performOperations() {
if (lock == 1)
return false;
lock = 1;
var data = operationGroup.serialize() + '&' + operations.serialize(); var data = operationGroup.serialize() + '&' + operations.serialize();
$.ajax({ api_with_auth({
dataType: "json", url: Urls['kfet.kpsul.perform_operations'](),
url : "{% url 'kfet.kpsul.perform_operations' %}", data: data,
method : "POST", on_success: function(data) {
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
})
.done(function(data) {
updatePreviousOp(); updatePreviousOp();
coolReset(); coolReset();
lock = 0; },
}) on_400: function(data) {
.fail(function($xhr) { if ('need_comment' in data['errors'])
var data = $xhr.responseJSON;
switch ($xhr.status) {
case 403:
requestAuth(data, performOperations, articleSelect);
break;
case 400:
if ('need_comment' in data['errors']) {
askComment(performOperations); askComment(performOperations);
} else { else
displayErrors(getErrorsHtml(data)); displayErrors(data);
} },
break; next_focus: articleSelect,
}
lock = 0;
}); });
} }
@ -290,40 +262,18 @@ $(document).ready(function() {
var cancelButton = $('#cancel_operations'); var cancelButton = $('#cancel_operations');
var cancelForm = $('#cancel_form'); var cancelForm = $('#cancel_form');
function cancelOperations(opes_array, password = '') { function cancelOperations(opes_array) {
if (lock == 1)
return false
lock = 1;
var data = { 'operations' : opes_array } var data = { 'operations' : opes_array }
$.ajax({ api_with_auth({
dataType: "json", url: Urls['kfet.kpsul.cancel_operations'](),
url : "{% url 'kfet.kpsul.cancel_operations' %}", data: data,
method : "POST", on_success: function(data) {
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
})
.done(function(data) {
coolReset(); coolReset();
lock = 0; },
}) on_400: function(data) {
.fail(function($xhr) { displayErrors(data);
var data = $xhr.responseJSON; },
switch ($xhr.status) { next_focus: kpsul.account_manager,
case 403:
requestAuth(data, function(password) {
cancelOperations(opes_array, password);
}, account_manager._$input_trigramme);
break;
case 400:
displayErrors(getErrorsHtml(data));
break;
}
lock = 0;
}); });
} }
@ -546,7 +496,7 @@ $(document).ready(function() {
function amountEuroPurchase(article_data, nb) { function amountEuroPurchase(article_data, nb) {
var amount_euro = - article_data[3] * nb ; var amount_euro = - article_data[3] * nb ;
if (Config.get('addcost_for') && Config.get('addcost_amount') && account_manager.account.trigramme != Config.get('addcost_for')) if (Config.get('addcost_for') && Config.get('addcost_amount') && kpsul.account_manager.account.trigramme != Config.get('addcost_for'))
amount_euro -= Config.get('addcost_amount') * nb; amount_euro -= Config.get('addcost_amount') * nb;
var reduc_divisor = 1; var reduc_divisor = 1;
if (kpsul.account_manager.account.is_cof) if (kpsul.account_manager.account.is_cof)
@ -691,59 +641,38 @@ $(document).ready(function() {
// Ask deposit or withdraw // Ask deposit or withdraw
// ----- // -----
function askDeposit(is_checkout=1) { function askDeposit(is_checkout=1) {
var title = is_checkout ? 'Montant de la charge' : "Montant de l'édition"; var depositDialog = new UserDialog({
$.confirm({ title: is_checkout ? 'Montant de la charge' : "Montant de l'édition",
title: title,
content: '<input type="number" lang="en" step="0.01" min="0.01" on autofocus placeholder="€">', content: '<input type="number" lang="en" step="0.01" min="0.01" on autofocus placeholder="€">',
backgroundDismiss: true, });
animation:'top',
closeAnimation:'bottom', function callback(amount) {
keyboardEnabled: true,
confirm: function() {
var amount = this.$content.find('input').val();
if (!$.isNumeric(amount) || amount <= 0) if (!$.isNumeric(amount) || amount <= 0)
return false; return false;
addDeposit(amount, is_checkout); addDeposit(amount, is_checkout);
},
onOpen: function() {
var that = this
this.$content.find('input').on('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
that.$confirmButton.click();
} }
}); var next_focus = articleSelect.val() ? articleNb : articleSelect ;
},
onClose: function() { this._lastFocused = (articleSelect.val() ? articleNb : articleSelect) ; } depositDialog.open(callback, '', next_focus);
});
} }
function askWithdraw() { function askWithdraw() {
$.confirm({ var withdrawDialog = new UserDialog({
title: 'Montant du retrait', 'title': 'Montant du retrait',
content: '<input type="number" lang="en" step="0.01" min="0.01" on autofocus placeholder="€">', 'content': '<input type="number" lang="en" step="0.01" min="0.01" on autofocus placeholder="€">',
backgroundDismiss: true, });
animation:'top',
closeAnimation:'bottom', function callback(amount) {
keyboardEnabled: true,
confirm: function() {
var amount = this.$content.find('input').val();
if (!$.isNumeric(amount) || amount <= 0) if (!$.isNumeric(amount) || amount <= 0)
return false; return false;
addWithdraw(amount); addWithdraw(amount, is_checkout);
},
onOpen: function() {
var that = this
this.$content.find('input').on('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
that.$confirmButton.click();
} }
});
}, withdrawDialog.open(callback, '', articleSelect);
onClose: function() { this._lastFocused = articleSelect; }
});
} }
// Event // Event
@ -862,65 +791,31 @@ $(document).ready(function() {
addcost_html addcost_html
.find('.addcost_for').text(Config.get('addcost_for')).end() .find('.addcost_for').text(Config.get('addcost_for')).end()
.find('.addcost_amount').text(Config.get('addcost_amount').toFixed(2)).end(); .find('.addcost_amount').text(Config.get('addcost_amount').toFixed(2)).end();
checkout_container.prepend(addcost_html); kpsul.checkout_manager._$container.prepend(addcost_html);
$('body').css('animation', 'colorchange 3s infinite'); $('body').css('animation', 'colorchange 3s infinite');
} }
} }
function sendAddcost(trigramme, amount, password='') {
var data = { var addcostDialog = new UserDialog({
trigramme: trigramme, 'title': 'Majoration',
amount: amount, 'content': '<input type="text" placeholder="Trigramme" autocomplete="off" name="trigramme" spellcheck="false" style="text-transform:uppercase" autofocus><input type="number" lang="en" step="0.01" min="0.01" placeholder="€" name="amount">',
}
$.ajax({
dataType: "json",
url : "{% url 'kfet.kpsul.update_addcost' %}",
method : "POST",
data : data,
beforeSend: function ($xhr) {
$xhr.setRequestHeader("X-CSRFToken", csrftoken);
if (password != '')
$xhr.setRequestHeader("KFetPassword", password);
},
})
.fail(function($xhr) {
var data = $xhr.responseJSON;
switch ($xhr.status) {
case 403:
requestAuth(data, function(password) {
sendAddcost(trigramme, amount, password);
}, account_manager._$input_trigramme);
break;
case 400:
askAddcost(getErrorsHtml(data));
break;
}
}); });
}
function askAddcost(errors = '') { function askAddcost(errors = '') {
$.confirm({ addcostDialog.open(
title: 'Majoration', function(data) {
content: errors + '<input type="text" placeholder="Trigramme" autocomplete="off" name="trigramme" spellcheck="false" style="text-transform:uppercase" autofocus><input type="number" lang="en" step="0.01" min="0.01" placeholder="€" name="amount">', api_with_auth({
backgroundDismiss: true, url: Urls['kfet.kpsul.update_addcost'](),
animation:'top', data: data,
closeAnimation:'bottom', on_400: function(data) {
keyboardEnabled: true, askAddcost(getErrorsHtml(data));
confirm: function() {
var trigramme = this.$content.find('input[name=trigramme]').val().toUpperCase();
var amount = this.$content.find('input[name=amount]').val();
sendAddcost(trigramme, amount);
}, },
onOpen: function() { next_focus: kpsul.account_manager,
var that = this
this.$content.find('input[name=amount]').on('keydown', function(e) {
if (e.keyCode == 13) {
e.preventDefault();
that.$confirmButton.click();
}
}); });
}, },
onClose: function() { this._lastFocused = account_manager._$input_trigramme; } errors,
}); kpsul.account_manager
);
} }