From d819327c6102fba1441f1c4b2e546c5c06561032 Mon Sep 17 00:00:00 2001 From: Antonin Date: Tue, 16 Apr 2019 15:11:34 +0200 Subject: [PATCH] Prevent leaving in case of vote started or comment written but not submitted --- js/app/studs.js | 16 ++++++++++++++++ locale/en.json | 1 + locale/fr.json | 1 + tpl/part/messages.tpl | 3 ++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/js/app/studs.js b/js/app/studs.js index 816f814..45ef0a6 100644 --- a/js/app/studs.js +++ b/js/app/studs.js @@ -21,6 +21,8 @@ var form; $(document).ready(function () { + // Flag for onbeforeunload event + var isSubmittingVote = false; /** * Save a list of polls inside LocalStorage @@ -88,6 +90,8 @@ $(document).ready(function () { $('html, body').animate({ scrollTop: messageContainer.offset().top }, 750); + } else { + isSubmittingVote = true; } }); @@ -172,6 +176,7 @@ $(document).ready(function () { return false; }); + /** * Disable view public results option when there's a password and the poll is not hidden */ @@ -182,6 +187,17 @@ $(document).ready(function () { $('#resultsPubliclyVisible').attr('disabled','disabled'); } }); + + $(window).on('beforeunload', function(e) { + var name = $('#name').val().trim(); + var comment = $('#comment').val().trim(); + + if ((!isSubmittingVote && name.length > 0) || comment.length > 0) { + var confirmationMessage = $('#preventLeaving').text(); + e.returnValue = confirmationMessage; + return confirmationMessage; + } + }); }); diff --git a/locale/en.json b/locale/en.json index f6d5e9d..3ad7452 100644 --- a/locale/en.json +++ b/locale/en.json @@ -133,6 +133,7 @@ "Failed to save poll": "Failed to save poll", "Forbidden!": "Forbidden!", "Identifier is already used": "Identifier is already used", + "If you quit now, your changes will be lost.": "If you quit now, your changes will be lost.", "JavaScript is disabled on your browser. It is required to create a poll.": "JavaScript is disabled on your browser. It is required to create a poll.", "Missing values": "Missing values", "No polls found": "No polls found", diff --git a/locale/fr.json b/locale/fr.json index 0387714..0b9fc84 100644 --- a/locale/fr.json +++ b/locale/fr.json @@ -133,6 +133,7 @@ "Failed to save poll": "Échec de la sauvegarde du sondage", "Forbidden!": "Interdit !", "Identifier is already used": "L'identifiant est déjà utilisé", + "If you quit now, your changes will be lost.": "Si vous quittez maintenant, vos modifications seront perdues.", "JavaScript is disabled on your browser. It is required to create a poll.": "JavaScript est désactivé sur votre navigateur. Son activation est requise pour la création d'un sondage.", "Missing values": "Il manque des valeurs", "No polls found": "Aucun sondage n'a été trouvé", diff --git a/tpl/part/messages.tpl b/tpl/part/messages.tpl index 2bcb7b9..a2edb9e 100644 --- a/tpl/part/messages.tpl +++ b/tpl/part/messages.tpl @@ -22,4 +22,5 @@ - \ No newline at end of file + + \ No newline at end of file