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 | |
ac5fa736 | 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 | }); | |
a1f06c87 | 51 | $(editor.element.$).trigger('crmWysiwygCreate', ['ckeditor', editor]); |
183473a2 CW |
52 | deferred.resolve(); |
53 | } | |
ac5fa736 | 54 | |
183473a2 CW |
55 | function initialize() { |
56 | var | |
1ff4b7b5 TO |
57 | browseUrl = CRM.config.packagesBase + "kcfinder/browse.php?cms=civicrm", |
58 | uploadUrl = CRM.config.packagesBase + "kcfinder/upload.php?cms=civicrm&format=json", | |
7ad5ae6a CW |
59 | preset = $(item).data('preset') || 'default', |
60 | // This variable is always an array but a legacy extension could be setting it as a string. | |
61 | customConfig = (typeof CRM.config.CKEditorCustomConfig === 'string') ? CRM.config.CKEditorCustomConfig : | |
62 | (CRM.config.CKEditorCustomConfig[preset] || CRM.config.CKEditorCustomConfig.default); | |
183473a2 | 63 | |
9db15279 CW |
64 | $(item).addClass('crm-wysiwyg-enabled'); |
65 | ||
183473a2 CW |
66 | CKEDITOR.replace($(item)[0], { |
67 | filebrowserBrowseUrl: browseUrl + '&type=files', | |
68 | filebrowserImageBrowseUrl: browseUrl + '&type=images', | |
69 | filebrowserFlashBrowseUrl: browseUrl + '&type=flash', | |
70 | filebrowserUploadUrl: uploadUrl + '&type=files', | |
71 | filebrowserImageUploadUrl: uploadUrl + '&type=images', | |
72 | filebrowserFlashUploadUrl: uploadUrl + '&type=flash', | |
7ad5ae6a | 73 | customConfig: customConfig, |
183473a2 CW |
74 | on: { |
75 | instanceReady: onReady | |
76 | } | |
77 | }); | |
78 | } | |
9db15279 CW |
79 | |
80 | if ($(item).hasClass('crm-wysiwyg-enabled')) { | |
81 | deferred.resolve(); | |
82 | } | |
83 | else if ($(item).length) { | |
80ab9d76 CW |
84 | // Lazy-load ckeditor.js |
85 | if (window.CKEDITOR) { | |
86 | initialize(); | |
87 | } else { | |
286a7e5a | 88 | CRM.loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js').done(initialize); |
80ab9d76 | 89 | } |
c63f9bfb CW |
90 | } else { |
91 | deferred.reject(); | |
7c523661 | 92 | } |
c63f9bfb | 93 | return deferred; |
7c523661 | 94 | }; |
183473a2 | 95 | |
7c523661 | 96 | CRM.wysiwyg.destroy = function(item) { |
9db15279 | 97 | $(item).removeClass('crm-wysiwyg-enabled'); |
7c523661 TC |
98 | var editor = getInstance(item); |
99 | if (editor) { | |
100 | editor.destroy(); | |
101 | } | |
102 | }; | |
183473a2 | 103 | |
7c523661 TC |
104 | CRM.wysiwyg.updateElement = function(item) { |
105 | var editor = getInstance(item); | |
106 | if (editor) { | |
107 | editor.updateElement(); | |
108 | } | |
109 | }; | |
183473a2 | 110 | |
c62731c3 | 111 | CRM.wysiwyg.getVal = function(item) { |
7c523661 TC |
112 | var editor = getInstance(item); |
113 | if (editor) { | |
114 | return editor.getData(); | |
115 | } else { | |
116 | return $(item).val(); | |
117 | } | |
118 | }; | |
183473a2 | 119 | |
c62731c3 TC |
120 | CRM.wysiwyg.setVal = function(item, val) { |
121 | var editor = getInstance(item); | |
122 | if (editor) { | |
123 | return editor.setData(val); | |
124 | } else { | |
125 | return $(item).val(val); | |
126 | } | |
127 | }; | |
183473a2 | 128 | |
c62731c3 | 129 | CRM.wysiwyg.insert = function(item, text) { |
7c523661 TC |
130 | var editor = getInstance(item); |
131 | if (editor) { | |
132 | editor.insertText(text); | |
c62731c3 | 133 | } else { |
f91b1c0c | 134 | CRM.wysiwyg._insertIntoTextarea(item, text); |
7c523661 TC |
135 | } |
136 | }; | |
80ab9d76 | 137 | |
c62731c3 | 138 | CRM.wysiwyg.focus = function(item) { |
7c523661 TC |
139 | var editor = getInstance(item); |
140 | if (editor) { | |
c62731c3 | 141 | editor.focus(); |
f91b1c0c CW |
142 | } else { |
143 | $(item).focus(); | |
7c523661 | 144 | } |
b608cfb1 | 145 | }; |
c62731c3 | 146 | |
b608cfb1 | 147 | })(CRM.$, CRM._); |