// https://civicrm.org/licensing
(function($, _) {
- function openWysiwyg(item) {
- $(item).show();
- $(item).next('.replace-plain').hide();
- CRM.wysiwyg.create(item);
- $(item).on( 'blur', function( e ) {
- CRM.wysiwyg.updateElement(item);
- CRM.wysiwyg.destroy(item);
- $(item).hide().next('.replace-plain').show().html($(item).val());
- });
- }
- CRM.wysiwyg = {};
- CRM.wysiwyg.supportsFileUploads = false;
- CRM.wysiwyg.create = _.noop;
- CRM.wysiwyg.destroy = _.noop;
- CRM.wysiwyg.updateElement = _.noop;
- CRM.wysiwyg.val = function(item) {
- return $(item).val();
- };
- CRM.wysiwyg.insertText = _.noop;
- CRM.wysiwyg.insertHTML = _.noop;
- CRM.wysiwyg.createPlain = function(item) {
- $(item)
- .hide()
- .after('<div class="replace-plain" tabindex="0" title="Click to edit"></div>');
- $(item).next('.replace-plain').click(function(){
- openWysiwyg(item);
- });
- $(item).next('.replace-plain').keypress(function(){
- openWysiwyg(item);
- });
+ // This defines an interface which by default only handles plain textareas
+ // A wysiwyg implementation can extend this by overriding as many of these functions as needed
+ CRM.wysiwyg = {
+ supportsFileUploads: false,
+ create: _.noop,
+ destroy: _.noop,
+ updateElement: _.noop,
+ getVal: function(item) {
+ return $(item).val();
+ },
+ setVal: function(item, val) {
+ return $(item).val(val);
+ },
+ insert: function(item, text) {
+ CRM.wysiwyg._insertIntoTextarea(item, text);
+ },
+ focus: function(item) {
+ $(item).focus();
+ },
+ // Fallback function to use when a wysiwyg has not been initialized
+ _insertIntoTextarea: function(item, text) {
+ var origVal = $(item).val();
+ var origPos = item[0].selectionStart;
+ var newVal = origVal + text;
+ $(item).val(newVal);
+ var newPos = (origPos + text.length);
+ item[0].selectionStart = newPos;
+ item[0].selectionEnd = newPos;
+ $(item).triggerHandler('change');
+ CRM.wysiwyg.focus(item);
+ },
+ // Create a "collapsed" textarea that expands into a wysiwyg when clicked
+ createCollapsed: function(item) {
+ $(item)
+ .hide()
+ .on('blur', function () {
+ CRM.wysiwyg.destroy(item);
+ $(item).hide().next('.replace-plain').show().html($(item).val());
+ })
+ .after('<div class="replace-plain" tabindex="0"></div>');
+ $(item).next('.replace-plain')
+ .attr('title', ts('Click to edit'))
+ .html($(item).val())
+ .on('click keypress', function (e) {
+ // Stop browser from opening clicked links
+ e.preventDefault();
+ $(item).show().next('.replace-plain').hide();
+ CRM.wysiwyg.create(item);
+ });
+ }
};
})(CRM.$, CRM._);