Merge pull request #5739 from onlyjob/license
[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 }
21 if (editor) {
22 editor.config.filebrowserBrowseUrl = browseUrl + '&type=files';
23 editor.config.filebrowserImageBrowseUrl = browseUrl + '&type=images';
24 editor.config.filebrowserFlashBrowseUrl = browseUrl + '&type=flash';
25 editor.config.filebrowserUploadUrl = uploadUrl + '&type=files';
26 editor.config.filebrowserImageUploadUrl = uploadUrl + '&type=images';
27 editor.config.filebrowserFlashUploadUrl = uploadUrl + '&type=flash';
28 editor.on('focus', function() {
29 $(item).trigger('focus');
30 });
31 editor.on('blur', function() {
32 editor.updateElement();
33 $(item).trigger("blur");
34 $(item).trigger("change");
35 });
36 editor.on('insertText', function() {
37 $(item).trigger("keypress");
38 });
39 editor.on('pasteState', function() {
40 $(item).trigger("paste");
41 });
42 }
43 };
44 CRM.wysiwyg.destroy = function(item) {
45 var editor = getInstance(item);
46 if (editor) {
47 editor.destroy();
48 }
49 };
50 CRM.wysiwyg.updateElement = function(item) {
51 var editor = getInstance(item);
52 if (editor) {
53 editor.updateElement();
54 }
55 };
56 CRM.wysiwyg.getVal = function(item) {
57 var editor = getInstance(item);
58 if (editor) {
59 return editor.getData();
60 } else {
61 return $(item).val();
62 }
63 };
64 CRM.wysiwyg.setVal = function(item, val) {
65 var editor = getInstance(item);
66 if (editor) {
67 return editor.setData(val);
68 } else {
69 return $(item).val(val);
70 }
71 };
72 CRM.wysiwyg.insert = function(item, text) {
73 var editor = getInstance(item);
74 if (editor) {
75 editor.insertText(text);
76 } else {
77 CRM.wysiwyg._insertIntoTextarea(item, text);
78 }
79 };
80 CRM.wysiwyg.focus = function(item) {
81 var editor = getInstance(item);
82 if (editor) {
83 editor.focus();
84 } else {
85 $(item).focus();
86 }
87 };
88
89 })(CRM.$, CRM._);