From 3d527838d4b2f41ad31f3d42501d724584fbdd36 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 6 Jul 2014 18:35:03 +0100 Subject: [PATCH] CRM-14949 - Move localization strings into a dynamic js tpl --- CRM/Admin/Page/AJAX.php | 19 ++----- CRM/Core/Page/AJAX.php | 21 ++++++++ CRM/Core/Resources.php | 44 ++++++---------- CRM/Core/xml/Menu/Misc.xml | 4 ++ js/Common.js | 22 ++++++-- js/crm.ajax.js | 2 +- templates/CRM/Form/validate.js.tpl | 85 ++++++++++++++++++++++++++++++ templates/CRM/Form/validate.tpl | 71 ++++--------------------- 8 files changed, 159 insertions(+), 109 deletions(-) create mode 100644 templates/CRM/Form/validate.js.tpl diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index d036198457..844f614417 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -43,22 +43,11 @@ class CRM_Admin_Page_AJAX { * @see smarty_function_crmNavigationMenu */ static function getNavigationMenu() { - $session = CRM_Core_Session::singleton(); - $contactID = $session->get('userID'); + $contactID = CRM_Core_Session::singleton()->get('userID'); if ($contactID) { - // Set headers to encourage browsers to cache for a long time - // If we want to refresh the menu we will send a different url - $year = 60*60*24*364; - header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); - header('Content-Type: application/javascript'); - header("Cache-Control: max-age=$year, public"); - - // Render template as a javascript file - $smarty = CRM_Core_Smarty::singleton(); - $navigation = CRM_Core_BAO_Navigation::createNavigation($contactID); - $smarty->assign('timeGenerated', date('d M Y H:i:s')); - $smarty->assign('navigation', $navigation); - print $smarty->fetch('CRM/common/navigation.js.tpl'); + CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/navigation.js.tpl', array( + 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID), + )); } CRM_Utils_System::civiExit(); } diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index 593beb5f3c..dbbd257cb0 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -199,6 +199,27 @@ class CRM_Core_Page_AJAX { CRM_Utils_System::civiExit(); } + /** + * Render and output a template as a javascript file + * @param string $tplFile + * @param array $vars - template variables + */ + static function returnDynamicJS($tplFile, $vars = array()) { + // Set headers to encourage browsers to cache for a long time + $year = 60*60*24*364; + header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $year)); + header('Content-Type: application/javascript'); + header("Cache-Control: max-age=$year, public"); + + $smarty = CRM_Core_Smarty::singleton(); + $vars += array('timeGenerated' => date('d M Y H:i:s')); + foreach ($vars as $name => $val) { + $smarty->assign($name, $val); + } + print $smarty->fetch($tplFile); + CRM_Utils_System::civiExit(); + } + /** * Send autocomplete results to the client. Input can be a simple or nested array. * @param array $results - If nested array, also provide: diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 9064f1a54d..66332cf1af 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -467,14 +467,6 @@ class CRM_Core_Resources { } } - // Initialize CRM.url and CRM.formatMoney - $url = CRM_Utils_System::url('civicrm/example', 'placeholder', FALSE, NULL, FALSE); - $js = "CRM.url('init', '$url');\n"; - $js .= "CRM.formatMoney('init', " . json_encode(CRM_Utils_Money::format(1234.56)) . ");"; - - $this->addLocalization($js); - $this->addScript($js, $jsWeight++, $region); - // Add global settings $settings = array( 'userFramework' => $config->userFramework, @@ -484,7 +476,15 @@ class CRM_Core_Resources { ); $this->addSetting(array('config' => $settings)); - // Give control of jQuery back to the CMS - this loads last + // Contact create profiles with localized names + if (CRM_Core_Permission::check('edit all contacts') || CRM_Core_Permission::check('add contacts')) { + $this->addSetting(array('profile' => array('contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks()))); + } + + // Add dynamic localization script + $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/localizationjs/' . $config->lcMessages), $jsWeight++, $region); + + // Give control of jQuery and _ back to the CMS - this loads last $this->addScriptFile('civicrm', 'js/noconflict.js', 9999, $region, FALSE); $this->addCoreStyles($region); @@ -551,26 +551,16 @@ class CRM_Core_Resources { } /** - * Add inline scripts needed to localize js widgets - * @param string $js + * Callback to add dymanic script for localizing js widgets */ - function addLocalization(&$js) { + static function outputLocalizationJS() { $config = CRM_Core_Config::singleton(); - - // Localize select2 strings - $contactSearch = json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')); - $otherSearch = json_encode(ts('Enter search term...')); - $js .= " - $.fn.select2.defaults.formatNoMatches = " . json_encode(ts("None found.")) . "; - $.fn.select2.defaults.formatLoadMore = " . json_encode(ts("Loading...")) . "; - $.fn.select2.defaults.formatSearching = " . json_encode(ts("Searching...")) . "; - $.fn.select2.defaults.formatInputTooShort = function(){return CRM.$(this).data('api-entity') == 'contact' ? $contactSearch : $otherSearch}; - "; - - // Contact create profiles with localized names - if (CRM_Core_Permission::check('edit all contacts') || CRM_Core_Permission::check('add contacts')) { - $this->addSetting(array('profile' => array('contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks()))); - } + $vars = array( + 'moneyFormat' => json_encode(CRM_Utils_Money::format(1234.56)), + 'contactSearch' => json_encode($config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...')), + 'otherSearch' => json_encode(ts('Enter search term...')), + ); + CRM_Core_Page_AJAX::returnDynamicJS('CRM/Form/validate.js.tpl', $vars); } /** diff --git a/CRM/Core/xml/Menu/Misc.xml b/CRM/Core/xml/Menu/Misc.xml index e1f4a1d11d..020ff717f5 100644 --- a/CRM/Core/xml/Menu/Misc.xml +++ b/CRM/Core/xml/Menu/Misc.xml @@ -175,4 +175,8 @@ CRM_Core_Page_Angular access CiviCRM + + civicrm/ajax/localizationjs + CRM_Core_Resources::outputLocalizationJS + diff --git a/js/Common.js b/js/Common.js index fa3c910722..3f8beae757 100644 --- a/js/Common.js +++ b/js/Common.js @@ -195,10 +195,6 @@ function showHideRow(index) { CRM.utils = CRM.utils || {}; CRM.strings = CRM.strings || {}; -CRM.validate = CRM.validate || { - params: {}, - functions: [] -}; (function ($, _, undefined) { "use strict"; @@ -481,6 +477,24 @@ CRM.validate = CRM.validate || { return markup; } + /** + * Wrapper for jQuery validate initialization function; supplies defaults + * @param options object + */ + $.fn.crmValidate = function(params) { + return $(this).each(function () { + var that = this, + settings = $.extend({}, CRM.validate._defaults, CRM.validate.params); + $(this).validate(settings); + // Call any post-initialization callbacks + if (CRM.validate.functions && CRM.validate.functions.length) { + $.each(CRM.validate.functions, function(i, func) { + func.call(that); + }); + } + }); + } + // Initialize widgets $(document) .on('crmLoad', function(e) { diff --git a/js/crm.ajax.js b/js/crm.ajax.js index a3e9932e6e..b647e370c8 100644 --- a/js/crm.ajax.js +++ b/js/crm.ajax.js @@ -380,7 +380,7 @@ } }); if (settings.validate) { - $("form", this).validate(typeof(settings.validate) == 'object' ? settings.validate : CRM.validate.params); + $("form", this).crmValidate(); } $("form:not('[data-no-ajax-submit=true]')", this).ajaxForm($.extend({ url: data.url.replace(/reset=1[&]?/, ''), diff --git a/templates/CRM/Form/validate.js.tpl b/templates/CRM/Form/validate.js.tpl new file mode 100644 index 0000000000..0fde36f85f --- /dev/null +++ b/templates/CRM/Form/validate.js.tpl @@ -0,0 +1,85 @@ + {* + +--------------------------------------------------------------------+ + | CiviCRM version 4.5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2014 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ +*}// http://civicrm.org/licensing +// {/literal} - {if $form && $form.formName} - $("#{$form.formName}").validate(params); - {literal} - // Call any post-initialization callbacks - if (CRM.validate && CRM.validate.functions.length) { - $.each(CRM.validate.functions, function(i, func) { - func(); - }); - } - {/literal} - {/if} -}); - {/if} -- 2.25.1