Commit | Line | Data |
---|---|---|
b608cfb1 TC |
1 | // https://civicrm.org/licensing |
2 | (function($, _) { | |
183473a2 | 3 | |
b608cfb1 | 4 | function getInstance(item) { |
f91b1c0c CW |
5 | var name = $(item).attr("name"), |
6 | id = $(item).attr("id"); | |
c62731c3 TC |
7 | if (name && CKEDITOR.instances[name]) { |
8 | return CKEDITOR.instances[name]; | |
9 | } | |
10 | if (id && CKEDITOR.instances[id]) { | |
11 | return CKEDITOR.instances[id]; | |
12 | } | |
b608cfb1 | 13 | } |
183473a2 | 14 | |
c90cd6ed | 15 | CRM.wysiwyg.supportsFileUploads = true; |
183473a2 | 16 | |
f91b1c0c | 17 | CRM.wysiwyg.create = function(item) { |
183473a2 CW |
18 | var deferred = $.Deferred(); |
19 | ||
20 | function onReady() { | |
21 | var debounce, | |
22 | editor = this; | |
23 | ||
13d9bc82 CW |
24 | editor.on('focus', function() { |
25 | $(item).trigger('focus'); | |
26 | }); | |
c62731c3 TC |
27 | editor.on('blur', function() { |
28 | editor.updateElement(); | |
7c523661 | 29 | $(item).trigger("blur"); |
f91b1c0c | 30 | $(item).trigger("change"); |
7c523661 | 31 | }); |
c62731c3 TC |
32 | editor.on('insertText', function() { |
33 | $(item).trigger("keypress"); | |
34 | }); | |
e4d81a60 | 35 | _.each(['key', 'pasteState'], function(evName) { |
6633d03f TO |
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 | }); | |
c62731c3 TC |
44 | editor.on('pasteState', function() { |
45 | $(item).trigger("paste"); | |
46 | }); | |
9cc08ac2 CW |
47 | // Hide CiviCRM menubar when editor is fullscreen |
48 | editor.on('maximize', function (e) { | |
49 | $('#civicrm-menu').toggle(e.data === 2); | |
50 | }); | |
183473a2 CW |
51 | deferred.resolve(); |
52 | } | |
53 | ||
54 | function initialize() { | |
55 | var | |
56 | browseUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/browse.php?cms=civicrm", | |
57 | uploadUrl = CRM.config.userFrameworkResourceURL + "packages/kcfinder/upload.php?cms=civicrm"; | |
58 | ||
59 | CKEDITOR.replace($(item)[0], { | |
60 | filebrowserBrowseUrl: browseUrl + '&type=files', | |
61 | filebrowserImageBrowseUrl: browseUrl + '&type=images', | |
62 | filebrowserFlashBrowseUrl: browseUrl + '&type=flash', | |
63 | filebrowserUploadUrl: uploadUrl + '&type=files', | |
64 | filebrowserImageUploadUrl: uploadUrl + '&type=images', | |
65 | filebrowserFlashUploadUrl: uploadUrl + '&type=flash', | |
66 | customConfig: CRM.config.CKEditorCustomConfig, | |
67 | on: { | |
68 | instanceReady: onReady | |
69 | } | |
70 | }); | |
71 | } | |
72 | ||
73 | if ($(item).length) { | |
74 | initialize(); | |
c63f9bfb CW |
75 | } else { |
76 | deferred.reject(); | |
7c523661 | 77 | } |
c63f9bfb | 78 | return deferred; |
7c523661 | 79 | }; |
183473a2 | 80 | |
7c523661 TC |
81 | CRM.wysiwyg.destroy = function(item) { |
82 | var editor = getInstance(item); | |
83 | if (editor) { | |
84 | editor.destroy(); | |
85 | } | |
86 | }; | |
183473a2 | 87 | |
7c523661 TC |
88 | CRM.wysiwyg.updateElement = function(item) { |
89 | var editor = getInstance(item); | |
90 | if (editor) { | |
91 | editor.updateElement(); | |
92 | } | |
93 | }; | |
183473a2 | 94 | |
c62731c3 | 95 | CRM.wysiwyg.getVal = function(item) { |
7c523661 TC |
96 | var editor = getInstance(item); |
97 | if (editor) { | |
98 | return editor.getData(); | |
99 | } else { | |
100 | return $(item).val(); | |
101 | } | |
102 | }; | |
183473a2 | 103 | |
c62731c3 TC |
104 | CRM.wysiwyg.setVal = function(item, val) { |
105 | var editor = getInstance(item); | |
106 | if (editor) { | |
107 | return editor.setData(val); | |
108 | } else { | |
109 | return $(item).val(val); | |
110 | } | |
111 | }; | |
183473a2 | 112 | |
c62731c3 | 113 | CRM.wysiwyg.insert = function(item, text) { |
7c523661 TC |
114 | var editor = getInstance(item); |
115 | if (editor) { | |
116 | editor.insertText(text); | |
c62731c3 | 117 | } else { |
f91b1c0c | 118 | CRM.wysiwyg._insertIntoTextarea(item, text); |
7c523661 TC |
119 | } |
120 | }; | |
183473a2 | 121 | |
c62731c3 | 122 | CRM.wysiwyg.focus = function(item) { |
7c523661 TC |
123 | var editor = getInstance(item); |
124 | if (editor) { | |
c62731c3 | 125 | editor.focus(); |
f91b1c0c CW |
126 | } else { |
127 | $(item).focus(); | |
7c523661 | 128 | } |
b608cfb1 | 129 | }; |
c62731c3 | 130 | |
b608cfb1 | 131 | })(CRM.$, CRM._); |