(function(factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if (typeof module === 'object' && module.exports) { // Node/CommonJS module.exports = factory(require('jquery')); } else { // Browser globals factory(window.jQuery); } }(function($) { // pull in some summernote core functions var ui = $.summernote.ui; var dom = $.summernote.dom; // define the popover plugin var DataBasicPlugin = function(context) { var self = this; var options = context.options; var lang = options.langInfo; self.icon = ''; // add context menu button for dialog context.memo('button.databasic', function() { return ui.button({ contents: self.icon, tooltip: lang.databasic.insert, click: context.createInvokeHandler('databasic.showDialog'), }).render(); }); // add popover edit button context.memo('button.databasicDialog', function() { return ui.button({ contents: self.icon, tooltip: lang.databasic.edit, click: context.createInvokeHandler('databasic.showDialog'), }).render(); }); // add popover size buttons context.memo('button.databasicSize100', function() { return ui.button({ contents: '100%', tooltip: lang.image.resizeFull, click: context.createInvokeHandler('editor.resize', '1'), }).render(); }); context.memo('button.databasicSize50', function() { return ui.button({ contents: '50%', tooltip: lang.image.resizeHalf, click: context.createInvokeHandler('editor.resize', '0.5'), }).render(); }); context.memo('button.databasicSize25', function() { return ui.button({ contents: '25%', tooltip: lang.image.resizeQuarter, click: context.createInvokeHandler('editor.resize', '0.25'), }).render(); }); self.events = { 'summernote.init': function(we, e) { // update existing containers $('data.ext-databasic', e.editable).each(function() { self.setContent($(this)); }); // TODO: make this an undo snapshot... }, 'summernote.keyup summernote.mouseup summernote.change summernote.scroll': function() { self.update(); }, 'summernote.dialog.shown': function() { self.hidePopover(); }, }; self.initialize = function() { // create dialog markup var $container = options.dialogsInBody ? $(document.body) : context.layoutInfo.editor; var body = '
' + self.icon + ' ' + lang.databasic.name + ': ' + $node.attr('data-test') + '
'); }; self.updateNode = function(info) { self.setContent(info.node .attr('data-test', info.test)); }; self.createNode = function(info) { var $node = $(''); if ($node) { // save node to info structure info.node = $node; // insert node into editor dom context.invoke('editor.insertNode', $node[0]); } return $node; }; self.showDialog = function() { var info = self.getInfo(); var newNode = !info.node; context.invoke('editor.saveRange'); self .openDialog(info) .then(function(dialogInfo) { // [workaround] hide dialog before restore range for IE range focus ui.hideDialog(self.$dialog); context.invoke('editor.restoreRange'); // insert a new node if (newNode) { self.createNode(info); } // update info with dialog info $.extend(info, dialogInfo); self.updateNode(info); }) .fail(function() { context.invoke('editor.restoreRange'); }); }; self.openDialog = function(info) { return $.Deferred(function(deferred) { var $inpTest = self.$dialog.find('.ext-databasic-test'); var $saveBtn = self.$dialog.find('.ext-databasic-save'); var onKeyup = function(event) { if (event.keyCode === 13) { $saveBtn.trigger('click'); } }; ui.onDialogShown(self.$dialog, function() { context.triggerEvent('dialog.shown'); $inpTest.val(info.test).on('input', function() { ui.toggleBtn($saveBtn, $inpTest.val()); }).trigger('focus').on('keyup', onKeyup); $saveBtn .text(info.node ? lang.databasic.edit : lang.databasic.insert) .click(function(event) { event.preventDefault(); deferred.resolve({ test: $inpTest.val() }); }); // init save button ui.toggleBtn($saveBtn, $inpTest.val()); }); ui.onDialogHidden(self.$dialog, function() { $inpTest.off('input keyup'); $saveBtn.off('click'); if (deferred.state() === 'pending') { deferred.reject(); } }); ui.showDialog(self.$dialog); }); }; }; // Extends summernote $.extend(true, $.summernote, { plugins: { databasic: DataBasicPlugin, }, options: { popover: { databasic: [ ['databasic', ['databasicDialog', 'databasicSize100', 'databasicSize50', 'databasicSize25']], ], }, }, // add localization texts lang: { 'en-US': { databasic: { name: 'Basic Data Container', insert: 'insert basic data container', edit: 'edit basic data container', testLabel: 'test input', }, }, }, }); }));