/** * * copyright 2018 [Taal & Digitaal | Hendri Smit]. * email: hen3smit@gmail.com * license: MIT * */ (function (factory) { /* Global define */ 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 ($) { $.extend(true, $.summernote.lang, { 'en-US': { audio: { audio: 'Audio', insert: 'Insert Audio', selectFromFiles: 'Select from files', url: 'Audio URL', maximumFileSize: 'Maximum file size', maximumFileSizeError: 'Maximum file size exceeded.' } }, 'nl-NL': { audio: { audio: 'Audio', insert: 'Audio invoegen', selectFromFiles: 'Selecteer een bestand', url: 'URL van de audio', maximumFileSize: 'Maximale bestandsgrootte', maximumFileSizeError: 'Bestand te groot.' } }, }); $.extend($.summernote.options, { audio: { icon: '' }, callbacks: { onAudioUpload: null, onAudioUploadError: null, onAudioLinkInsert: null } }); $.extend($.summernote.plugins, { /** * @param {Object} context - context object has status of editor. */ 'audio': function (context) { var self = this, // ui has renders to build ui elements // for e.g. you can create a button with 'ui.button' ui = $.summernote.ui, $note = context.layoutInfo.note, // contentEditable element $editor = context.layoutInfo.editor, $editable = context.layoutInfo.editable, $toolbar = context.layoutInfo.toolbar, // options holds the Options Information from Summernote and what we extended above. options = context.options, // lang holds the Language Information from Summernote and what we extended above. lang = options.langInfo; context.memo('button.audio', function () { // Here we create a button var button = ui.button({ // icon for button contents: options.audio.icon, // tooltip for button tooltip: lang.audio.audio, click: function (e) { context.invoke('audio.show'); } }); return button.render(); }); this.initialize = function () { // This is how we can add a Modal Dialog to allow users to interact with the Plugin. // get the correct container for the plugin how it's attached to the document DOM. var $container = options.dialogsInBody ? $(document.body) : $editor; var audioLimitation = ''; if (options.maximumAudioFileSize) { var unit = Math.floor(Math.log(options.maximumAudioFileSize) / Math.log(1024)); var readableSize = (options.maximumAudioFileSize / Math.pow(1024, unit)).toFixed(2) * 1 + ' ' + ' KMGTP'[unit] + 'B'; audioLimitation = '' + lang.audio.maximumFileSize + ' : ' + readableSize + ''; } // Build the Body HTML of the Dialog. var body = [ '
', '', '', '
', audioLimitation, '
', '', '', '
' ].join(''); // Build the Footer HTML of the Dialog. var footer = ''; this.$dialog = ui.dialog({ // Set the title for the Dialog. Note: We don't need to build the markup for the Modal // Header, we only need to set the Title. title: lang.audio.insert, // Set the Body of the Dialog. body: body, // Set the Footer of the Dialog. footer: footer // This adds the Modal to the DOM. }).render().appendTo($container); }; this.destroy = function () { ui.hideDialog(this.$dialog); this.$dialog.remove(); }; this.bindEnterKey = function ($input, $btn) { $input.on('keypress', function (event) { if (event.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(); }); }; /** * @method readFileAsDataURL * * read contents of file as representing URL * * @param {File} file * @return {Promise} - then: dataUrl * * @todo this method already exists in summernote.js so we should use that one */ this.readFileAsDataURL = function (file) { return $.Deferred(function (deferred) { $.extend(new FileReader(), { onload: function (e) { var dataURL = e.target.result; deferred.resolve(dataURL); }, onerror: function (err) { deferred.reject(err); } }).readAsDataURL(file); }).promise(); }; this.createAudio = function (url) { // audio url patterns (mp3, ogg) var mp3RegExp = /^.+.(mp3)$/; var mp3Match = url.match(mp3RegExp); var oggRegExp = /^.+.(ogg|oga)$/; var oggMatch = url.match(oggRegExp); var base64RegExp = /^data:(audio\/mpeg|audio\/ogg).+$/; var base64Match = url.match(base64RegExp); var $audio; if (mp3Match || oggMatch || base64Match) { $audio = $('