From 8ef777710997dd01c24d75a49e8e79a2a37689be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Tue, 19 May 2020 23:54:59 +0200
Subject: [PATCH] Prevent clearing of form when changing language in between

Ask user if they really want to change the language as it will clear the form as soon as the main form on the page was edited at least once.
Fixes #80
---
 locale/de_DE/LC_MESSAGES/django.po | 11 ++++++++---
 templates/base.html                | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/locale/de_DE/LC_MESSAGES/django.po b/locale/de_DE/LC_MESSAGES/django.po
index 821d4c31..9c902594 100644
--- a/locale/de_DE/LC_MESSAGES/django.po
+++ b/locale/de_DE/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2020-05-19 06:38+0000\n"
+"POT-Creation-Date: 2020-05-19 21:50+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -46,10 +46,15 @@ msgstr "Anzahl"
 msgid "Event Status"
 msgstr "Event-Status"
 
-#: templates/base.html:78
+#: templates/base.html:28
+msgid ""
+"Are you sure you want to change the language now? This will clear the form!"
+msgstr "Wirklich jetzt die Sprache ändern? Das wird das Formular zurücksetzen!"
+
+#: templates/base.html:97
 msgid "Impress"
 msgstr "Impressum"
 
-#: templates/base.html:81
+#: templates/base.html:100
 msgid "This software is open source"
 msgstr "Diese Software ist Open Source"
diff --git a/templates/base.html b/templates/base.html
index 2b24b946..7971ddbf 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -19,6 +19,26 @@
 
     <link rel="stylesheet" href="{% static 'common/css/custom.css' %}">
 
+    <script type='text/javascript'>
+        var changed_form = false;
+
+         $(document).ready(function() {
+            // Ask before changing language if user changed main form on page
+            $("#language-switcher").submit(function(e){
+                if(changed_form) {
+                    if(!confirm("{% trans 'Are you sure you want to change the language now? This will clear the form!' %}"))
+                        e.preventDefault(e);
+                }
+            });
+
+            // Remember any main form edits/changes
+            $("form.post-form :input").change(function(){
+                if($(this).val() != '')
+                    changed_form = true;
+            });
+        });
+    </script>
+
     {% block imports %}{% endblock %}
 </head>
 <body>
@@ -29,6 +49,7 @@
                     method="post"
                     class="form-inline"
                     name="language-form"
+                    id="language-switcher"
                     style="flex-direction: column;">
                 {% csrf_token %}
 
-- 
GitLab