(function (factory) { 'use strict'; if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define('bootstrap.wysihtml5', ['jquery', 'wysihtml5', 'bootstrap', 'bootstrap.wysihtml5.templates', 'bootstrap.wysihtml5.commands'], factory); } else { // Browser globals factory(jQuery, wysihtml5); // jshint ignore:line } }(function ($, wysihtml5) { 'use strict'; var bsWysihtml5 = function($, wysihtml5) { var templates = function(key, locale, options) { if(wysihtml5.tpl[key]) { return wysihtml5.tpl[key]({locale: locale, options: options}); } }; var Wysihtml5 = function(el, options) { this.el = el; var toolbarOpts = $.extend(true, {}, defaultOptions, options); for(var t in toolbarOpts.customTemplates) { if (toolbarOpts.customTemplates.hasOwnProperty(t)) { wysihtml5.tpl[t] = toolbarOpts.customTemplates[t]; } } this.toolbar = this.createToolbar(el, toolbarOpts); this.editor = this.createEditor(toolbarOpts); }; Wysihtml5.prototype = { constructor: Wysihtml5, createEditor: function(options) { options = options || {}; // Add the toolbar to a clone of the options object so multiple instances // of the WYISYWG don't break because 'toolbar' is already defined options = $.extend(true, {}, options); options.toolbar = this.toolbar[0]; this.initializeEditor(this.el[0], options); }, initializeEditor: function(el, options) { var editor = new wysihtml5.Editor(this.el[0], options); editor.on('beforeload', this.syncBootstrapDialogEvents); editor.on('beforeload', this.loadParserRules); // #30 - body is in IE 10 not created by default, which leads to nullpointer // 2014/02/13 - adapted to wysihtml5-0.4, does not work in IE if(editor.composer.editableArea.contentDocument) { this.addMoreShortcuts(editor, editor.composer.editableArea.contentDocument.body || editor.composer.editableArea.contentDocument, options.shortcuts); } else { this.addMoreShortcuts(editor, editor.composer.editableArea, options.shortcuts); } if(options && options.events) { for(var eventName in options.events) { if (options.events.hasOwnProperty(eventName)) { editor.on(eventName, options.events[eventName]); } } } return editor; }, loadParserRules: function() { if($.type(this.config.parserRules) === 'string') { $.ajax({ dataType: 'json', url: this.config.parserRules, context: this, error: function (jqXHR, textStatus, errorThrown) { console.log(errorThrown); }, success: function (parserRules) { this.config.parserRules = parserRules; console.log('parserrules loaded'); } }); } if(this.config.pasteParserRulesets && $.type(this.config.pasteParserRulesets) === 'string') { $.ajax({ dataType: 'json', url: this.config.pasteParserRulesets, context: this, error: function (jqXHR, textStatus, errorThrown) { console.log(errorThrown); }, success: function (pasteParserRulesets) { this.config.pasteParserRulesets = pasteParserRulesets; } }); } }, //sync wysihtml5 events for dialogs with bootstrap events syncBootstrapDialogEvents: function() { var editor = this; $.map(this.toolbar.commandMapping, function(value) { return [value]; }).filter(function(commandObj) { return commandObj.dialog; }).map(function(commandObj) { return commandObj.dialog; }).forEach(function(dialog) { dialog.on('show', function() { $(this.container).modal('show'); }); dialog.on('hide', function() { $(this.container).modal('hide'); setTimeout(editor.composer.focus, 0); }); $(dialog.container).on('shown.bs.modal', function () { $(this).find('input, select, textarea').first().focus(); }); }); this.on('change_view', function() { $(this.toolbar.container.children).find('a.btn').not('[data-wysihtml5-action="change_view"]').toggleClass('disabled'); }); }, createToolbar: function(el, options) { var self = this; var toolbar = $('