1 // https://civicrm.org/licensing
4 function getInstance(item
) {
5 var name
= $(item
).attr("name"),
6 id
= $(item
).attr("id");
7 if (name
&& window
.CKEDITOR
&& CKEDITOR
.instances
[name
]) {
8 return CKEDITOR
.instances
[name
];
10 if (id
&& window
.CKEDITOR
&& CKEDITOR
.instances
[id
]) {
11 return CKEDITOR
.instances
[id
];
15 CRM
.wysiwyg
.supportsFileUploads
= true;
17 CRM
.wysiwyg
._create = function(item
) {
18 var deferred
= $.Deferred();
24 editor
.on('focus', function() {
25 $(item
).trigger('focus');
27 editor
.on('blur', function() {
28 editor
.updateElement();
29 $(item
).trigger("blur");
30 $(item
).trigger("change");
32 editor
.on('insertText', function() {
33 $(item
).trigger("keypress");
35 _
.each(['key', 'pasteState'], function(evName
) {
36 editor
.on(evName
, function(evt
) {
37 if (debounce
) clearTimeout(debounce
);
38 debounce
= setTimeout(function() {
39 editor
.updateElement();
40 $(item
).trigger("change");
44 editor
.on('pasteState', function() {
45 $(item
).trigger("paste");
47 // Hide CiviCRM menubar when editor is fullscreen
48 editor
.on('maximize', function (e
) {
49 $('#civicrm-menu').toggle(e
.data
=== 2);
54 function initialize() {
56 browseUrl
= CRM
.config
.resourceBase
+ "packages/kcfinder/browse.php?cms=civicrm",
57 uploadUrl
= CRM
.config
.resourceBase
+ "packages/kcfinder/upload.php?cms=civicrm",
58 preset
= $(item
).data('preset') || 'default',
59 // This variable is always an array but a legacy extension could be setting it as a string.
60 customConfig
= (typeof CRM
.config
.CKEditorCustomConfig
=== 'string') ? CRM
.config
.CKEditorCustomConfig
:
61 (CRM
.config
.CKEditorCustomConfig
[preset
] || CRM
.config
.CKEditorCustomConfig
.default);
63 $(item
).addClass('crm-wysiwyg-enabled');
65 CKEDITOR
.replace($(item
)[0], {
66 filebrowserBrowseUrl
: browseUrl
+ '&type=files',
67 filebrowserImageBrowseUrl
: browseUrl
+ '&type=images',
68 filebrowserFlashBrowseUrl
: browseUrl
+ '&type=flash',
69 filebrowserUploadUrl
: uploadUrl
+ '&type=files',
70 filebrowserImageUploadUrl
: uploadUrl
+ '&type=images',
71 filebrowserFlashUploadUrl
: uploadUrl
+ '&type=flash',
72 customConfig
: customConfig
,
74 instanceReady
: onReady
79 if ($(item
).hasClass('crm-wysiwyg-enabled')) {
82 else if ($(item
).length
) {
83 // Lazy-load ckeditor.js
84 if (window
.CKEDITOR
) {
87 CRM
.loadScript(CRM
.config
.resourceBase
+ 'bower_components/ckeditor/ckeditor.js').done(initialize
);
95 CRM
.wysiwyg
.destroy = function(item
) {
96 $(item
).removeClass('crm-wysiwyg-enabled');
97 var editor
= getInstance(item
);
103 CRM
.wysiwyg
.updateElement = function(item
) {
104 var editor
= getInstance(item
);
106 editor
.updateElement();
110 CRM
.wysiwyg
.getVal = function(item
) {
111 var editor
= getInstance(item
);
113 return editor
.getData();
115 return $(item
).val();
119 CRM
.wysiwyg
.setVal = function(item
, val
) {
120 var editor
= getInstance(item
);
122 return editor
.setData(val
);
124 return $(item
).val(val
);
128 CRM
.wysiwyg
.insert = function(item
, text
) {
129 var editor
= getInstance(item
);
131 editor
.insertText(text
);
133 CRM
.wysiwyg
._insertIntoTextarea(item
, text
);
137 CRM
.wysiwyg
.focus = function(item
) {
138 var editor
= getInstance(item
);