Merge pull request #10009 from seanmadsen/CRM-20046
[civicrm-core.git] / js / wysiwyg / crm.ckeditor.js
1 // https://civicrm.org/licensing
2 (function($, _) {
3
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];
9 }
10 if (id && window.CKEDITOR && CKEDITOR.instances[id]) {
11 return CKEDITOR.instances[id];
12 }
13 }
14
15 CRM.wysiwyg.supportsFileUploads = true;
16
17 CRM.wysiwyg._create = function(item) {
18 var deferred = $.Deferred();
19
20 function onReady() {
21 var debounce,
22 editor = this;
23
24 editor.on('focus', function() {
25 $(item).trigger('focus');
26 });
27 editor.on('blur', function() {
28 editor.updateElement();
29 $(item).trigger("blur");
30 $(item).trigger("change");
31 });
32 editor.on('insertText', function() {
33 $(item).trigger("keypress");
34 });
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");
41 }, 50);
42 });
43 });
44 editor.on('pasteState', function() {
45 $(item).trigger("paste");
46 });
47 // Hide CiviCRM menubar when editor is fullscreen
48 editor.on('maximize', function (e) {
49 $('#civicrm-menu').toggle(e.data === 2);
50 });
51 deferred.resolve();
52 }
53
54 function initialize() {
55 var
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);
62
63 $(item).addClass('crm-wysiwyg-enabled');
64
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,
73 on: {
74 instanceReady: onReady
75 }
76 });
77 }
78
79 if ($(item).hasClass('crm-wysiwyg-enabled')) {
80 deferred.resolve();
81 }
82 else if ($(item).length) {
83 // Lazy-load ckeditor.js
84 if (window.CKEDITOR) {
85 initialize();
86 } else {
87 CRM.loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js').done(initialize);
88 }
89 } else {
90 deferred.reject();
91 }
92 return deferred;
93 };
94
95 CRM.wysiwyg.destroy = function(item) {
96 $(item).removeClass('crm-wysiwyg-enabled');
97 var editor = getInstance(item);
98 if (editor) {
99 editor.destroy();
100 }
101 };
102
103 CRM.wysiwyg.updateElement = function(item) {
104 var editor = getInstance(item);
105 if (editor) {
106 editor.updateElement();
107 }
108 };
109
110 CRM.wysiwyg.getVal = function(item) {
111 var editor = getInstance(item);
112 if (editor) {
113 return editor.getData();
114 } else {
115 return $(item).val();
116 }
117 };
118
119 CRM.wysiwyg.setVal = function(item, val) {
120 var editor = getInstance(item);
121 if (editor) {
122 return editor.setData(val);
123 } else {
124 return $(item).val(val);
125 }
126 };
127
128 CRM.wysiwyg.insert = function(item, text) {
129 var editor = getInstance(item);
130 if (editor) {
131 editor.insertText(text);
132 } else {
133 CRM.wysiwyg._insertIntoTextarea(item, text);
134 }
135 };
136
137 CRM.wysiwyg.focus = function(item) {
138 var editor = getInstance(item);
139 if (editor) {
140 editor.focus();
141 } else {
142 $(item).focus();
143 }
144 };
145
146 })(CRM.$, CRM._);