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"); | |
5f005e5d | 7 | if (name && window.CKEDITOR && CKEDITOR.instances[name]) { |
c62731c3 TC |
8 | return CKEDITOR.instances[name]; |
9 | } | |
5f005e5d | 10 | if (id && window.CKEDITOR && CKEDITOR.instances[id]) { |
c62731c3 TC |
11 | return CKEDITOR.instances[id]; |
12 | } | |
b608cfb1 | 13 | } |
183473a2 | 14 | |
c90cd6ed | 15 | CRM.wysiwyg.supportsFileUploads = true; |
183473a2 | 16 | |
286a7e5a | 17 | CRM.wysiwyg._create = function(item) { |
183473a2 | 18 | var deferred = $.Deferred(); |
80ab9d76 | 19 | |
183473a2 CW |
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 | |
dc662c39 | 56 | browseUrl = CRM.config.resourceBase + "packages/kcfinder/browse.php?cms=civicrm", |
7ad5ae6a CW |
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); | |
183473a2 | 62 | |
9db15279 CW |
63 | $(item).addClass('crm-wysiwyg-enabled'); |
64 | ||
183473a2 CW |
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', | |
7ad5ae6a | 72 | customConfig: customConfig, |
183473a2 CW |
73 | on: { |
74 | instanceReady: onReady | |
75 | } | |
76 | }); | |
77 | } | |
9db15279 CW |
78 | |
79 | if ($(item).hasClass('crm-wysiwyg-enabled')) { | |
80 | deferred.resolve(); | |
81 | } | |
82 | else if ($(item).length) { | |
80ab9d76 CW |
83 | // Lazy-load ckeditor.js |
84 | if (window.CKEDITOR) { | |
85 | initialize(); | |
86 | } else { | |
286a7e5a | 87 | CRM.loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js').done(initialize); |
80ab9d76 | 88 | } |
c63f9bfb CW |
89 | } else { |
90 | deferred.reject(); | |
7c523661 | 91 | } |
c63f9bfb | 92 | return deferred; |
7c523661 | 93 | }; |
183473a2 | 94 | |
7c523661 | 95 | CRM.wysiwyg.destroy = function(item) { |
9db15279 | 96 | $(item).removeClass('crm-wysiwyg-enabled'); |
7c523661 TC |
97 | var editor = getInstance(item); |
98 | if (editor) { | |
99 | editor.destroy(); | |
100 | } | |
101 | }; | |
183473a2 | 102 | |
7c523661 TC |
103 | CRM.wysiwyg.updateElement = function(item) { |
104 | var editor = getInstance(item); | |
105 | if (editor) { | |
106 | editor.updateElement(); | |
107 | } | |
108 | }; | |
183473a2 | 109 | |
c62731c3 | 110 | CRM.wysiwyg.getVal = function(item) { |
7c523661 TC |
111 | var editor = getInstance(item); |
112 | if (editor) { | |
113 | return editor.getData(); | |
114 | } else { | |
115 | return $(item).val(); | |
116 | } | |
117 | }; | |
183473a2 | 118 | |
c62731c3 TC |
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 | }; | |
183473a2 | 127 | |
c62731c3 | 128 | CRM.wysiwyg.insert = function(item, text) { |
7c523661 TC |
129 | var editor = getInstance(item); |
130 | if (editor) { | |
131 | editor.insertText(text); | |
c62731c3 | 132 | } else { |
f91b1c0c | 133 | CRM.wysiwyg._insertIntoTextarea(item, text); |
7c523661 TC |
134 | } |
135 | }; | |
80ab9d76 | 136 | |
c62731c3 | 137 | CRM.wysiwyg.focus = function(item) { |
7c523661 TC |
138 | var editor = getInstance(item); |
139 | if (editor) { | |
c62731c3 | 140 | editor.focus(); |
f91b1c0c CW |
141 | } else { |
142 | $(item).focus(); | |
7c523661 | 143 | } |
b608cfb1 | 144 | }; |
c62731c3 | 145 | |
b608cfb1 | 146 | })(CRM.$, CRM._); |