From d4e4e9df242438230ccbc8ac8535cdb70e241945 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 17 Nov 2013 08:56:54 -0800 Subject: [PATCH] CRM-10693 - Ajax - Add helpers CRM.loadPage and CRM.loadForm --- CRM/Core/Controller.php | 2 +- CRM/Core/QuickForm/Action/Display.php | 5 +- js/Common.js | 80 +++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/CRM/Core/Controller.php b/CRM/Core/Controller.php index 21a4ac461b..7bf8d8c7c6 100644 --- a/CRM/Core/Controller.php +++ b/CRM/Core/Controller.php @@ -53,7 +53,7 @@ class CRM_Core_Controller extends HTML_QuickForm_Controller { * * @var string */ - protected $_title; + public $_title; /** * The key associated with this controller diff --git a/CRM/Core/QuickForm/Action/Display.php b/CRM/Core/QuickForm/Action/Display.php index 14c76669c6..7302b26d24 100644 --- a/CRM/Core/QuickForm/Action/Display.php +++ b/CRM/Core/QuickForm/Action/Display.php @@ -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']; diff --git a/js/Common.js b/js/Common.js index 21dca3559e..cd1cda4ec7 100644 --- a/js/Common.js +++ b/js/Common.js @@ -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') { + $('
' + ts('Loading') + '...
').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 { -- 2.25.1