CRM-14789 - crmCaseType - Allow enable/disable/delete
[civicrm-core.git] / js / crm.ajax.js
index e018472b6b7f179130377ab664945fbe2359a66f..a3e9932e6e0ebd4c98f2ee77022cba9e40117f52 100644 (file)
@@ -3,7 +3,7 @@
  * @see https://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface
  * @see https://wiki.civicrm.org/confluence/display/CRMDOC/Ajax+Pages+and+Forms
  */
-(function($, CRM) {
+(function($, CRM, undefined) {
   /**
    * Almost like {crmURL} but on the client side
    * eg: var url = CRM.url('civicrm/contact/view', {reset:1,cid:42});
       this.element.trigger('crmAjaxFail', data);
       CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error');
     },
+    _onError: function(data) {
+      this.element.attr('data-unsaved-changes', 'false').trigger('crmAjaxError', data);
+      if (this.options.crmForm && this.options.crmForm.autoClose && this.element.data('uiDialog')) {
+        this.element.dialog('close');
+      }
+    },
     _formatUrl: function(url) {
       // Strip hash
       url = url.split('#')[0];
       }
       this.options.block && $('.blockOverlay', this.element).length < 1 && this.element.block();
       $.getJSON(url, function(data) {
-        if (typeof(data) != 'object' || typeof(data.content) != 'string') {
+        if (!$.isPlainObject(data)) {
           that._onFailure(data);
           return;
         }
+        if (data.status === 'error') {
+          that._onError(data);
+          return;
+        }
         data.url = url;
         that.element.trigger('crmBeforeLoad', data).html(data.content);
         that._handleOrderLinks();
         refreshAction: ['next_new', 'submit_savenext', 'upload_new'],
         cancelButton: '.cancel',
         openInline: 'a.open-inline, a.button, a.action-item',
-        onCancel: function(event) {},
-        onError: function(data) {
-          var $el = $(this);
-          $el.html(data.content).trigger('crmLoad', data).trigger('crmFormLoad', data).trigger('crmFormError', data);
-          if (typeof(data.errors) == 'object') {
-            $.each(data.errors, function(formElement, msg) {
-              $('[name="'+formElement+'"]', $el).crmError(msg);
-            });
-          }
-        }
+        onCancel: function(event) {}
       }
     };
     // Move options that belong to crmForm. Others will be passed through to crmSnippet
         url: data.url.replace(/reset=1[&]?/, ''),
         dataType: 'json',
         success: function(response) {
-          if (response.status !== 'form_error') {
+          if (response.content === undefined) {
             $el.crmSnippet('option', 'block') && $el.unblock();
             $el.trigger('crmFormSuccess', response);
             // Reset form for e.g. "save and new"
           }
           else {
             response.url = data.url;
-            settings.onError.call($el, response);
+            $el.html(response.content).trigger('crmLoad', response).trigger('crmFormLoad', response);
+            if (response.status === 'form_error') {
+              $el.trigger('crmFormError', response);
+              $.each(response.errors || [], function(formElement, msg) {
+                $('[name="'+formElement+'"]', $el).crmError(msg);
+              });
+            }
           }
         },
         beforeSerialize: function(form, options) {
           return false;
         });
       }
+      // Alow a button to prevent ajax submit
+      $('input[data-no-ajax-submit=true]').click(function() {
+        $(this).closest('form').ajaxFormUnbind();
+      });
       // For convenience, focus the first field
       $('input[type=text], textarea, select', this).filter(':visible').first().not('.dateplugin').focus();
     });
   };
   /**
    * Handler for jQuery click event e.g. $('a').click(CRM.popup)
-   * @returns {boolean}
    */
   CRM.popup = function(e) {
     var $el = $(this).first(),