CRM-14353 CRM-15053 - Better handler for dialog close events which accounts for esc key
authorColeman Watts <coleman@civicrm.org>
Wed, 30 Jul 2014 09:00:27 +0000 (10:00 +0100)
committerColeman Watts <coleman@civicrm.org>
Wed, 30 Jul 2014 09:00:27 +0000 (10:00 +0100)
js/crm.ajax.js

index b647e370c8bfd5a604da392944e2c9e3211b4b1a..417e62296b6f9867e72cdee82d8d677e49622845 100644 (file)
     // CRM-14353 - Warn of unsaved changes for all forms except those which have opted out
     function cancelAction() {
       var dirty = CRM.utils.initialValueChanged($('form:not([data-warn-changes=false])', widget));
-      widget
-        .attr('data-unsaved-changes', dirty ? 'true' : 'false')
-        .dialog('close');
+      widget.attr('data-unsaved-changes', dirty ? 'true' : 'false');
       if (dirty) {
         var id = widget.attr('id') + '-unsaved-alert',
           title = widget.dialog('option', 'title'),
         });
       }
     }
-    if (widget.data('uiDialog')) {
-      // CRM-14353 - This is a bit harsh but we are removing jQuery UI's event handler from the close button and adding our own
-      widget.parent().find('.ui-dialog-titlebar-close').first().off().click(cancelAction);
-    }
+
+    widget.data('uiDialog') && widget.on('dialogbeforeclose', function(e) {
+      // CRM-14353 - Warn unsaved changes if user clicks close button or presses "esc"
+      if (e.originalEvent) {
+        cancelAction();
+      }
+    });
 
     widget.on('crmFormLoad.crmForm', function(event, data) {
       var $el = $(this)
           $el.trigger('crmFormCancel', e);
           if ($el.data('uiDialog') && settings.autoClose) {
             cancelAction();
+            $el.dialog('close');
           }
           else if (!settings.autoClose) {
             $el.crmSnippet('resetUrl').crmSnippet('refresh');