/*! * medium-editor-insert-plugin v0.3.2 - jQuery insert plugin for MediumEditor * * https://github.com/orthes/medium-editor-insert-plugin * * Copyright (c) 2014 Pavel Linkesch (http://linkesch.sk) * Released under the MIT license */ /* global MediumEditor */ (function ($) { /* * Private storage of registered addons */ var addons = {}; /** * Extend MediumEditor functions if the editor exists */ if (MediumEditor && typeof(MediumEditor) === "function") { /** * Extend MediumEditor's serialize function to get rid of unnecesarry Medium Editor Insert Plugin stuff * * @return {object} content Object containing HTML content of each element */ MediumEditor.prototype.serialize = function () { var i, j, elementid, content = {}, $clone, $inserts, $insert, $insertData, html; for (i = 0; i < this.elements.length; i += 1) { elementid = (this.elements[i].id !== '') ? this.elements[i].id : 'element-' + i; $clone = $(this.elements[i]).clone(); $inserts = $('.mediumInsert', $clone); for (j = 0; j < $inserts.length; j++) { $insert = $($inserts[j]); $insertData = $('.mediumInsert-placeholder', $insert).children(); if ($insertData.length === 0) { $insert.remove(); } else { $insert.removeAttr('contenteditable'); $('img[draggable]', $insert).removeAttr('draggable'); if ($insert.hasClass('small')) { $insertData.addClass('small'); } $('.mediumInsert-buttons', $insert).remove(); $insertData.unwrap(); } } html = $clone.html().trim(); content[elementid] = { value: html }; } return content; }; /** * Extend MediumEditor's deactivate function to call $.fn.mediumInsert.insert.disable function * * @return {void} */ MediumEditor.prototype.deactivate = function () { var i; if (!this.isActive) { return false; } this.isActive = false; if (this.toolbar !== undefined) { this.toolbar.style.display = 'none'; } document.documentElement.removeEventListener('mouseup', this.checkSelectionWrapper); for (i = 0; i < this.elements.length; i += 1) { this.elements[i].removeEventListener('keyup', this.checkSelectionWrapper); this.elements[i].removeEventListener('blur', this.checkSelectionWrapper); this.elements[i].removeAttribute('contentEditable'); } if ($.fn.mediumInsert.insert.$el) $.fn.mediumInsert.insert.$el.mediumInsert('disable'); }; /** * Extend MediumEditor's activate function to call $.fn.mediumInsert.insert.enable function * * @return {void} */ MediumEditor.prototype.activate = function () { var i; if (this.isActive) { return false; } if (this.toolbar !== undefined) { this.toolbar.style.display = 'block'; } this.isActive = true; for (i = 0; i < this.elements.length; i += 1) { this.elements[i].setAttribute('contentEditable', true); } this.bindSelect(); if ($.fn.mediumInsert.insert.$el) $.fn.mediumInsert.insert.$el.mediumInsert('enable'); }; } /** * Medium Editor Insert Plugin * * @param {object} options Options for the plugin * @param {void} */ $.fn.mediumInsert = function (options) { if (typeof options === 'string' && $.fn.mediumInsert.insert[options]) { $.fn.mediumInsert.insert[options](); } else { $.fn.mediumInsert.settings = $.extend($.fn.mediumInsert.settings, options); /** * Initial plugin loop */ return this.each(function () { $(this).addClass('medium-editor-insert-plugin'); var blocks = 'p, h1, h2, h3, h4, h5, h6, ol, ul, blockquote'; $(this).on('dragover drop', blocks, function (e) { e.preventDefault(); return false; }); $.fn.mediumInsert.insert.init($(this)); $.each($.fn.mediumInsert.settings.addons, function (i) { if (typeof addons[i] !== 'undefined') { var addonOptions = $.fn.mediumInsert.settings.addons[i]; addonOptions.$el = $.fn.mediumInsert.insert.$el; addons[i].init(addonOptions); } }); }); } }; /** * Settings */ $.fn.mediumInsert.settings = { enabled: true, beginning: false, addons: { images: {}, embeds: {} } }; /** * Register new addon */ $.fn.mediumInsert.registerAddon = function(name, addon){ addons[name] = addon; }; /** * Get registered addon */ $.fn.mediumInsert.getAddon = function(name){ return addons[name]; }; /** * Addon Initialization */ $.fn.mediumInsert.insert = { /** * Insert initial function * * @param {element} el Parent container element * @return {void} */ init: function ($el) { this.$el = $el; this.isFirefox = navigator.userAgent.match(/firefox/i); this.setPlaceholders(); this.setEvents(); }, /** * Deselect selected text * * @return {void} */ deselect: function () { document.getSelection().removeAllRanges(); }, /** * Disable the plugin * * @return {void} */ disable: function () { $.fn.mediumInsert.settings.enabled = false; $.fn.mediumInsert.insert.$el.find('.mediumInsert-buttons').addClass('hide'); }, /** * Enable the plugin * * @return {void} */ enable: function () { $.fn.mediumInsert.settings.enabled = true; $.fn.mediumInsert.insert.$el.find('.mediumInsert-buttons').removeClass('hide'); }, /** * Return max id in #mediumInsert-* * * @return {int} max (Max number, -1 if no placeholders exist) */ getMaxId: function () { var max = -1; $('div[id^="mediumInsert-"]').each(function () { var id = parseInt($(this).attr('id').split('-')[1], 10); if (id > max) { max = id; } }); return max; }, /** * Return insert buttons optionally filtered by addon name * * @param {string} addon Addon name of addon to display only * @return {void} */ getButtons: function (addon) { var editor = $.fn.mediumInsert.settings.editor, buttonLabels = (editor && editor.options) ? editor.options.buttonLabels : ''; var buttons; if($.fn.mediumInsert.settings.enabled) { buttons = '
'+ '+'+ '