More resistant javascript code

This commit is contained in:
Raphael Michel
2016-11-08 20:28:16 +01:00
parent b30a3b5bee
commit ddf769e55d
5 changed files with 106 additions and 25 deletions
+30 -5
View File
@@ -7,8 +7,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-10-09 11:17+0000\n" "POT-Creation-Date: 2016-11-08 19:21+0000\n"
"PO-Revision-Date: 2016-10-08 22:12+0200\n" "PO-Revision-Date: 2016-11-08 20:23+0100\n"
"Last-Translator: Raphael Michel <michel@rami.io>\n" "Last-Translator: Raphael Michel <michel@rami.io>\n"
"Language-Team: \n" "Language-Team: \n"
"Language: de\n" "Language: de\n"
@@ -46,12 +46,11 @@ msgstr "Gesamtumsatz"
msgid "Contacting Stripe …" msgid "Contacting Stripe …"
msgstr "Kontaktiere 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" msgid "Close message"
msgstr "Schließen" msgstr "Schließen"
#: static/pretixcontrol/js/ui/main.js:43 #: static/pretixcontrol/js/ui/main.js:43
#: static/pretixpresale/js/ui/asynctask.js:49
msgid "Unknown error." msgid "Unknown error."
msgstr "Unbekannter Fehler." msgstr "Unbekannter Fehler."
@@ -63,7 +62,33 @@ msgstr "Sonstige"
msgid "Count" msgid "Count"
msgstr "Anzahl" 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 …" msgid "We are processing your request …"
msgstr "Wir verarbeiten deine Anfrage …" msgstr "Wir verarbeiten deine Anfrage …"
@@ -7,8 +7,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-10-09 11:17+0000\n" "POT-Creation-Date: 2016-11-08 19:21+0000\n"
"PO-Revision-Date: 2016-10-08 22:13+0200\n" "PO-Revision-Date: 2016-11-08 20:22+0100\n"
"Last-Translator: Raphael Michel <michel@rami.io>\n" "Last-Translator: Raphael Michel <michel@rami.io>\n"
"Language-Team: \n" "Language-Team: \n"
"Language: de\n" "Language: de\n"
@@ -46,12 +46,11 @@ msgstr "Gesamtumsatz"
msgid "Contacting Stripe …" msgid "Contacting Stripe …"
msgstr "Kontaktiere 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" msgid "Close message"
msgstr "Schließen" msgstr "Schließen"
#: static/pretixcontrol/js/ui/main.js:43 #: static/pretixcontrol/js/ui/main.js:43
#: static/pretixpresale/js/ui/asynctask.js:49
msgid "Unknown error." msgid "Unknown error."
msgstr "Unbekannter Fehler." msgstr "Unbekannter Fehler."
@@ -63,7 +62,33 @@ msgstr "Sonstige"
msgid "Count" msgid "Count"
msgstr "Anzahl" 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 …" msgid "We are processing your request …"
msgstr "Wir verarbeiten deine Anfrage …" msgstr "Wir verarbeiten deine Anfrage …"
+40 -9
View File
@@ -1,4 +1,4 @@
/*global $, waitingDialog, failsafe_gettext */ /*global $, waitingDialog, gettext */
var async_task_id = null; var async_task_id = null;
var async_task_timeout = null; var async_task_timeout = null;
var async_task_check_url = null; var async_task_check_url = null;
@@ -10,7 +10,7 @@ function async_task_check() {
'type': 'GET', 'type': 'GET',
'url': async_task_check_url, 'url': async_task_check_url,
'success': async_task_check_callback, 'success': async_task_check_callback,
'error': async_task_error, 'error': async_task_check_error,
'context': this, 'context': this,
'dataType': 'json' 'dataType': 'json'
} }
@@ -24,11 +24,32 @@ function async_task_check_callback(data, jqXHR, status) {
return; return;
} }
async_task_timeout = window.setTimeout(async_task_check, 250); 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) { function async_task_callback(data, jqXHR, status) {
"use strict"; "use strict";
$(this).data('ajaxing', false); $("body").data('ajaxing', false);
if (data.redirect) { if (data.redirect) {
location.href = data.redirect; location.href = data.redirect;
return; return;
@@ -36,17 +57,26 @@ function async_task_callback(data, jqXHR, status) {
async_task_id = data.async_id; async_task_id = data.async_id;
async_task_check_url = data.check_url; async_task_check_url = data.check_url;
async_task_timeout = window.setTimeout(async_task_check, 100); 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) { function async_task_error(jqXHR, textStatus, errorThrown) {
"use strict"; "use strict";
waitingDialog.hide(); $("body").data('ajaxing', false);
// TODO: Handle status codes != 200
var c = $(jqXHR.responseText).filter('.container'); var c = $(jqXHR.responseText).filter('.container');
if (c.length > 0) { if (c.length > 0) {
waitingDialog.hide();
ajaxErrDialog.show(c.first().html()); ajaxErrDialog.show(c.first().html());
} else { } 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"; "use strict";
$("body").on('submit', 'form[data-asynctask]', function (e) { $("body").on('submit', 'form[data-asynctask]', function (e) {
e.preventDefault(); e.preventDefault();
if ($(this).data('ajaxing')) { if ($("body").data('ajaxing')) {
return; return;
} }
$(this).data('ajaxing', true); async_task_id = null;
waitingDialog.show(failsafe_gettext('We are processing your request …')); $("body").data('ajaxing', true);
waitingDialog.show(gettext('We are processing your request …'));
$.ajax( $.ajax(
{ {
+3 -3
View File
@@ -1,4 +1,4 @@
/*global $,failsafe_gettext,failsafe_ngettext */ /*global $,gettext,ngettext */
var cart = { var cart = {
_deadline: null, _deadline: null,
@@ -7,10 +7,10 @@ var cart = {
draw_deadline: function () { draw_deadline: function () {
var diff = Math.floor(cart._deadline.diff(moment()) / 1000 / 60); var diff = Math.floor(cart._deadline.diff(moment()) / 1000 / 60);
if (diff < 0) { 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); window.clearInterval(cart._deadline_interval);
} else { } 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 one minute.",
"The items in your cart are reserved for you for {num} minutes.", "The items in your cart are reserved for you for {num} minutes.",
diff diff
+3 -3
View File
@@ -1,12 +1,12 @@
/*global $ */ /*global $ */
function failsafe_gettext(msgid) { function gettext(msgid) {
if (typeof django !== 'undefined' && typeof django.gettext !== 'undefined') { if (typeof django !== 'undefined' && typeof django.gettext !== 'undefined') {
return django.gettext(msgid); return django.gettext(msgid);
} }
return msgid; return msgid;
} }
function failsafe_ngettext(singular, plural, count) { function ngettext(singular, plural, count) {
if (typeof django !== 'undefined' && typeof django.ngettext !== 'undefined') { if (typeof django !== 'undefined' && typeof django.ngettext !== 'undefined') {
return django.ngettext(singular, plural, count); return django.ngettext(singular, plural, count);
} }
@@ -54,7 +54,7 @@ var ajaxErrDialog = {
"use strict"; "use strict";
$("#ajaxerr").html(c); $("#ajaxerr").html(c);
$("#ajaxerr .links").html("<a class='btn btn-default ajaxerr-close'>" $("#ajaxerr .links").html("<a class='btn btn-default ajaxerr-close'>"
+ failsafe_gettext("Close message") + "</a>"); + gettext("Close message") + "</a>");
$("body").addClass("ajaxerr"); $("body").addClass("ajaxerr");
}, },
hide: function () { hide: function () {