Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-10-14-11-04-09
[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 // Create a "collapsed" textarea that expands into a wysiwyg when clicked
35 createCollapsed: function(item) {
36 $(item)
37 .hide()
38 .on('blur', function () {
39 CRM.wysiwyg.destroy(item);
40 $(item).hide().next('.replace-plain').show().html($(item).val());
41 })
42 .after('<div class="replace-plain" tabindex="0"></div>');
43 $(item).next('.replace-plain')
44 .attr('title', ts('Click to edit'))
45 .html($(item).val())
46 .on('click keypress', function (e) {
47 // Stop browser from opening clicked links
48 e.preventDefault();
49 $(item).show().next('.replace-plain').hide();
50 CRM.wysiwyg.create(item);
51 });
52 }
53 };
54 })(CRM.$, CRM._);