/** * Trumbowyg v2.0.0-beta.2 - A lightweight WYSIWYG editor * Trumbowyg core file * ------------------------ * @link http://alex-d.github.io/Trumbowyg * @license MIT * @author Alexandre Demode (Alex-D) * Twitter : @AlexandreDemode * Website : alex-d.fr */ jQuery.trumbowyg = { langs: { en: { viewHTML: "View HTML", formatting: "Formatting", p: "Paragraph", blockquote: "Quote", code: "Code", header: "Header", bold: "Bold", italic: "Italic", strikethrough: "Stroke", underline: "Underline", strong: "Strong", em: "Emphasis", del: "Deleted", unorderedList: "Unordered list", orderedList: "Ordered list", insertImage: "Insert Image", insertVideo: "Insert Video", link: "Link", createLink: "Insert link", unlink: "Remove link", justifyLeft: "Align Left", justifyCenter: "Align Center", justifyRight: "Align Right", justifyFull: "Align Justify", horizontalRule: "Insert horizontal rule", removeformat: "Remove format", fullscreen: "fullscreen", close: "Close", submit: "Confirm", reset: "Cancel", required: "Required", description: "Description", title: "Title", text: "Text" } }, // User default options opts: {}, btnsGrps: { design: ['bold', 'italic', 'underline', 'strikethrough'], semantic: ['strong', 'em', 'del'], justify: ['justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull'], lists: ['unorderedList', 'orderedList'] } }; (function(navigator, window, document, $, undefined){ 'use strict'; // @param : o are options // @param : p are params $.fn.trumbowyg = function(o, p){ if(o === Object(o) || !o){ return this.each(function(){ if(!$(this).data('trumbowyg')) $(this).data('trumbowyg', new Trumbowyg(this, o)); }); } if(this.length === 1){ try { var t = $(this).data('trumbowyg'); switch(o){ // Modal box case 'openModal': return t.openModal(p.title, p.content); case 'closeModal': return t.closeModal(); case 'openModalInsert': return t.openModalInsert(p.title, p.fields, p.callback); // Selection case 'saveSelection': return t.saveSelection(); case 'getSelection': return t.selection; case 'getSelectedText': return t.getSelectedText(); case 'restoreSelection': return t.restoreSelection(); // Destroy case 'destroy': return t.destroy(); // Empty case 'empty': return t.empty(); // Public options case 'lang': return t.lang; // HTML case 'html': return t.html(p); } } catch(e){} } return false; }; // @param : editorElem is the DOM element // @param : o are options var Trumbowyg = function(editorElem, o){ var t = this; // Get the document of the element. It use to makes the plugin // compatible on iframes. t.doc = editorElem.ownerDocument || document; // jQuery object of the editor t.$ta = $(editorElem); // $ta : Textarea t.$c = $(editorElem); // $c : creator // Extend with options o = $.extend(true, {}, o, $.trumbowyg.opts); // Localization management if(typeof o.lang === 'undefined' || typeof $.trumbowyg.langs[o.lang] === 'undefined') t.lang = $.trumbowyg.langs.en; else t.lang = $.extend(true, {}, $.trumbowyg.langs.en, $.trumbowyg.langs[o.lang]); // Header translation var h = t.lang.header; // Defaults Options t.o = $.extend(true, {}, { lang: 'en', dir: 'ltr', closable: false, fullscreenable: true, fixedBtnPane: false, fixedFullWidth: false, autogrow: false, prefix: 'trumbowyg-', // WYSIWYG only semantic: true, resetCss: false, removeformatPasted: false, btns: [ 'viewHTML', '|', 'formatting', '|', 'btnGrp-design', '|', 'link', '|', 'insertImage', '|', 'btnGrp-justify', '|', 'btnGrp-lists', '|', 'horizontalRule', '|', 'removeformat' ], btnsAdd: [], /** * When the button is associated to a empty object * func and title attributs are defined from the button key value * * For example * foo: {} * is equivalent to : * foo: { * func: 'foo', * title: this.lang.foo * } */ btnsDef: { viewHTML: { func: 'toggle' }, p: { func: 'formatBlock' }, blockquote: { func: 'formatBlock' }, h1: { func: 'formatBlock', title: h + ' 1' }, h2: { func: 'formatBlock', title: h + ' 2' }, h3: { func: 'formatBlock', title: h + ' 3' }, h4: { func: 'formatBlock', title: h + ' 4' }, bold: { key: 'B' }, italic: { key: 'I' }, underline: {}, strikethrough: {}, strong: { func: 'bold', key: 'B' }, em: { func: 'italic', key: 'I' }, del: { func: 'strikethrough' }, createLink: { key: 'K' }, unlink: {}, insertImage: {}, justifyLeft: {}, justifyCenter: {}, justifyRight: {}, justifyFull: {}, unorderedList: { func: 'insertUnorderedList' }, orderedList: { func: 'insertOrderedList' }, horizontalRule: { func: 'insertHorizontalRule' }, removeformat: {}, // Dropdowns formatting: { dropdown: ['p', 'blockquote', 'h1', 'h2', 'h3', 'h4'] }, link: { dropdown: ['createLink', 'unlink'] } } }, o); if(o.btns) t.o.btns = o.btns; else if(t.o.semantic) t.o.btns[4] = 'btnGrp-semantic'; // Keyboard shortcuts are load in this array t.keys = []; t.init(); }; Trumbowyg.prototype = { init: function(){ var t = this; t.height = t.$ta.height(); t.buildEditor(); t.buildBtnPane(); t.fixedBtnPaneEvents(); t.buildOverlay(); }, buildEditor: function(){ var t = this, prefix = t.o.prefix, html = ''; t.$box = $('
', { 'class': prefix + 'box ' + prefix + 'editor-visible ' + prefix + t.o.lang + ' trumbowyg' }); // $ta = Textarea // $ed = Editor t.isTextarea = t.$ta.is('textarea'); if(t.isTextarea){ html = t.$ta.val(); t.$ed = $('
'); t.$box .insertAfter(t.$ta) .append(t.$ed, t.$ta); } else { t.$ed = t.$ta; html = t.$ed.html(); t.$ta = $('