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);
};
}
};
(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) {
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._);
$(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()
openWysiwyg(item);
});
};
+
})(CRM.$, CRM._);