CRM-10693 - Ajax - Add helpers CRM.loadPage and CRM.loadForm
authorColeman Watts <coleman@civicrm.org>
Sun, 17 Nov 2013 16:56:54 +0000 (08:56 -0800)
committerColeman Watts <coleman@civicrm.org>
Sun, 17 Nov 2013 16:56:54 +0000 (08:56 -0800)
CRM/Core/Controller.php
CRM/Core/QuickForm/Action/Display.php
js/Common.js

index 21a4ac461b3686fa30c2a2e9eee793b5096a610c..7bf8d8c7c64a356de074fa5ca1d85f2cdf62f14f 100644 (file)
@@ -53,7 +53,7 @@ class CRM_Core_Controller extends HTML_QuickForm_Controller {
    *
    * @var string
    */
-  protected $_title;
+  public $_title;
 
   /**
    * The key associated with this controller
index 14c76669c6e11cbd556ccea3abd4ab55552efb32..7302b26d2470b35f2992cf660ee15a9a9ad807ad 100644 (file)
@@ -157,7 +157,10 @@ class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action {
     }
 
     if ($controller->_QFResponseType == 'json') {
-      $response = array('content' => $html);
+      $response = array(
+        'content' => $html,
+        'title' => $controller->_title,
+      );
       if (!empty($form['errors'])) {
         $response['status'] = 'form_error';
         $response['errors'] = $form['errors'];
index 21dca3559eb8042dd686f3a7637a30034d21bd27..cd1cda4ec708bbbdd26f3fce84218ebea2b99444 100644 (file)
@@ -820,6 +820,86 @@ CRM.validate = CRM.validate || {
     });
   }
 
+  CRM.loadPage = function(url, options) {
+    options = options || {};
+    url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6';
+    var settings = {
+      target: '#crm-ajax-dialog',
+      dialogSettings: {
+        modal: true,
+        minWidth: 600,
+        close: function() {
+          $(this).dialog('destroy');
+          $(this).remove();
+        }
+      },
+      onLoad: null,
+      type: 'Page'
+    };
+    $.extend(true, settings, options);
+    if (settings.target == '#crm-ajax-dialog') {
+      $('<div id="crm-ajax-dialog" class="crm-container"><div class="crm-loading-element">' + ts('Loading') + '...</div></div>').dialog(settings.dialogSettings);
+    }
+    $.getJSON(url, function(data) {
+      if (settings.target == '#crm-ajax-dialog' && !settings.dialogSettings.title && data.title) {
+        $(settings.target).dialog('option', 'title', data.title);
+      }
+      $(settings.target).html(data.content).trigger('crm' + settings.type + 'Load', data);
+      if (typeof(settings.onLoad) == 'function') {
+        settings.onLoad(data, url);
+      }
+    });
+  };
+
+  CRM.loadForm = function(url, options) {
+    options = options || {};
+    var settings = {
+      target: '#crm-ajax-dialog',
+      validate: true,
+      onLoad: null,
+      onCancel: function(e) {
+        $(settings.target).dialog('close');
+        return false;
+      },
+      onError: function(data) {
+        $(settings.target).html(data.content).trigger('crmFormReload', data);
+        if (typeof(data.errors) == 'object') {
+          $.each(data.errors, function(target, msg) {
+            $('[name="'+target+'"]').crmError(msg);
+          });
+        }
+        settings.onLoad(data, url);
+      },
+      onSuccess: function(data) {
+        $(settings.target).dialog('close');
+      }
+    };
+    $.extend(settings, options, {type: 'Form'});
+    settings.onLoad = function(data, url) {
+      $(".cancel.form-submit", settings.target).click(settings.onCancel);
+      if (settings.validate) {
+        $("form", settings.target).validate(CRM.validate.params);
+      }
+      $("form", settings.target).ajaxForm({
+        url: url,
+        dataType: 'json',
+        success: function(response) {
+          if (response.status == 'success') {
+           settings.onSuccess(response);
+          }
+          else {
+            settings.onError(response);
+          }
+        }
+      });
+      // Call original onLoad fn
+      if (typeof(options.onLoad) == 'function') {
+        options.onLoad(data, url);
+      }
+    };
+    CRM.loadPage(url, settings);
+  };
+
   // Preprocess all cj ajax calls to display messages
   $(document).ajaxSuccess(function(event, xhr, settings) {
     try {