CRM-16354 fixed issue with angular
authorTyrell Cook <tyrell@aghstrategies.com>
Mon, 27 Apr 2015 20:56:06 +0000 (16:56 -0400)
committerTyrell Cook <tyrell@aghstrategies.com>
Mon, 27 Apr 2015 20:56:06 +0000 (16:56 -0400)
ang/crmUi.js
js/wysiwyg/crm.ckeditor.js
js/wysiwyg/crm.wysiwyg.js

index de2e078246463eebb641ba66f27c32dbda3585d1..952e75c168b34216a2bac0302fe6771a4cde5d9f 100644 (file)
       return {
         link: function(scope, element, attrs) {
           scope.$on(attrs.crmUiInsertRx, function(e, tokenName) {
-            var id = element.attr('id');
-            if (CKEDITOR.instances[id]) {
-              CKEDITOR.instances[id].insertText(tokenName);
-              $(element).select2('close').select2('val', '');
-              CKEDITOR.instances[id].focus();
-            }
-            else {
-              var crmForEl = $('#' + id);
-              var origVal = crmForEl.val();
-              var origPos = crmForEl[0].selectionStart;
-              var newVal = origVal.substring(0, origPos) + tokenName + origVal.substring(origPos, origVal.length);
-              crmForEl.val(newVal);
-              var newPos = (origPos + tokenName.length);
-              crmForEl[0].selectionStart = newPos;
-              crmForEl[0].selectionEnd = newPos;
-
-              $(element).select2('close').select2('val', '');
-              crmForEl.triggerHandler('change');
-              crmForEl.focus();
-            }
+            CRM.wysiwyg.insert(element, tokenName);
+            $(element).select2('close').select2('val', '');
+            CRM.wysiwyg.focus();
           });
         }
       };
       return {
         require: '?ngModel',
         link: function (scope, elm, attr, ngModel) {
-          var ck = CKEDITOR.replace(elm[0]);
-
-          if (ck) {
-            _.extend(ck.config, {
-              width: '94%',
-              height: '400',
-              filebrowserBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=files',
-              filebrowserImageBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=images',
-              filebrowserFlashBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=flash',
-              filebrowserUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=files',
-              filebrowserImageUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=images',
-              filebrowserFlashUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=flash',
-            });
-          }
 
+          var editor = CRM.wysiwyg.create(elm);
           if (!ngModel) {
             return;
           }
 
+
           if (attr.ngBlur) {
-            ck.on('blur', function(){
+            $(elm).on('blur', function(){
               $timeout(function(){
                 scope.$eval(attr.ngBlur);
               });
             });
           }
 
-          ck.on('pasteState', function () {
+          $(elm).on('paste', function () {
             scope.$apply(function () {
-              ngModel.$setViewValue(ck.getData());
+              ngModel.$setViewValue(CRM.wysiwyg.getVal(elm));
             });
           });
 
-          ck.on('insertText', function () {
+          $(elm).on('keypress', function () {
             $timeout(function () {
-              ngModel.$setViewValue(ck.getData());
+              ngModel.$setViewValue(CRM.wysiwyg.getVal(elm));
             });
           });
 
           ngModel.$render = function (value) {
-            ck.setData(ngModel.$viewValue);
+            CRM.wysiwyg.setVal(elm, ngModel.$viewValue);
           };
         }
       };
index 05a69d5097b490c0d192a303298a21ac857afcfa..ea4eafe16c0e4b4b7d27519184de3b0b0e9a85e8 100644 (file)
@@ -2,12 +2,16 @@
 (function($, _) {
   function getInstance(item) {
     var name = $(item).attr("name");
-    return CKEDITOR.instances[name];
+    var id = $(item).attr("id");
+    if (name && CKEDITOR.instances[name]) {
+      return CKEDITOR.instances[name];
+    }
+    if (id && CKEDITOR.instances[id]) {
+      return CKEDITOR.instances[id];
+    }
   }
-
   CRM.wysiwyg.supportsFileUploads =  true;
   CRM.wysiwyg.create =  function(item) {
-    //var browseUrl = CRM.config.userFrameworkResourceUrl + "packages/kcfinder/browse.php";
     var browseUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/browse.php";
     var uploadUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/upload.php";
     var editor = CKEDITOR.replace($(item)[0]);
       editor.config.filebrowserUploadUrl = uploadUrl+'?cms=civicrm&type=files';
       editor.config.filebrowserImageUploadUrl = uploadUrl+'?cms=civicrm&type=images';
       editor.config.filebrowserFlashUploadUrl = uploadUrl+'?cms=civicrm&type=flash';
-      editor.on('blur', function(){
+      editor.on('blur', function() {
+        editor.updateElement();
         $(item).trigger("blur");
       });
+      editor.on('insertText', function() {
+        $(item).trigger("keypress");
+      });
+      editor.on('pasteState', function() {
+        $(item).trigger("paste");
+      });
     }
   };
   CRM.wysiwyg.destroy = function(item) {
@@ -35,7 +46,7 @@
       editor.updateElement();
     }
   };
-  CRM.wysiwyg.val = function(item) {
+  CRM.wysiwyg.getVal = function(item) {
     var editor = getInstance(item);
     if (editor) {
       return editor.getData();
       return $(item).val();
     }
   };
-  CRM.wysiwyg.insertText = function(item, text) {
+  CRM.wysiwyg.setVal = function(item, val) {
+    var editor = getInstance(item);
+    if (editor) {
+      return editor.setData(val);
+    } else {
+      return $(item).val(val);
+    }
+  };
+  CRM.wysiwyg.insert = function(item, text) {
     var editor = getInstance(item);
     if (editor) {
       editor.insertText(text);
+    } else {
+      CRM.wysiwyg.insertIntoTextarea(item, text);
     }
   };
-  CRM.wysiwyg.insertHTML = function(item, html) {
+  CRM.wysiwyg.focus = function(item) {
     var editor = getInstance(item);
     if (editor) {
-      editor.insertText(html);
+      editor.focus();
     }
   };
+
 })(CRM.$, CRM._);
index 28102538a23ce2d5c9ae0545bf33df106d5ad70a..2bfee6228464e488fc3071fafa11fd78b69a28a3 100644 (file)
@@ -4,8 +4,7 @@
     $(item).show();
     $(item).next('.replace-plain').hide();
     CRM.wysiwyg.create(item);
-    $(item).on( 'blur', function( e ) {
-      CRM.wysiwyg.updateElement(item);
+    $(item).on('blur', function() {
       CRM.wysiwyg.destroy(item);
       $(item).hide().next('.replace-plain').show().html($(item).val());
     });
   CRM.wysiwyg.create = _.noop;
   CRM.wysiwyg.destroy = _.noop;
   CRM.wysiwyg.updateElement = _.noop;
-  CRM.wysiwyg.val = function(item) {
+  CRM.wysiwyg.getVal = function(item) {
     return $(item).val();
   };
-  CRM.wysiwyg.insertText = _.noop;
-  CRM.wysiwyg.insertHTML = _.noop;
+  CRM.wysiwyg.setVal = function(item, val) {
+    return $(item).val(val);
+  };
+  CRM.wysiwyg.insert = function(item, text) {
+    CRM.wysiwyg.insertIntoTextarea(item, text);
+  };
+  CRM.wysiwyg.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');
+    CRM.wysiwyg.focus(item);
+  };
+  CRM.wysiwyg.focus = function(item) {
+    $(item).focus();
+  };
   CRM.wysiwyg.createPlain = function(item) {
     $(item)
       .hide()
@@ -31,4 +48,5 @@
       openWysiwyg(item);
     });
   };
+
 })(CRM.$, CRM._);