Merge pull request #9511 from civicrm/4.7.14-rc
[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: !!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;
20 },
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) {
37 var origVal = $(item).val();
38 var origPos = item[0].selectionStart;
39 var newVal = origVal + text;
40 $(item).val(newVal);
41 var newPos = (origPos + text.length);
42 item[0].selectionStart = newPos;
43 item[0].selectionEnd = newPos;
44 $(item).triggerHandler('change');
45 CRM.wysiwyg.focus(item);
46 },
47 // Create a "collapsed" textarea that expands into a wysiwyg when clicked
48 createCollapsed: function(item) {
49 $(item)
50 .hide()
51 .on('blur', function () {
52 CRM.wysiwyg.destroy(item);
53 $(item).hide().next('.replace-plain').show().html($(item).val());
54 })
55 .after('<div class="replace-plain" tabindex="0"></div>');
56 $(item).next('.replace-plain')
57 .attr('title', ts('Click to edit'))
58 .html($(item).val())
59 .on('click keypress', function (e) {
60 // Stop browser from opening clicked links
61 e.preventDefault();
62 $(item).show().next('.replace-plain').hide();
63 CRM.wysiwyg.create(item);
64 });
65 }
66 };
67 })(CRM.$, CRM._);