Commit | Line | Data |
---|---|---|
7c523661 TC |
1 | // https://civicrm.org/licensing |
2 | (function($, _) { | |
f91b1c0c CW |
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 = { | |
286a7e5a CW |
6 | supportsFileUploads: !!CRM.config.wysisygScriptLocation, |
7 | create: function(item) { | |
8 | var ret = $.Deferred(); | |
9 | // Lazy-load the wysiwyg js | |
10 | if (CRM.config.wysisygScriptLocation) { | |
11 | CRM.loadScript(CRM.config.wysisygScriptLocation).done(function() { | |
12 | CRM.wysiwyg._create(item).done(function() { | |
13 | ret.resolve(); | |
14 | }); | |
15 | }); | |
16 | } else { | |
17 | ret.resolve(); | |
18 | } | |
19 | return ret; | |
c63f9bfb | 20 | }, |
f91b1c0c CW |
21 | destroy: _.noop, |
22 | updateElement: _.noop, | |
23 | getVal: function(item) { | |
24 | return $(item).val(); | |
25 | }, | |
26 | setVal: function(item, val) { | |
27 | return $(item).val(val); | |
28 | }, | |
29 | insert: function(item, text) { | |
30 | CRM.wysiwyg._insertIntoTextarea(item, text); | |
31 | }, | |
32 | focus: function(item) { | |
33 | $(item).focus(); | |
34 | }, | |
35 | // Fallback function to use when a wysiwyg has not been initialized | |
36 | _insertIntoTextarea: function(item, text) { | |
e815b31b | 37 | var itemObj = $(item); |
ede7390a | 38 | var origVal = itemObj.val(); |
e815b31b NM |
39 | var origStart = itemObj[0].selectionStart; |
40 | var origEnd = itemObj[0].selectionEnd; | |
41 | var newVal = origVal.substring(0, origStart) + text + origVal.substring(origEnd); | |
ede7390a | 42 | itemObj.val(newVal); |
e815b31b | 43 | var newPos = (origStart + text.length); |
ede7390a JP |
44 | itemObj[0].selectionStart = newPos; |
45 | itemObj[0].selectionEnd = newPos; | |
46 | itemObj.triggerHandler('change'); | |
f91b1c0c CW |
47 | CRM.wysiwyg.focus(item); |
48 | }, | |
0bd65d9b | 49 | // Create a "collapsed" textarea that expands into a wysiwyg when clicked |
f91b1c0c CW |
50 | createCollapsed: function(item) { |
51 | $(item) | |
52 | .hide() | |
f91b1c0c CW |
53 | .on('blur', function () { |
54 | CRM.wysiwyg.destroy(item); | |
55 | $(item).hide().next('.replace-plain').show().html($(item).val()); | |
0bd65d9b | 56 | }) |
071e5a4f CW |
57 | .on('change', function() { |
58 | $(this).next('.replace-plain').html($(this).val()); | |
59 | }) | |
0bd65d9b CW |
60 | .after('<div class="replace-plain" tabindex="0"></div>'); |
61 | $(item).next('.replace-plain') | |
62 | .attr('title', ts('Click to edit')) | |
63 | .html($(item).val()) | |
64 | .on('click keypress', function (e) { | |
65 | // Stop browser from opening clicked links | |
66 | e.preventDefault(); | |
67 | $(item).show().next('.replace-plain').hide(); | |
68 | CRM.wysiwyg.create(item); | |
f91b1c0c | 69 | }); |
f91b1c0c | 70 | } |
7c523661 | 71 | }; |
7c523661 | 72 | })(CRM.$, CRM._); |