crm.ckeditor.js code cleanup
authorColeman Watts <coleman@civicrm.org>
Sun, 18 Oct 2015 19:25:51 +0000 (15:25 -0400)
committerColeman Watts <coleman@civicrm.org>
Sun, 18 Oct 2015 19:26:40 +0000 (15:26 -0400)
js/wysiwyg/crm.ckeditor.js

index 6d6c3337632c92503ee595df9e6b8a937a32c5ea..ff0126693d21452dbf21604dd38f496cfe730070 100644 (file)
@@ -1,5 +1,6 @@
 // https://civicrm.org/licensing
 (function($, _) {
+
   function getInstance(item) {
     var name = $(item).attr("name"),
       id = $(item).attr("id");
       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);
       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) {
       return $(item).val();
     }
   };
+
   CRM.wysiwyg.setVal = function(item, val) {
     var editor = getInstance(item);
     if (editor) {
       return $(item).val(val);
     }
   };
+
   CRM.wysiwyg.insert = function(item, text) {
     var editor = getInstance(item);
     if (editor) {
       CRM.wysiwyg._insertIntoTextarea(item, text);
     }
   };
+  
   CRM.wysiwyg.focus = function(item) {
     var editor = getInstance(item);
     if (editor) {