| 1 | // https://civicrm.org/licensing |
| 2 | (function($, _) { |
| 3 | |
| 4 | function getInstance(item) { |
| 5 | var name = $(item).attr("name"), |
| 6 | id = $(item).attr("id"); |
| 7 | if (name && window.CKEDITOR && CKEDITOR.instances[name]) { |
| 8 | return CKEDITOR.instances[name]; |
| 9 | } |
| 10 | if (id && window.CKEDITOR && CKEDITOR.instances[id]) { |
| 11 | return CKEDITOR.instances[id]; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | CRM.wysiwyg.supportsFileUploads = true; |
| 16 | |
| 17 | CRM.wysiwyg._create = function(item) { |
| 18 | var deferred = $.Deferred(); |
| 19 | |
| 20 | function onReady() { |
| 21 | var debounce, |
| 22 | editor = this; |
| 23 | |
| 24 | editor.on('focus', function() { |
| 25 | $(item).trigger('focus'); |
| 26 | }); |
| 27 | editor.on('blur', function() { |
| 28 | editor.updateElement(); |
| 29 | $(item).trigger("blur"); |
| 30 | $(item).trigger("change"); |
| 31 | }); |
| 32 | editor.on('insertText', function() { |
| 33 | $(item).trigger("keypress"); |
| 34 | }); |
| 35 | _.each(['key', 'pasteState'], function(evName) { |
| 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 | }); |
| 44 | editor.on('pasteState', function() { |
| 45 | $(item).trigger("paste"); |
| 46 | }); |
| 47 | // Hide CiviCRM menubar when editor is fullscreen |
| 48 | editor.on('maximize', function (e) { |
| 49 | $('#civicrm-menu').toggle(e.data === 2); |
| 50 | }); |
| 51 | $(editor.element.$).trigger('crmWysiwygCreate', ['ckeditor', editor]); |
| 52 | deferred.resolve(); |
| 53 | } |
| 54 | |
| 55 | function initialize() { |
| 56 | var |
| 57 | browseUrl = CRM.config.resourceBase + "packages/kcfinder/browse.php?cms=civicrm", |
| 58 | uploadUrl = CRM.config.resourceBase + "packages/kcfinder/upload.php?cms=civicrm&format=json", |
| 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); |
| 63 | |
| 64 | $(item).addClass('crm-wysiwyg-enabled'); |
| 65 | |
| 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', |
| 73 | customConfig: customConfig, |
| 74 | on: { |
| 75 | instanceReady: onReady |
| 76 | } |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | if ($(item).hasClass('crm-wysiwyg-enabled')) { |
| 81 | deferred.resolve(); |
| 82 | } |
| 83 | else if ($(item).length) { |
| 84 | // Lazy-load ckeditor.js |
| 85 | if (window.CKEDITOR) { |
| 86 | initialize(); |
| 87 | } else { |
| 88 | CRM.loadScript(CRM.config.resourceBase + 'bower_components/ckeditor/ckeditor.js').done(initialize); |
| 89 | } |
| 90 | } else { |
| 91 | deferred.reject(); |
| 92 | } |
| 93 | return deferred; |
| 94 | }; |
| 95 | |
| 96 | CRM.wysiwyg.destroy = function(item) { |
| 97 | $(item).removeClass('crm-wysiwyg-enabled'); |
| 98 | var editor = getInstance(item); |
| 99 | if (editor) { |
| 100 | editor.destroy(); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | CRM.wysiwyg.updateElement = function(item) { |
| 105 | var editor = getInstance(item); |
| 106 | if (editor) { |
| 107 | editor.updateElement(); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | CRM.wysiwyg.getVal = function(item) { |
| 112 | var editor = getInstance(item); |
| 113 | if (editor) { |
| 114 | return editor.getData(); |
| 115 | } else { |
| 116 | return $(item).val(); |
| 117 | } |
| 118 | }; |
| 119 | |
| 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 | }; |
| 128 | |
| 129 | CRM.wysiwyg.insert = function(item, text) { |
| 130 | var editor = getInstance(item); |
| 131 | if (editor) { |
| 132 | editor.insertText(text); |
| 133 | } else { |
| 134 | CRM.wysiwyg._insertIntoTextarea(item, text); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | CRM.wysiwyg.focus = function(item) { |
| 139 | var editor = getInstance(item); |
| 140 | if (editor) { |
| 141 | editor.focus(); |
| 142 | } else { |
| 143 | $(item).focus(); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | })(CRM.$, CRM._); |