From 87943ea7b9bb1d9668f971291179f2087a4dc543 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Sun, 26 Mar 2017 00:20:47 -0300 Subject: [PATCH] Adapt kpsul to new functions --- kfet/templates/kfet/kpsul.html | 261 ++++++++++----------------------- 1 file changed, 78 insertions(+), 183 deletions(-) diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 15f83076..1506f6a5 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -182,7 +182,7 @@ $(document).ready(function() { // ----- // Lock to avoid multiple requests - var lock = 0; + window.lock = 0; // ----- // Auth @@ -190,26 +190,18 @@ $(document).ready(function() { function askComment(callback) { var comment = $('#id_comment').val(); - $.confirm({ + var commentDialog = new UserDialog({ title: 'Commentaire requis', content: '', - backgroundDismiss: true, - animation: 'top', - closeAnimation: 'bottom', - keyboardEnabled: true, - confirm: function() { - $('#id_comment').val(this.$content.find('input').val()); - callback(); - }, - onOpen: function() { - var that = this; - this.$content.find('input').on('keydown', function(e) { - if (e.keyCode == 13) - that.$confirmButton.click(); - }); - }, - onClose: function() { this._lastFocused = articleSelect; } + }); + + function confirm_callback(comment) { + $('#id_comment').val(comment); + callback(); + } + + commentDialog.open(confirm_callback, '', articleSelect); } function resetComment() { @@ -239,42 +231,22 @@ $(document).ready(function() { var operationGroup = $('#operationgroup_form'); var operations = $('#operation_formset'); - function performOperations(password = '') { - if (lock == 1) - return false; - lock = 1; + function performOperations() { var data = operationGroup.serialize() + '&' + operations.serialize(); - $.ajax({ - dataType: "json", - url : "{% url 'kfet.kpsul.perform_operations' %}", - method : "POST", - data : data, - beforeSend: function ($xhr) { - $xhr.setRequestHeader("X-CSRFToken", csrftoken); - if (password != '') - $xhr.setRequestHeader("KFetPassword", password); + api_with_auth({ + url: Urls['kfet.kpsul.perform_operations'](), + data: data, + on_success: function(data) { + updatePreviousOp(); + coolReset(); }, - }) - .done(function(data) { - updatePreviousOp(); - coolReset(); - lock = 0; - }) - .fail(function($xhr) { - var data = $xhr.responseJSON; - switch ($xhr.status) { - case 403: - requestAuth(data, performOperations, articleSelect); - break; - case 400: - if ('need_comment' in data['errors']) { - askComment(performOperations); - } else { - displayErrors(getErrorsHtml(data)); - } - break; - } - lock = 0; + on_400: function(data) { + if ('need_comment' in data['errors']) + askComment(performOperations); + else + displayErrors(data); + }, + next_focus: articleSelect, }); } @@ -290,40 +262,18 @@ $(document).ready(function() { var cancelButton = $('#cancel_operations'); var cancelForm = $('#cancel_form'); - function cancelOperations(opes_array, password = '') { - if (lock == 1) - return false - lock = 1; + function cancelOperations(opes_array) { 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); + api_with_auth({ + url: Urls['kfet.kpsul.cancel_operations'](), + data: data, + on_success: function(data) { + coolReset(); }, - - }) - .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); - }, account_manager._$input_trigramme); - break; - case 400: - displayErrors(getErrorsHtml(data)); - break; - } - lock = 0; + on_400: function(data) { + displayErrors(data); + }, + next_focus: kpsul.account_manager, }); } @@ -546,7 +496,7 @@ $(document).ready(function() { function amountEuroPurchase(article_data, 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; var reduc_divisor = 1; if (kpsul.account_manager.account.is_cof) @@ -691,59 +641,38 @@ $(document).ready(function() { // Ask deposit or withdraw // ----- + function askDeposit(is_checkout=1) { - var title = is_checkout ? 'Montant de la charge' : "Montant de l'édition"; - $.confirm({ - title: title, + var depositDialog = new UserDialog({ + title: is_checkout ? 'Montant de la charge' : "Montant de l'édition", content: '', - backgroundDismiss: true, - animation:'top', - closeAnimation:'bottom', - keyboardEnabled: true, - confirm: function() { - var amount = this.$content.find('input').val(); - if (!$.isNumeric(amount) || amount <= 0) - return false; - 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(); - } - }); - }, - onClose: function() { this._lastFocused = (articleSelect.val() ? articleNb : articleSelect) ; } }); + + function callback(amount) { + if (!$.isNumeric(amount) || amount <= 0) + return false; + addDeposit(amount, is_checkout); + + } + var next_focus = articleSelect.val() ? articleNb : articleSelect ; + + depositDialog.open(callback, '', next_focus); } function askWithdraw() { - $.confirm({ - title: 'Montant du retrait', - content: '', - backgroundDismiss: true, - animation:'top', - closeAnimation:'bottom', - keyboardEnabled: true, - confirm: function() { - var amount = this.$content.find('input').val(); - if (!$.isNumeric(amount) || amount <= 0) - return false; - addWithdraw(amount); - }, - onOpen: function() { - var that = this - this.$content.find('input').on('keydown', function(e) { - if (e.keyCode == 13) { - e.preventDefault(); - that.$confirmButton.click(); - } - }); - }, - onClose: function() { this._lastFocused = articleSelect; } + var withdrawDialog = new UserDialog({ + 'title': 'Montant du retrait', + 'content': '', }); + + function callback(amount) { + if (!$.isNumeric(amount) || amount <= 0) + return false; + addWithdraw(amount, is_checkout); + + } + + withdrawDialog.open(callback, '', articleSelect); } // Event @@ -862,65 +791,31 @@ $(document).ready(function() { addcost_html .find('.addcost_for').text(Config.get('addcost_for')).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'); } } - function sendAddcost(trigramme, amount, password='') { - var data = { - trigramme: trigramme, - amount: 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; - } - }); - } + + var addcostDialog = new UserDialog({ + 'title': 'Majoration', + 'content': '', + }); function askAddcost(errors = '') { - $.confirm({ - title: 'Majoration', - content: errors + '', - backgroundDismiss: true, - animation:'top', - closeAnimation:'bottom', - keyboardEnabled: true, - 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() { - var that = this - this.$content.find('input[name=amount]').on('keydown', function(e) { - if (e.keyCode == 13) { - e.preventDefault(); - that.$confirmButton.click(); - } + addcostDialog.open( + function(data) { + api_with_auth({ + url: Urls['kfet.kpsul.update_addcost'](), + data: data, + on_400: function(data) { + askAddcost(getErrorsHtml(data)); + }, + next_focus: kpsul.account_manager, }); }, - onClose: function() { this._lastFocused = account_manager._$input_trigramme; } - }); + errors, + kpsul.account_manager + ); }