From 281d532a20228654e7b724bae0d35ab962ab9af4 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 27 Jan 2015 16:31:53 +0100 Subject: [PATCH] Js code for solving #391 - refs #391 --- Resources/public/js/chill.js | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Resources/public/js/chill.js b/Resources/public/js/chill.js index fb553dfa1..f2cfbd89c 100644 --- a/Resources/public/js/chill.js +++ b/Resources/public/js/chill.js @@ -82,6 +82,42 @@ var chill = function() { } } + + /** + * Protect to leave a page where the form 'form_id' has unsaved changes by displaying + * a popup-containing a warning. + * + * @param{string} form_id A identification string of the form + * @param{string} unsaved_data_message The string to display in the warning pop-up + * @return nothing + */ + function protectUnsavedDataForFrom(form_id, unsaved_data_message) { + var form_submitted = false; + var unsaved_data = false; + + $(form_id) + .submit(function() { + form_submitted = true; + }) + .on('reset', function() { + unsaved_data = false; + }); + + + + $.each($(form_id).find(':input'), function(i,e) { + $(e).change(function() { + unsaved_data = true; + }); + }); + + $(window).bind('beforeunload', function(){ + if((!form_submitted) && unsaved_data) { + return unsaved_data_message; + } + }); + } + /* Enable the following behavior : when the user change the value of an other field, its checkbox is checked. */ @@ -98,5 +134,6 @@ var chill = function() { initPikaday: initPikaday, emulateSticky: emulateSticky, checkOtherValueOnChange: checkOtherValueOnChange, + protectUnsavedDataForFrom: protectUnsavedDataForFrom, }; } (); \ No newline at end of file