From 286a7e5ad60dbeb6ca15687f42079ab0a6f48af1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 6 Dec 2016 18:19:14 -0500 Subject: [PATCH] Wysiwyg js optimizations --- CRM/Core/Resources.php | 8 ++++++-- js/Common.js | 14 ++++++++++++++ js/wysiwyg/crm.ckeditor.js | 17 ++--------------- js/wysiwyg/crm.wysiwyg.js | 17 ++++++++++++++--- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index d998717d43..847ae601d2 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -727,9 +727,13 @@ class CRM_Core_Resources { // add wysiwyg editor $editor = Civi::settings()->get('editor_id'); if ($editor == "CKEditor") { - $items[] = "js/wysiwyg/crm.ckeditor.js"; CRM_Admin_Page_CKEditorConfig::setConfigDefault(); - $items[] = array('config' => array('CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig::getConfigUrl())); + $items[] = array( + 'config' => array( + 'wysisygScriptLocation' => Civi::paths()->getUrl("[civicrm.root]/js/wysiwyg/crm.ckeditor.js"), + 'CKEditorCustomConfig' => CRM_Admin_Page_CKEditorConfig::getConfigUrl(), + ), + ); } // These scripts are only needed by back-office users diff --git a/js/Common.js b/js/Common.js index 5969aa270e..43383ddf86 100644 --- a/js/Common.js +++ b/js/Common.js @@ -237,6 +237,20 @@ if (!CRM.vars) CRM.vars = {}; } }; + var scriptsLoaded = {}; + CRM.loadScript = function(url) { + if (!scriptsLoaded[url]) { + var script = document.createElement('script'); + scriptsLoaded[url] = $.Deferred(); + script.onload = function () { + scriptsLoaded[url].resolve(); + }; + script.src = url; + document.getElementsByTagName("head")[0].appendChild(script); + } + return scriptsLoaded[url]; + }; + /** * Populate a select list, overwriting the existing options except for the placeholder. * @param select jquery selector - 1 or more select elements diff --git a/js/wysiwyg/crm.ckeditor.js b/js/wysiwyg/crm.ckeditor.js index 422ee5711a..21446ee482 100644 --- a/js/wysiwyg/crm.ckeditor.js +++ b/js/wysiwyg/crm.ckeditor.js @@ -1,6 +1,5 @@ // https://civicrm.org/licensing (function($, _) { - var scriptLoaded = false; function getInstance(item) { var name = $(item).attr("name"), @@ -13,18 +12,9 @@ } } - function loadScript(url) { - var deferred = $.Deferred(), - script = document.createElement('script'); - script.onload = function() {deferred.resolve();}; - script.src = url; - document.getElementsByTagName("head")[0].appendChild(script); - return deferred; - } - CRM.wysiwyg.supportsFileUploads = true; - CRM.wysiwyg.create = function(item) { + CRM.wysiwyg._create = function(item) { var deferred = $.Deferred(); function onReady() { @@ -94,10 +84,7 @@ if (window.CKEDITOR) { initialize(); } else { - if (scriptLoaded === false) { - scriptLoaded = loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js'); - } - scriptLoaded.done(initialize); + CRM.loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js').done(initialize); } } else { deferred.reject(); diff --git a/js/wysiwyg/crm.wysiwyg.js b/js/wysiwyg/crm.wysiwyg.js index 54c86e8145..d61bcdea82 100644 --- a/js/wysiwyg/crm.wysiwyg.js +++ b/js/wysiwyg/crm.wysiwyg.js @@ -3,9 +3,20 @@ // This defines an interface which by default only handles plain textareas // A wysiwyg implementation can extend this by overriding as many of these functions as needed CRM.wysiwyg = { - supportsFileUploads: false, - create: function() { - return $.Deferred().resolve(); + supportsFileUploads: !!CRM.config.wysisygScriptLocation, + create: function(item) { + var ret = $.Deferred(); + // Lazy-load the wysiwyg js + if (CRM.config.wysisygScriptLocation) { + CRM.loadScript(CRM.config.wysisygScriptLocation).done(function() { + CRM.wysiwyg._create(item).done(function() { + ret.resolve(); + }); + }); + } else { + ret.resolve(); + } + return ret; }, destroy: _.noop, updateElement: _.noop, -- 2.25.1