Merge pull request #14584 from seamuslee001/contact_group_cache_backend_convert
[civicrm-core.git] / js / wysiwyg / crm.wysiwyg.js
index 54c86e81454c1e0a633413fcf5931f8ac6ba7278..41adc35d4e74bf73899113780554858ed807951d 100644 (file)
@@ -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,
     },
     // Fallback function to use when a wysiwyg has not been initialized
     _insertIntoTextarea: function(item, text) {
-      var origVal = $(item).val();
-      var origPos = item[0].selectionStart;
-      var newVal = origVal + text;
-      $(item).val(newVal);
-      var newPos = (origPos + text.length);
-      item[0].selectionStart = newPos;
-      item[0].selectionEnd = newPos;
-      $(item).triggerHandler('change');
+      var itemObj = $(item);
+      var origVal = itemObj.val();
+      var origStart = itemObj[0].selectionStart;
+      var origEnd = itemObj[0].selectionEnd;
+      var newVal = origVal.substring(0, origStart) + text + origVal.substring(origEnd);
+      itemObj.val(newVal);
+      var newPos = (origStart + text.length);
+      itemObj[0].selectionStart = newPos;
+      itemObj[0].selectionEnd = newPos;
+      itemObj.triggerHandler('change');
       CRM.wysiwyg.focus(item);
     },
     // Create a "collapsed" textarea that expands into a wysiwyg when clicked
@@ -41,6 +54,9 @@
           CRM.wysiwyg.destroy(item);
           $(item).hide().next('.replace-plain').show().html($(item).val());
         })
+        .on('change', function() {
+          $(this).next('.replace-plain').html($(this).val());
+        })
         .after('<div class="replace-plain" tabindex="0"></div>');
       $(item).next('.replace-plain')
         .attr('title', ts('Click to edit'))