From: Coleman Watts Date: Fri, 22 Nov 2013 23:05:51 +0000 (-0800) Subject: CRM-10693 - LoadPage/Form improvements X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0e017a411c27dc7d8838b41cbdd1739a444afe9a;p=civicrm-core.git CRM-10693 - LoadPage/Form improvements --- diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 1ba8b8f326..a30dcb3c00 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -123,14 +123,18 @@ class CRM_Core_Page { self::$_session = CRM_Core_Session::singleton(); } - if (isset($_REQUEST['snippet']) && $_REQUEST['snippet']) { + // FIXME - why are we messing with 'snippet'? Why not just pass it directly into $this->_print? + if (!empty($_REQUEST['snippet'])) { if ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_PDF) { $this->_print = CRM_Core_Smarty::PRINT_PDF; } // FIXME - why does this number not match the constant? - else if ($_REQUEST['snippet'] == 5) { + elseif ($_REQUEST['snippet'] == 5) { $this->_print = CRM_Core_Smarty::PRINT_NOFORM; } + elseif ($_REQUEST['snippet'] == CRM_Core_Smarty::PRINT_JSON) { + $this->_print = CRM_Core_Smarty::PRINT_JSON; + } else { $this->_print = CRM_Core_Smarty::PRINT_SNIPPET; } @@ -164,7 +168,7 @@ class CRM_Core_Page { if ($this->_print) { if (in_array( $this->_print, array( CRM_Core_Smarty::PRINT_SNIPPET, - CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM ))) { + CRM_Core_Smarty::PRINT_PDF, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON ))) { $content = self::$_template->fetch('CRM/common/snippet.tpl'); } else { @@ -184,6 +188,9 @@ class CRM_Core_Page { array('paper_size' => 'a3', 'orientation' => 'landscape') ); } + elseif ($this->_print == CRM_Core_Smarty::PRINT_JSON) { + CRM_Core_Page_AJAX::returnJsonResponse($content); + } else { echo $content; } diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index fa720eb869..e39ac960eb 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -172,12 +172,15 @@ class CRM_Core_Page_AJAX { if (is_string($response)) { $response = array('content' => $response); } + // Add session variables to response $session = CRM_Core_Session::singleton(); $response += array( 'status' => 'success', 'userContext' => htmlspecialchars_decode($session->readUserContext()), + 'title' => CRM_Utils_System::$title, ); - // crmMessages will be automatically handled by our client-side ajax preprocessor @see Common.js + // crmMessages will be automatically handled by our ajax preprocessor + // @see js/Common.js if ($session->getStatus(FALSE)) { $response['crmMessages'] = $session->getStatus(TRUE); } diff --git a/CRM/Core/QuickForm/Action/Display.php b/CRM/Core/QuickForm/Action/Display.php index ad9cc91873..832b617d79 100644 --- a/CRM/Core/QuickForm/Action/Display.php +++ b/CRM/Core/QuickForm/Action/Display.php @@ -159,7 +159,6 @@ class CRM_Core_QuickForm_Action_Display extends CRM_Core_QuickForm_Action { if ($controller->_QFResponseType == 'json') { $response = array( 'content' => $html, - 'title' => CRM_Utils_System::$title, ); if (!empty($form['errors'])) { $response['status'] = 'form_error'; diff --git a/js/Common.js b/js/Common.js index 60738a3489..7387bb3724 100644 --- a/js/Common.js +++ b/js/Common.js @@ -820,11 +820,11 @@ CRM.validate = CRM.validate || { }); } + var dialogCount = 0; CRM.loadPage = function(url, options) { options = options || {}; - url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; var settings = { - target: '#crm-ajax-dialog', + target: '#crm-ajax-dialog-' + (dialogCount++), dialog: { modal: true, width: '65%', @@ -834,74 +834,91 @@ CRM.validate = CRM.validate || { $(this).remove(); } }, - onLoad: null, - type: 'Page', - url: url + autoClose: true, + onFailure: function(data, settings) { + if (settings.dialog !== false && settings.autoClose) { + $(this).dialog('close'); + } + CRM.alert(ts('Unable to reach the server. Please refresh this page in your browser and try again.'), ts('Network Error'), 'error'); + }, + type: 'Page' }; $.extend(true, settings, options); + // Add snippet argument to url + if (url.search(/[&?]snippet=/) < 0) { + url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=6'; + } + settings.url = url; // Create new dialog - if (settings.dialog && settings.target[0] == '#') { + if (settings.dialog !== false && settings.target[0] == '#') { $('
' + ts('Loading') + '...
').dialog(settings.dialog); } $.getJSON(url, function(data) { + if (typeof(data) != 'object' || typeof(data.content) != 'string') { + settings.onFailure.call($(settings.target), data, settings); + return; + } if (settings.dialog && !settings.dialog.title && data.title) { $(settings.target).dialog('option', 'title', data.title); } settings.content = data.content; $(settings.target).html(data.content).trigger('crm' + settings.type + 'Load', data); if (typeof(settings.onLoad) == 'function') { - settings.onLoad(data, settings); + settings.onLoad.call($(settings.target),data, settings); } }); + return $(settings.target); }; CRM.loadForm = function(url, options) { options = options || {}; var settings = { - target: '#crm-ajax-dialog', validate: true, - onLoad: null, - onCancel: function(e) { - if (settings.dialog !== false) { - $(settings.target).dialog('close'); - } + onCancel: function(event, settings) { return false; }, onError: function(data, settings) { - $(settings.target).html(data.content).trigger('crmFormReload', data); + $(this).html(data.content).trigger('crmFormReload', data); if (typeof(data.errors) == 'object') { - $.each(data.errors, function(target, msg) { - $('[name="'+target+'"]').crmError(msg); + $.each(data.errors, function(formElement, msg) { + $('[name="'+formElement+'"]').crmError(msg); }); } - settings.onLoad(data, settings); - }, - onSuccess: function(data, settings) { - if (settings.dialog !== false) { - $(settings.target).dialog('close'); - } + settings.onLoad.call($(settings.target), data, settings); } }; $.extend(settings, options, {type: 'Form'}); settings.onLoad = function(data, settings) { - $(".cancel.form-submit", settings.target).click(settings.onCancel); + $(".cancel.form-submit", this).click(function(event) { + var returnVal = settings.onCancel.call($(settings.target), event, settings); + if (settings.dialog !== false && settings.autoClose && returnVal !== true) { + $(settings.target).dialog('close'); + } + return returnVal === true ? returnVal : false; + }); if (settings.validate) { - $("form", settings.target).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); + $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); } - $("form", settings.target).ajaxForm({ + $("form", this).ajaxForm({ url: settings.url, dataType: 'json', success: function(response) { if (response.status == 'success') { - settings.onSuccess(response, settings); + if (typeof(settings.onSuccess) === 'function') { + settings.onSuccess.call($(settings.target), response, settings); + } $(settings.target).unblock().trigger('crmFormSuccess', data); + // Reset form for e.g. "save and new" if (settings.resetButton && response.buttonName == settings.resetButton) { $(settings.target).html(settings.content).trigger('crmFormLoad', data); - settings.onLoad(data, settings); + settings.onLoad.call($(settings.target), data, settings); + } + else if (settings.dialog !== false && settings.autoClose) { + $(settings.target).dialog('close'); } } else { - settings.onError(response, settings); + settings.onError.call($(settings.target), response, settings); } }, beforeSubmit: function() { @@ -910,10 +927,10 @@ CRM.validate = CRM.validate || { }); // Call original onLoad fn if (typeof(options.onLoad) == 'function') { - options.onLoad(data, settings); + options.onLoad.call($(settings.target), data, settings); } }; - CRM.loadPage(url, settings); + return CRM.loadPage(url, settings); }; // Preprocess all cj ajax calls to display messages diff --git a/js/view/crm.designer.js b/js/view/crm.designer.js index 51e1ec739b..f35d60b0b6 100644 --- a/js/view/crm.designer.js +++ b/js/view/crm.designer.js @@ -414,10 +414,7 @@ CRM.loadForm(url, { resetButton: 'next_new', onSuccess: function(data, settings) { - paletteView.doRefresh('custom_' + data.customField.id); - if (data.buttonName != 'next_new') { - $(settings.target).dialog('close'); - } + paletteView.doRefresh('custom_' + data.id); } }); return false;