From 183473a2d19a739738b63edf2e193ba3df7f8eba Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 18 Oct 2015 15:25:51 -0400 Subject: [PATCH] crm.ckeditor.js code cleanup --- js/wysiwyg/crm.ckeditor.js | 61 ++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/js/wysiwyg/crm.ckeditor.js b/js/wysiwyg/crm.ckeditor.js index 6d6c333763..ff0126693d 100644 --- a/js/wysiwyg/crm.ckeditor.js +++ b/js/wysiwyg/crm.ckeditor.js @@ -1,5 +1,6 @@ // https://civicrm.org/licensing (function($, _) { + function getInstance(item) { var name = $(item).attr("name"), id = $(item).attr("id"); @@ -10,29 +11,16 @@ return CKEDITOR.instances[id]; } } + CRM.wysiwyg.supportsFileUploads = true; + CRM.wysiwyg.create = function(item) { - var editor, - deferred = $.Deferred(), - browseUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/browse.php?cms=civicrm", - uploadUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/upload.php?cms=civicrm"; - if ($(item).length) { - editor = CKEDITOR.replace($(item)[0], { - filebrowserBrowseUrl: browseUrl + '&type=files', - filebrowserImageBrowseUrl: browseUrl + '&type=images', - filebrowserFlashBrowseUrl: browseUrl + '&type=flash', - filebrowserUploadUrl: uploadUrl + '&type=files', - filebrowserImageUploadUrl: uploadUrl + '&type=images', - filebrowserFlashUploadUrl: uploadUrl + '&type=flash', - customConfig: CRM.config.CKEditorCustomConfig, - on: { - instanceReady: function() { - deferred.resolve(); - } - } - }); - } - if (editor) { + var deferred = $.Deferred(); + + function onReady() { + var debounce, + editor = this; + editor.on('focus', function() { $(item).trigger('focus'); }); @@ -44,7 +32,6 @@ editor.on('insertText', function() { $(item).trigger("keypress"); }); - var debounce = null; _.each(['key', 'pasteState'], function(evName) { editor.on(evName, function(evt) { if (debounce) clearTimeout(debounce); @@ -61,23 +48,50 @@ editor.on('maximize', function (e) { $('#civicrm-menu').toggle(e.data === 2); }); + deferred.resolve(); + } + + function initialize() { + var + browseUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/browse.php?cms=civicrm", + uploadUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/upload.php?cms=civicrm"; + + CKEDITOR.replace($(item)[0], { + filebrowserBrowseUrl: browseUrl + '&type=files', + filebrowserImageBrowseUrl: browseUrl + '&type=images', + filebrowserFlashBrowseUrl: browseUrl + '&type=flash', + filebrowserUploadUrl: uploadUrl + '&type=files', + filebrowserImageUploadUrl: uploadUrl + '&type=images', + filebrowserFlashUploadUrl: uploadUrl + '&type=flash', + customConfig: CRM.config.CKEditorCustomConfig, + on: { + instanceReady: onReady + } + }); + } + + if ($(item).length) { + initialize(); } else { deferred.reject(); } return deferred; }; + CRM.wysiwyg.destroy = function(item) { var editor = getInstance(item); if (editor) { editor.destroy(); } }; + CRM.wysiwyg.updateElement = function(item) { var editor = getInstance(item); if (editor) { editor.updateElement(); } }; + CRM.wysiwyg.getVal = function(item) { var editor = getInstance(item); if (editor) { @@ -86,6 +100,7 @@ return $(item).val(); } }; + CRM.wysiwyg.setVal = function(item, val) { var editor = getInstance(item); if (editor) { @@ -94,6 +109,7 @@ return $(item).val(val); } }; + CRM.wysiwyg.insert = function(item, text) { var editor = getInstance(item); if (editor) { @@ -102,6 +118,7 @@ CRM.wysiwyg._insertIntoTextarea(item, text); } }; + CRM.wysiwyg.focus = function(item) { var editor = getInstance(item); if (editor) { -- 2.25.1