1 // https://civicrm.org/licensing
3 var scriptLoaded
= false;
5 function getInstance(item
) {
6 var name
= $(item
).attr("name"),
7 id
= $(item
).attr("id");
8 if (name
&& window
.CKEDITOR
&& CKEDITOR
.instances
[name
]) {
9 return CKEDITOR
.instances
[name
];
11 if (id
&& window
.CKEDITOR
&& CKEDITOR
.instances
[id
]) {
12 return CKEDITOR
.instances
[id
];
16 function loadScript(url
) {
17 var deferred
= $.Deferred(),
18 script
= document
.createElement('script');
19 script
.onload = function() {deferred
.resolve();};
21 document
.getElementsByTagName("head")[0].appendChild(script
);
25 CRM
.wysiwyg
.supportsFileUploads
= true;
27 CRM
.wysiwyg
.create = function(item
) {
28 var deferred
= $.Deferred();
34 editor
.on('focus', function() {
35 $(item
).trigger('focus');
37 editor
.on('blur', function() {
38 editor
.updateElement();
39 $(item
).trigger("blur");
40 $(item
).trigger("change");
42 editor
.on('insertText', function() {
43 $(item
).trigger("keypress");
45 _
.each(['key', 'pasteState'], function(evName
) {
46 editor
.on(evName
, function(evt
) {
47 if (debounce
) clearTimeout(debounce
);
48 debounce
= setTimeout(function() {
49 editor
.updateElement();
50 $(item
).trigger("change");
54 editor
.on('pasteState', function() {
55 $(item
).trigger("paste");
57 // Hide CiviCRM menubar when editor is fullscreen
58 editor
.on('maximize', function (e
) {
59 $('#civicrm-menu').toggle(e
.data
=== 2);
64 function initialize() {
66 browseUrl
= CRM
.config
.resourceBase
+ "packages/kcfinder/browse.php?cms=civicrm",
67 uploadUrl
= CRM
.config
.resourceBase
+ "packages/kcfinder/upload.php?cms=civicrm",
68 preset
= $(item
).data('preset') || 'default',
69 // This variable is always an array but a legacy extension could be setting it as a string.
70 customConfig
= (typeof CRM
.config
.CKEditorCustomConfig
=== 'string') ? CRM
.config
.CKEditorCustomConfig
:
71 (CRM
.config
.CKEditorCustomConfig
[preset
] || CRM
.config
.CKEditorCustomConfig
.default);
73 $(item
).addClass('crm-wysiwyg-enabled');
75 CKEDITOR
.replace($(item
)[0], {
76 filebrowserBrowseUrl
: browseUrl
+ '&type=files',
77 filebrowserImageBrowseUrl
: browseUrl
+ '&type=images',
78 filebrowserFlashBrowseUrl
: browseUrl
+ '&type=flash',
79 filebrowserUploadUrl
: uploadUrl
+ '&type=files',
80 filebrowserImageUploadUrl
: uploadUrl
+ '&type=images',
81 filebrowserFlashUploadUrl
: uploadUrl
+ '&type=flash',
82 customConfig
: customConfig
,
84 instanceReady
: onReady
89 if ($(item
).hasClass('crm-wysiwyg-enabled')) {
92 else if ($(item
).length
) {
93 // Lazy-load ckeditor.js
94 if (window
.CKEDITOR
) {
97 if (scriptLoaded
=== false) {
98 scriptLoaded
= loadScript(CRM
.config
.resourceBase
+ 'bower_components/ckeditor/ckeditor.js');
100 scriptLoaded
.done(initialize
);
108 CRM
.wysiwyg
.destroy = function(item
) {
109 $(item
).removeClass('crm-wysiwyg-enabled');
110 var editor
= getInstance(item
);
116 CRM
.wysiwyg
.updateElement = function(item
) {
117 var editor
= getInstance(item
);
119 editor
.updateElement();
123 CRM
.wysiwyg
.getVal = function(item
) {
124 var editor
= getInstance(item
);
126 return editor
.getData();
128 return $(item
).val();
132 CRM
.wysiwyg
.setVal = function(item
, val
) {
133 var editor
= getInstance(item
);
135 return editor
.setData(val
);
137 return $(item
).val(val
);
141 CRM
.wysiwyg
.insert = function(item
, text
) {
142 var editor
= getInstance(item
);
144 editor
.insertText(text
);
146 CRM
.wysiwyg
._insertIntoTextarea(item
, text
);
150 CRM
.wysiwyg
.focus = function(item
) {
151 var editor
= getInstance(item
);