Merge pull request #6993 from eileenmcnaughton/tests
[civicrm-core.git] / js / wysiwyg / crm.ckeditor.js
1 // https://civicrm.org/licensing
2 (function($, _) {
3 function getInstance(item) {
4 var name = $(item).attr("name"),
5 id = $(item).attr("id");
6 if (name && CKEDITOR.instances[name]) {
7 return CKEDITOR.instances[name];
8 }
9 if (id && CKEDITOR.instances[id]) {
10 return CKEDITOR.instances[id];
11 }
12 }
13 CRM.wysiwyg.supportsFileUploads = true;
14 CRM.wysiwyg.create = function(item) {
15 var editor,
16 browseUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/browse.php?cms=civicrm",
17 uploadUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/upload.php?cms=civicrm";
18 if ($(item).length) {
19 editor = CKEDITOR.replace($(item)[0], {
20 filebrowserBrowseUrl: browseUrl + '&type=files',
21 filebrowserImageBrowseUrl: browseUrl + '&type=images',
22 filebrowserFlashBrowseUrl: browseUrl + '&type=flash',
23 filebrowserUploadUrl: uploadUrl + '&type=files',
24 filebrowserImageUploadUrl: uploadUrl + '&type=images',
25 filebrowserFlashUploadUrl: uploadUrl + '&type=flash',
26 customConfig: CRM.config.CKEditorCustomConfig
27 });
28 }
29 if (editor) {
30 editor.on('focus', function() {
31 $(item).trigger('focus');
32 });
33 editor.on('blur', function() {
34 editor.updateElement();
35 $(item).trigger("blur");
36 $(item).trigger("change");
37 });
38 editor.on('insertText', function() {
39 $(item).trigger("keypress");
40 });
41 var debounce = null;
42 _.each(['key', 'pasteState'], function(evName) {
43 editor.on(evName, function(evt) {
44 if (debounce) clearTimeout(debounce);
45 debounce = setTimeout(function() {
46 editor.updateElement();
47 $(item).trigger("change");
48 }, 50);
49 });
50 });
51 editor.on('pasteState', function() {
52 $(item).trigger("paste");
53 });
54 // Hide CiviCRM menubar when editor is fullscreen
55 editor.on('maximize', function (e) {
56 $('#civicrm-menu').toggle(e.data === 2);
57 });
58 }
59 };
60 CRM.wysiwyg.destroy = function(item) {
61 var editor = getInstance(item);
62 if (editor) {
63 editor.destroy();
64 }
65 };
66 CRM.wysiwyg.updateElement = function(item) {
67 var editor = getInstance(item);
68 if (editor) {
69 editor.updateElement();
70 }
71 };
72 CRM.wysiwyg.getVal = function(item) {
73 var editor = getInstance(item);
74 if (editor) {
75 return editor.getData();
76 } else {
77 return $(item).val();
78 }
79 };
80 CRM.wysiwyg.setVal = function(item, val) {
81 var editor = getInstance(item);
82 if (editor) {
83 return editor.setData(val);
84 } else {
85 return $(item).val(val);
86 }
87 };
88 CRM.wysiwyg.insert = function(item, text) {
89 var editor = getInstance(item);
90 if (editor) {
91 editor.insertText(text);
92 } else {
93 CRM.wysiwyg._insertIntoTextarea(item, text);
94 }
95 };
96 CRM.wysiwyg.focus = function(item) {
97 var editor = getInstance(item);
98 if (editor) {
99 editor.focus();
100 } else {
101 $(item).focus();
102 }
103 };
104
105 })(CRM.$, CRM._);