From ddf769e55d66e2009be012dc0f970b99bb609880 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Tue, 8 Nov 2016 20:28:16 +0100 Subject: [PATCH] More resistant javascript code --- src/pretix/locale/de/LC_MESSAGES/djangojs.po | 35 +++++++++++-- .../de_Informal/LC_MESSAGES/djangojs.po | 35 +++++++++++-- src/static/pretixpresale/js/ui/asynctask.js | 49 +++++++++++++++---- src/static/pretixpresale/js/ui/cart.js | 6 +-- src/static/pretixpresale/js/ui/main.js | 6 +-- 5 files changed, 106 insertions(+), 25 deletions(-) diff --git a/src/pretix/locale/de/LC_MESSAGES/djangojs.po b/src/pretix/locale/de/LC_MESSAGES/djangojs.po index 5067a55550..2c7d49cd87 100644 --- a/src/pretix/locale/de/LC_MESSAGES/djangojs.po +++ b/src/pretix/locale/de/LC_MESSAGES/djangojs.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2016-10-09 11:17+0000\n" -"PO-Revision-Date: 2016-10-08 22:12+0200\n" +"POT-Creation-Date: 2016-11-08 19:21+0000\n" +"PO-Revision-Date: 2016-11-08 20:23+0100\n" "Last-Translator: Raphael Michel \n" "Language-Team: \n" "Language: de\n" @@ -46,12 +46,11 @@ msgstr "Gesamtumsatz" msgid "Contacting Stripe …" msgstr "Kontaktiere Stripe …" -#: static/pretixcontrol/js/ui/main.js:28 static/pretixpresale/js/ui/main.js:44 +#: static/pretixcontrol/js/ui/main.js:28 static/pretixpresale/js/ui/main.js:57 msgid "Close message" msgstr "Schließen" #: static/pretixcontrol/js/ui/main.js:43 -#: static/pretixpresale/js/ui/asynctask.js:49 msgid "Unknown error." msgstr "Unbekannter Fehler." @@ -63,7 +62,33 @@ msgstr "Sonstige" msgid "Count" msgstr "Anzahl" -#: static/pretixpresale/js/ui/asynctask.js:61 +#: static/pretixpresale/js/ui/asynctask.js:27 +msgid "Your request has been queued on the server and will now be processed." +msgstr "" +"Ihre Anfrage befindet sich beim Server in der Warteschlange und wird nun " +"verarbeitet." + +#: static/pretixpresale/js/ui/asynctask.js:40 +#: static/pretixpresale/js/ui/asynctask.js:74 +msgid "An error of type {code} occured." +msgstr "Ein Fehler ist aufgetreten. Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:43 +msgid "" +"We currenctly cannot reach the server, but we keep trying. Last error code: " +"{code}" +msgstr "" +"Wir können den Server aktuell nicht erreichen, versuchen es aber weiter. " +"Letzter Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:77 +msgid "" +"We currenctly cannot reach the server. Please try again. Error code: {code}" +msgstr "" +"Wir können den Server aktuell nicht erreichen. Bitte versuchen Sie es noch " +"einmal. Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:92 msgid "We are processing your request …" msgstr "Wir verarbeiten deine Anfrage …" diff --git a/src/pretix/locale/de_Informal/LC_MESSAGES/djangojs.po b/src/pretix/locale/de_Informal/LC_MESSAGES/djangojs.po index a50cb01505..e042b40a42 100644 --- a/src/pretix/locale/de_Informal/LC_MESSAGES/djangojs.po +++ b/src/pretix/locale/de_Informal/LC_MESSAGES/djangojs.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2016-10-09 11:17+0000\n" -"PO-Revision-Date: 2016-10-08 22:13+0200\n" +"POT-Creation-Date: 2016-11-08 19:21+0000\n" +"PO-Revision-Date: 2016-11-08 20:22+0100\n" "Last-Translator: Raphael Michel \n" "Language-Team: \n" "Language: de\n" @@ -46,12 +46,11 @@ msgstr "Gesamtumsatz" msgid "Contacting Stripe …" msgstr "Kontaktiere Stripe …" -#: static/pretixcontrol/js/ui/main.js:28 static/pretixpresale/js/ui/main.js:44 +#: static/pretixcontrol/js/ui/main.js:28 static/pretixpresale/js/ui/main.js:57 msgid "Close message" msgstr "Schließen" #: static/pretixcontrol/js/ui/main.js:43 -#: static/pretixpresale/js/ui/asynctask.js:49 msgid "Unknown error." msgstr "Unbekannter Fehler." @@ -63,7 +62,33 @@ msgstr "Sonstige" msgid "Count" msgstr "Anzahl" -#: static/pretixpresale/js/ui/asynctask.js:61 +#: static/pretixpresale/js/ui/asynctask.js:27 +msgid "Your request has been queued on the server and will now be processed." +msgstr "" +"Deine Anfrage befindet sich beim Server in der Warteschlange und wird nun " +"verarbeitet." + +#: static/pretixpresale/js/ui/asynctask.js:40 +#: static/pretixpresale/js/ui/asynctask.js:74 +msgid "An error of type {code} occured." +msgstr "Ein Fehler ist aufgetreten. Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:43 +msgid "" +"We currenctly cannot reach the server, but we keep trying. Last error code: " +"{code}" +msgstr "" +"Wir können den Server aktuell nicht erreichen, versuchen es aber weiter. " +"Letzter Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:77 +msgid "" +"We currenctly cannot reach the server. Please try again. Error code: {code}" +msgstr "" +"Wir können den Server aktuell nicht erreichen. Bitte versuche es noch " +"einmal. Fehlercode: {code}" + +#: static/pretixpresale/js/ui/asynctask.js:92 msgid "We are processing your request …" msgstr "Wir verarbeiten deine Anfrage …" diff --git a/src/static/pretixpresale/js/ui/asynctask.js b/src/static/pretixpresale/js/ui/asynctask.js index 23552a4c9a..a90ce6aee6 100644 --- a/src/static/pretixpresale/js/ui/asynctask.js +++ b/src/static/pretixpresale/js/ui/asynctask.js @@ -1,4 +1,4 @@ -/*global $, waitingDialog, failsafe_gettext */ +/*global $, waitingDialog, gettext */ var async_task_id = null; var async_task_timeout = null; var async_task_check_url = null; @@ -10,7 +10,7 @@ function async_task_check() { 'type': 'GET', 'url': async_task_check_url, 'success': async_task_check_callback, - 'error': async_task_error, + 'error': async_task_check_error, 'context': this, 'dataType': 'json' } @@ -24,11 +24,32 @@ function async_task_check_callback(data, jqXHR, status) { return; } async_task_timeout = window.setTimeout(async_task_check, 250); + $("#loadingmodal p").text(gettext('Your request has been queued on the server and will now be ' + + 'processed.')); +} + +function async_task_check_error(jqXHR, textStatus, errorThrown) { + "use strict"; + var c = $(jqXHR.responseText).filter('.container'); + if (c.length > 0) { + waitingDialog.hide(); + ajaxErrDialog.show(c.first().html()); + } else { + if (jqXHR.status >= 400 && jqXHR.status < 500) { + waitingDialog.hide(); + alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status)); + } else { + // 500 can be an application error or overload in some cases :( + $("#loadingmodal p").text(gettext('We currenctly cannot reach the server, but we keep trying.' + + ' Last error code: {code}').replace(/\{code\}/, jqXHR.status)); + async_task_timeout = window.setTimeout(async_task_check, 5000); + } + } } function async_task_callback(data, jqXHR, status) { "use strict"; - $(this).data('ajaxing', false); + $("body").data('ajaxing', false); if (data.redirect) { location.href = data.redirect; return; @@ -36,17 +57,26 @@ function async_task_callback(data, jqXHR, status) { async_task_id = data.async_id; async_task_check_url = data.check_url; async_task_timeout = window.setTimeout(async_task_check, 100); + + history.pushState({}, "Waiting", async_task_check_url.replace(/ajax=1/, '')); } function async_task_error(jqXHR, textStatus, errorThrown) { "use strict"; - waitingDialog.hide(); - // TODO: Handle status codes != 200 + $("body").data('ajaxing', false); var c = $(jqXHR.responseText).filter('.container'); if (c.length > 0) { + waitingDialog.hide(); ajaxErrDialog.show(c.first().html()); } else { - alert(failsafe_gettext('Unknown error.')); + if (jqXHR.status >= 400 && jqXHR.status < 500) { + waitingDialog.hide(); + alert(gettext('An error of type {code} occured.').replace(/\{code\}/, jqXHR.status)); + } else { + waitingDialog.hide(); + alert(gettext('We currenctly cannot reach the server. Please try again. ' + + 'Error code: {code}').replace(/\{code\}/, jqXHR.status)); + } } } @@ -54,11 +84,12 @@ $(function () { "use strict"; $("body").on('submit', 'form[data-asynctask]', function (e) { e.preventDefault(); - if ($(this).data('ajaxing')) { + if ($("body").data('ajaxing')) { return; } - $(this).data('ajaxing', true); - waitingDialog.show(failsafe_gettext('We are processing your request …')); + async_task_id = null; + $("body").data('ajaxing', true); + waitingDialog.show(gettext('We are processing your request …')); $.ajax( { diff --git a/src/static/pretixpresale/js/ui/cart.js b/src/static/pretixpresale/js/ui/cart.js index fb5add4397..91d2a06ba5 100644 --- a/src/static/pretixpresale/js/ui/cart.js +++ b/src/static/pretixpresale/js/ui/cart.js @@ -1,4 +1,4 @@ -/*global $,failsafe_gettext,failsafe_ngettext */ +/*global $,gettext,ngettext */ var cart = { _deadline: null, @@ -7,10 +7,10 @@ var cart = { draw_deadline: function () { var diff = Math.floor(cart._deadline.diff(moment()) / 1000 / 60); if (diff < 0) { - $("#cart-deadline").text(failsafe_gettext("The items in your cart are no longer reserved for you.")); + $("#cart-deadline").text(gettext("The items in your cart are no longer reserved for you.")); window.clearInterval(cart._deadline_interval); } else { - $("#cart-deadline").text(failsafe_ngettext( + $("#cart-deadline").text(ngettext( "The items in your cart are reserved for you for one minute.", "The items in your cart are reserved for you for {num} minutes.", diff diff --git a/src/static/pretixpresale/js/ui/main.js b/src/static/pretixpresale/js/ui/main.js index 862245204e..7124856909 100644 --- a/src/static/pretixpresale/js/ui/main.js +++ b/src/static/pretixpresale/js/ui/main.js @@ -1,12 +1,12 @@ /*global $ */ -function failsafe_gettext(msgid) { +function gettext(msgid) { if (typeof django !== 'undefined' && typeof django.gettext !== 'undefined') { return django.gettext(msgid); } return msgid; } -function failsafe_ngettext(singular, plural, count) { +function ngettext(singular, plural, count) { if (typeof django !== 'undefined' && typeof django.ngettext !== 'undefined') { return django.ngettext(singular, plural, count); } @@ -54,7 +54,7 @@ var ajaxErrDialog = { "use strict"; $("#ajaxerr").html(c); $("#ajaxerr .links").html("" - + failsafe_gettext("Close message") + ""); + + gettext("Close message") + ""); $("body").addClass("ajaxerr"); }, hide: function () {