commit bbf89bd4bd4379aa62eb56e7727cc56a3d4e173e
Author: Jérôme Schneider <jschneider@entrouvert.com>
Date:   Tue Jul 29 10:53:02 2014 +0200

    dossiers: show an alert when closing without saving
    
    Closes #2740

diff --git a/calebasse/dossiers/templates/dossiers/patientrecord_tab1_general.html b/calebasse/dossiers/templates/dossiers/patientrecord_tab1_general.html
index f924f777..64d2bf2a 100644
--- a/calebasse/dossiers/templates/dossiers/patientrecord_tab1_general.html
+++ b/calebasse/dossiers/templates/dossiers/patientrecord_tab1_general.html
@@ -89,7 +89,7 @@
       <button type="button" id="patientrecord-history">Historique</button>
       <hr/>
       <p id="confidential-msg" style="display: none; font-size: 10px;">Confidentialité mise à jour</p>
-      {{ form.confidential.label_tag }} : {{ form.confidential }}
+      {{ form.confidential.label_tag }} : <span class="autosubmit">{{ form.confidential }}</span>
       <p id="pause-msg" style="display: none; font-size: 10px;">Pause facturation mise à jour</p>
       {{ form.pause.label_tag }} : {{ form.pause }}
       <br/><label>Commentaire de la pause facturation</label> <button class="save enable-on-change btn-maj">✔</button><br/>
diff --git a/calebasse/static/js/calebasse.dossiers.js b/calebasse/static/js/calebasse.dossiers.js
index b572861a..a17500ec 100644
--- a/calebasse/static/js/calebasse.dossiers.js
+++ b/calebasse/static/js/calebasse.dossiers.js
@@ -43,6 +43,35 @@ function load_add_address_dialog() {
       '#address-dlg', '600px', 'Ajouter');
 }
 
+function warning_on_unsave_change() {
+    var form_changed = false;
+    $(window).on("beforeunload", function() {
+        if (form_changed) {
+            return "Vous n'avez pas enregistré vos changements.";
+        }
+    });
+    $("#tabs").on("tabsbeforeactivate", function(event, ui) {
+        if (form_changed) {
+            var answer = confirm('Vous avez des changements non sauvegardés. Voulez vous vraiment continuer ?');
+            if (! answer) {
+                event.preventDefault();
+            }
+            else {
+                form_changed = false;
+            }
+        }
+    });
+    $('.autosubmit').on("click", function() {
+        form_changed = false;
+    });
+    $('form').on("change", function() {
+        form_changed = true;
+    });
+    $('button').on("click", function() {
+        form_changed = false;
+    });
+}
+
 function state_dialog(url, state_title, state_type) {
     $('#change-record').load(url,
             function () {
@@ -80,6 +109,7 @@ function state_dialog(url, state_title, state_type) {
 }
 
 function load_tab1_general() {
+    warning_on_unsave_change();
     $('#update-paper-id-btn').click(function() {
         generic_ajaxform_dialog('update/paper_id', 'Modifier le numéro du dossier papier',
             '#ajax-dlg', '500px', 'Modifier');
@@ -122,15 +152,13 @@ function load_tab1_general() {
       $('#patientrecord-history').click();
       location.hash = '';
     }
-    $('#id_pause').click(function() {
-        $('#general-form').submit();
-    });
-    $('#id_confidential').click(function() {
+    $('.autosubmit').click(function() {
         $('#general-form').submit();
     });
 }
 
 function load_tab2_adm() {
+    warning_on_unsave_change();
     init_magic_dialog();
     $('#prescription-transport-btn').click(function() {
         $('#ajax-dlg').load('prescription-transport',
@@ -351,9 +379,7 @@ function load_tab7_socialisation() {
 
 function load_tab8_medical() {
   calebasse_ajax_form('#tabs-8');
-  SelectFilter.init("id_mises_1", "Catégorie", 0, "/static/admin/");
-  SelectFilter.init("id_mises_2", "Catégorie", 0, "/static/admin/");
-  SelectFilter.init("id_mises_3", "Catégorie", 0, "/static/admin/");
+  warning_on_unsave_change();
 }
 
 
@@ -380,8 +406,8 @@ function load_tab8_medical() {
     });
 
 
-    $('.atabs').click(function() {
-        location.hash = 'tab=' + $(this).data('id');
+    $('#tabs').on("tabsload", function(event, ui) {
+        location.hash = 'tab=' + $(ui.tab).data('id');
     });
 
     $('#btn_all_state').click(function() {
