From 8ce87d0b959ff3efc41fc587c081f6a237c4698d Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 6 Sep 2017 13:17:10 +0200
Subject: [PATCH] when form data is changed clicking on navigation link require
 confirmation

---
 smash/web/static/js/appointment.js | 14 ++++++++------
 smash/web/static/js/smash.js       | 13 +++++++++++++
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/smash/web/static/js/appointment.js b/smash/web/static/js/appointment.js
index 0be1d9e0..6cb81067 100644
--- a/smash/web/static/js/appointment.js
+++ b/smash/web/static/js/appointment.js
@@ -146,17 +146,19 @@ function appointment_date_change_behaviour(datetime_picker, worker_select, minut
 
     $(worker_select).addClass(previousClass);
 
-    $(worker_select).on('focus', function () {
-        // Store the current value on focus and on change
-        previousClass = getSelectedClass();
-    }).change(function () {
+    function workerChanged() {
         // Do something with the previous value after the change
 
         $(worker_select).removeClass(previousClass);
         // Make sure the previous value is updated
         previousClass = getSelectedClass();
         $(worker_select).addClass(previousClass);
-    });
+    }
+
+    $(worker_select).on('focus', function () {
+        // Store the current value on focus and on change
+        previousClass = getSelectedClass();
+    }).change(workerChanged);
 
     function timeChange() {
         if ($(datetime_picker).val() != "") {
@@ -190,7 +192,7 @@ function appointment_date_change_behaviour(datetime_picker, worker_select, minut
                         option.className = "worker-option-no-match";
                     }
                 }
-                $(worker_select).trigger("change");
+                workerChanged();
             });
         }
     }
diff --git a/smash/web/static/js/smash.js b/smash/web/static/js/smash.js
index 7d27ccd0..2d37d927 100644
--- a/smash/web/static/js/smash.js
+++ b/smash/web/static/js/smash.js
@@ -9,6 +9,19 @@ $(document).ready(function () {
         $("." + $(this).attr("data-hide")).hide();
     });
 
+    var formDataChanged = false;
+
+    $(":input", this).change(function () {
+        formDataChanged = true;
+    });
+
+    $('.main-sidebar a').click(function () {
+        if (formDataChanged) {
+            return confirm("Data changed. Are you sure you want to leave?");
+        } else {
+            return true;
+        }
+    })
 });
 
 $.ajaxSetup({
-- 
GitLab