CRM-16354 - wysiwyg code cleanup
[civicrm-core.git] / js / wysiwyg / crm.wysiwyg.js
1 // https://civicrm.org/licensing
2 (function($, _) {
3 // This defines an interface which by default only handles plain textareas
4 // A wysiwyg implementation can extend this by overriding as many of these functions as needed
5 CRM.wysiwyg = {
6 supportsFileUploads: false,
7 create: _.noop,
8 destroy: _.noop,
9 updateElement: _.noop,
10 getVal: function(item) {
11 return $(item).val();
12 },
13 setVal: function(item, val) {
14 return $(item).val(val);
15 },
16 insert: function(item, text) {
17 CRM.wysiwyg._insertIntoTextarea(item, text);
18 },
19 focus: function(item) {
20 $(item).focus();
21 },
22 // Fallback function to use when a wysiwyg has not been initialized
23 _insertIntoTextarea: function(item, text) {
24 var origVal = $(item).val();
25 var origPos = item[0].selectionStart;
26 var newVal = origVal + text;
27 $(item).val(newVal);
28 var newPos = (origPos + text.length);
29 item[0].selectionStart = newPos;
30 item[0].selectionEnd = newPos;
31 $(item).triggerHandler('change');
32 CRM.wysiwyg.focus(item);
33 },
34 createCollapsed: function(item) {
35 $(item)
36 .hide()
37 .after('<div class="replace-plain" tabindex="0"></div>')
38 .on('blur', function () {
39 CRM.wysiwyg.destroy(item);
40 $(item).hide().next('.replace-plain').show().html($(item).val());
41 });
42 $(item).next('.replace-plain').attr('title', ts('Click to edit')).on('click keypress', function () {
43 $(item).show().next('.replace-plain').hide();
44 CRM.wysiwyg.create(item);
45 });
46 }
47 };
48 })(CRM.$, CRM._);