From c62731c32b965b0afb8a796a2a643c1a9d05de74 Mon Sep 17 00:00:00 2001 From: Tyrell Cook Date: Mon, 27 Apr 2015 16:56:06 -0400 Subject: [PATCH] CRM-16354 fixed issue with angular --- ang/crmUi.js | 51 ++++++++------------------------------ js/wysiwyg/crm.ckeditor.js | 38 ++++++++++++++++++++++------ js/wysiwyg/crm.wysiwyg.js | 28 +++++++++++++++++---- 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/ang/crmUi.js b/ang/crmUi.js index de2e078246..952e75c168 100644 --- a/ang/crmUi.js +++ b/ang/crmUi.js @@ -360,26 +360,9 @@ 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(); }); } }; @@ -391,47 +374,35 @@ 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); }; } }; diff --git a/js/wysiwyg/crm.ckeditor.js b/js/wysiwyg/crm.ckeditor.js index 05a69d5097..ea4eafe16c 100644 --- a/js/wysiwyg/crm.ckeditor.js +++ b/js/wysiwyg/crm.ckeditor.js @@ -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]); @@ -18,9 +22,16 @@ 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(); @@ -43,16 +54,27 @@ 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._); diff --git a/js/wysiwyg/crm.wysiwyg.js b/js/wysiwyg/crm.wysiwyg.js index 28102538a2..2bfee62284 100644 --- a/js/wysiwyg/crm.wysiwyg.js +++ b/js/wysiwyg/crm.wysiwyg.js @@ -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()); }); @@ -15,11 +14,29 @@ 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._); -- 2.25.1