/* https://github.com/DiemenDesign/summernote-image-attributes */ (function (factory) { if (typeof define === 'function' && define.amd) { define(['jquery'], factory); } else if (typeof module === 'object' && module.exports) { module.exports = factory(require('jquery')); } else { factory(window.jQuery); } }(function ($) { var readFileAsDataURL = function (file) { return $.Deferred( function (deferred) { $.extend(new FileReader(),{ onload: function (e) { var sDataURL = e.target.result; deferred.resolve(sDataURL); }, onerror: function () { deferred.reject(this); } }).readAsDataURL(file); }).promise(); }; $.extend(true,$.summernote.lang, { 'en-US': { /* US English(Default Language) */ imageAttributes: { dialogTitle: 'Image Attributes', tooltip: 'Image Attributes', tabImage: 'Image', src: 'Source', browse: 'Browse', title: 'Title', alt: 'Alt Text', dimensions: 'Dimensions', tabAttributes: 'Attributes', class: 'Class', style: 'Style', role: 'Role', tabLink: 'Link', linkHref: 'URL', linkTarget: 'Target', linkTargetInfo: 'Options: _self, _blank, _top, _parent', linkClass: 'Class', linkStyle: 'Style', linkRel: 'Rel', linkRelInfo: 'Options: alternate, author, bookmark, help, license, next, nofollow, noreferrer, prefetch, prev, search, tag', linkRole: 'Role', tabUpload: 'Upload', upload: 'Upload', tabBrowse: 'Browse', editBtn: 'OK' } } }); $.extend($.summernote.options, { imageAttributes: { icon: '', removeEmpty: true, disableUpload: false, imageFolder: '' } }); $.extend($.summernote.plugins, { 'imageAttributes': function (context) { var self = this, ui = $.summernote.ui, $note = context.layoutInfo.note, $editor = context.layoutInfo.editor, $editable = context.layoutInfo.editable, options = context.options, lang = options.langInfo, imageAttributesLimitation = ''; if (options.maximumImageFileSize) { var unit = Math.floor(Math.log(options.maximumImageFileSize) / Math.log(1024)); var readableSize = (options.maximumImageFileSize/Math.pow(1024,unit)).toFixed(2) * 1 + ' ' + ' KMGTP'[unit] + 'B'; imageAttributesLimitation = '' + lang.image.maximumFileSize + ' : ' + readableSize+''; } if(! ('_counter' in $.summernote.options.imageAttributes)) { $.summernote.options.imageAttributes._counter = 0; } context.memo('button.imageAttributes', function() { var button = ui.button({ contents: options.imageAttributes.icon, container: "body", tooltip: lang.imageAttributes.tooltip, click: function () { context.invoke('imageAttributes.show'); } }); return button.render(); }); this.initialize = function () { var $container = options.dialogsInBody ? $(document.body) : $editor; $.summernote.options.imageAttributes._counter++; var i = $.summernote.options.imageAttributes._counter; // console.log('indice for imageAttribute : ', i); var body = '' + '
' + // Tab 2 '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + // Tab 3 ''; if (options.imageAttributes.disableUpload == false) { // Tab 4 body += '
' + '' + '
' + '' + imageAttributesLimitation + '
' + '
'; } // Tab 1 body += '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + 'x' + '' + '
' + '
' + '
' + '
'; this.$dialog=ui.dialog({ title: lang.imageAttributes.dialogTitle, body: body, footer: '' }).render().appendTo($container); }; this.destroy = function () { ui.hideDialog(this.$dialog); this.$dialog.remove(); }; this.bindEnterKey = function ($input,$btn) { $input.on('keypress', function (e) { if (e.keyCode === 13) $btn.trigger('click'); }); }; this.bindLabels = function () { self.$dialog.find('.form-control:first').focus().select(); self.$dialog.find('label').on('click', function () { $(this).parent().find('.form-control:first').focus(); }); }; this.show = function () { var $img = $($editable.data('target')); var imgInfo = { imgDom: $img, title: $img.attr('title'), src: $img.attr('src'), alt: $img.attr('alt'), width: $img.attr('width'), height: $img.attr('height'), role: $img.attr('role'), class: $img.attr('class'), style: $img.attr('style'), imgLink: $($img).parent().is("a") ? $($img).parent() : null }; this.showImageAttributesDialog(imgInfo).then( function (imgInfo) { ui.hideDialog(self.$dialog); var $img = imgInfo.imgDom; if (options.imageAttributes.removeEmpty) { if (imgInfo.alt) $img.attr('alt', imgInfo.alt); else $img.removeAttr('alt'); if (imgInfo.width) $img.attr('width', imgInfo.width); else $img.removeAttr('width'); if (imgInfo.height) $img.attr('height',imgInfo.height); else $img.removeAttr('height'); if (imgInfo.title) $img.attr('title', imgInfo.title); else $img.removeAttr('title'); if (imgInfo.src) $img.attr('src', imgInfo.src); else $img.attr('src', '#'); if (imgInfo.class) $img.attr('class', imgInfo.class); else $img.removeAttr('class'); if (imgInfo.style) $img.attr('style', imgInfo.style); else $img.removeAttr('style'); if (imgInfo.role) $img.attr('role', imgInfo.role); else $img.removeAttr('role'); } else { if (imgInfo.src) $img.attr('src', imgInfo.src); else $img.attr('src', '#'); $img.attr('alt', imgInfo.alt); $img.attr('width', imgInfo.width); $img.attr('height', imgInfo.height); $img.attr('title', imgInfo.title); $img.attr('class', imgInfo.class); $img.attr('style', imgInfo.style); $img.attr('role', imgInfo.role); } if($img.parent().is("a")) $img.unwrap(); if (imgInfo.linkHref) { var linkBody = '