/*!
* medium-editor-insert-plugin v2.4.1 - jQuery insert plugin for MediumEditor
*
* http://linkesch.com/medium-editor-insert-plugin
*
* Copyright (c) 2014 Pavel Linkesch (http://linkesch.com)
* Released under the MIT license
*/
!function(a){"function"==typeof define&&define.amd?define(["jquery","handlebars/runtime","medium-editor","blueimp-file-upload","jquery-sortable"],a):"object"==typeof module&&module.exports?module.exports=function(b){if("undefined"==typeof window)throw new Error("medium-editor-insert-plugin runs only in a browser.");return void 0===b&&(b=require("jquery")),window.jQuery=b,Handlebars=require("handlebars/runtime"),MediumEditor=require("medium-editor"),require("jquery-sortable"),require("blueimp-file-upload"),a(b,Handlebars,MediumEditor),b}:a(jQuery,Handlebars,MediumEditor)}(function(a,b,c){this.MediumInsert=this.MediumInsert||{},this.MediumInsert.Templates=this.MediumInsert.Templates||{},this.MediumInsert.Templates["src/js/templates/core-buttons.hbs"]=b.template({1:function(a,b,c,d,e){var f,g,h=null!=b?b:{},i=c.helperMissing,j="function";return'
'+(null!=(g=null!=(g=c.label||(null!=b?b.label:b))?g:i,f=typeof g===j?g.call(h,{name:"label",hash:{},data:e}):g)?f:"")+" \n"},compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f;return'\n"},useData:!0}),this.MediumInsert.Templates["src/js/templates/core-caption.hbs"]=b.template({compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f;return' '},useData:!0}),this.MediumInsert.Templates["src/js/templates/core-empty-line.hbs"]=b.template({compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){return"
\n"},useData:!0}),this.MediumInsert.Templates["src/js/templates/embeds-toolbar.hbs"]=b.template({1:function(a,b,c,d,e){var f;return' \n"},2:function(a,b,c,d,e){var f;return null!=(f=c.if.call(null!=b?b:{},null!=b?b.label:b,{name:"if",hash:{},fn:a.program(3,e,0),inverse:a.noop,data:e}))?f:""},3:function(a,b,c,d,e){var f,g,h=null!=b?b:{},i=c.helperMissing,j="function";return' \n '+(null!=(g=null!=(g=c.label||(null!=b?b.label:b))?g:i,f=typeof g===j?g.call(h,{name:"label",hash:{},data:e}):g)?f:"")+" \n \n"},5:function(a,b,c,d,e){var f;return' \n"},compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f,g=null!=b?b:{};return(null!=(f=c.if.call(g,null!=b?b.styles:b,{name:"if",hash:{},fn:a.program(1,e,0),inverse:a.noop,data:e}))?f:"")+"\n"+(null!=(f=c.if.call(g,null!=b?b.actions:b,{name:"if",hash:{},fn:a.program(5,e,0),inverse:a.noop,data:e}))?f:"")},useData:!0}),this.MediumInsert.Templates["src/js/templates/embeds-wrapper.hbs"]=b.template({compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f,g;return'\n\t
\n\t\t\n\t\t\t'+(null!=(g=null!=(g=c.html||(null!=b?b.html:b))?g:c.helperMissing,f="function"==typeof g?g.call(null!=b?b:{},{name:"html",hash:{},data:e}):g)?f:"")+'\n\t\t
\n\t \n\t
\n
'},useData:!0}),this.MediumInsert.Templates["src/js/templates/images-fileupload.hbs"]=b.template({compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){return' '},useData:!0}),this.MediumInsert.Templates["src/js/templates/images-image.hbs"]=b.template({1:function(a,b,c,d,e){return'
\n'},compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f,g,h=null!=b?b:{};return'\n \n'+(null!=(f=c.if.call(h,null!=b?b.progress:b,{name:"if",hash:{},fn:a.program(1,e,0),inverse:a.noop,data:e}))?f:"")+" \n"},useData:!0}),this.MediumInsert.Templates["src/js/templates/images-progressbar.hbs"]=b.template({compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){return'0 '},useData:!0}),this.MediumInsert.Templates["src/js/templates/images-toolbar.hbs"]=b.template({1:function(a,b,c,d,e){var f;return null!=(f=c.if.call(null!=b?b:{},null!=b?b.label:b,{name:"if",hash:{},fn:a.program(2,e,0),inverse:a.noop,data:e}))?f:""},2:function(a,b,c,d,e){var f,g,h=null!=b?b:{},i=c.helperMissing,j="function";return' \n '+(null!=(g=null!=(g=c.label||(null!=b?b.label:b))?g:i,f=typeof g===j?g.call(h,{name:"label",hash:{},data:e}):g)?f:"")+" \n \n"},4:function(a,b,c,d,e){var f;return'\t\n"},5:function(a,b,c,d,e){var f;return null!=(f=c.if.call(null!=b?b:{},null!=b?b.label:b,{name:"if",hash:{},fn:a.program(6,e,0),inverse:a.noop,data:e}))?f:""},6:function(a,b,c,d,e){var f,g,h=null!=b?b:{},i=c.helperMissing,j="function";return' \t \n \t '+(null!=(g=null!=(g=c.label||(null!=b?b.label:b))?g:i,f=typeof g===j?g.call(h,{name:"label",hash:{},data:e}):g)?f:"")+" \n \t \n"},compiler:[7,">= 4.0.0"],main:function(a,b,c,d,e){var f,g=null!=b?b:{};return'\n\n"+(null!=(f=c.if.call(g,null!=b?b.actions:b,{name:"if",hash:{},fn:a.program(4,e,0),inverse:a.noop,data:e}))?f:"")},useData:!0}),function(a,b,c,d){"use strict";function e(a){return a.charAt(0).toUpperCase()+a.slice(1)}function f(c,e){var f;this.el=c,this.$el=a(c),this.templates=b.MediumInsert.Templates,e&&(f=e.editor,e.editor=null),this.options=a.extend(!0,{},h,e),this.options.editor=f,this._defaults=h,this._name=g,this.options&&this.options.editor&&(this.options.editor._serialize===d&&(this.options.editor._serialize=this.options.editor.serialize),this.options.editor._destroy===d&&(this.options.editor._destroy=this.options.editor.destroy),this.options.editor._setup===d&&(this.options.editor._setup=this.options.editor.setup),this.options.editor._hideInsertButtons=this.hideButtons,this.options.editor.serialize=this.editorSerialize,this.options.editor.destroy=this.editorDestroy,this.options.editor.setup=this.editorSetup,this.options.editor.getExtensionByName("placeholder")!==d&&(this.options.editor.getExtensionByName("placeholder").updatePlaceholder=this.editorUpdatePlaceholder))}var g="mediumInsert",h={editor:null,enabled:!0,addons:{images:!0,embeds:!0}};f.prototype.init=function(){this.$el.addClass("medium-editor-insert-plugin"),"object"==typeof this.options.addons&&0!==Object.keys(this.options.addons).length||this.disable(),this.initAddons(),this.clean(),this.events()},f.prototype.events=function(){var c=this;this.$el.on("dragover drop",function(a){a.preventDefault()}).on("keyup click",a.proxy(this,"toggleButtons")).on("selectstart mousedown",".medium-insert, .medium-insert-buttons",a.proxy(this,"disableSelection")).on("click",".medium-insert-buttons-show",a.proxy(this,"toggleAddons")).on("click",".medium-insert-action",a.proxy(this,"addonAction")).on("paste",".medium-insert-caption-placeholder",function(b){a.proxy(c,"removeCaptionPlaceholder")(a(b.target))}),a(b).on("resize",a.proxy(this,"positionButtons",null))},f.prototype.getEditor=function(){return this.options.editor},f.prototype.editorSerialize=function(){var b=this._serialize();return a.each(b,function(c){var d=a("
").html(b[c].value);d.find(".medium-insert-buttons").remove(),d.find(".medium-insert-active").removeClass("medium-insert-active"),d.find("[data-embed-code]").each(function(){var b=a(this),c=a("
").html(b.attr("data-embed-code")).text();b.html(c)}),b[c].value=d.html()}),b},f.prototype.editorDestroy=function(){a.each(this.elements,function(b,c){a(c).data("plugin_"+g)instanceof f&&a(c).data("plugin_"+g).disable()}),this._destroy()},f.prototype.editorSetup=function(){this._setup(),a.each(this.elements,function(b,c){a(c).data("plugin_"+g)instanceof f&&a(c).data("plugin_"+g).enable()})},f.prototype.editorUpdatePlaceholder=function(b,c){var d=a(b).children().not(".medium-insert-buttons").contents();c||1!==d.length||"br"!==d[0].nodeName.toLowerCase()?this.hidePlaceholder(b):(this.showPlaceholder(b),this.base._hideInsertButtons(a(b)))},f.prototype.triggerInput=function(){this.getEditor()&&this.getEditor().trigger("editableInput",null,this.el)},f.prototype.deselect=function(){c.getSelection().removeAllRanges()},f.prototype.disable=function(){this.options.enabled=!1,this.$el.find(".medium-insert-buttons").addClass("hide")},f.prototype.enable=function(){this.options.enabled=!0,this.$el.find(".medium-insert-buttons").removeClass("hide")},f.prototype.disableSelection=function(b){var c=a(b.target);(!1===c.is("img")||c.hasClass("medium-insert-buttons-show"))&&b.preventDefault()},f.prototype.initAddons=function(){var b=this;this.options.addons&&0!==this.options.addons.length&&a.each(this.options.addons,function(a,c){var d=g+e(a);if(!1===c)return void delete b.options.addons[a];b.$el[d](c),b.options.addons[a]=b.$el.data("plugin_"+d).options})},f.prototype.clean=function(){var b,c,d,e=this;!1!==this.options.enabled&&(0===this.$el.children().length&&this.$el.html(this.templates["src/js/templates/core-empty-line.hbs"]().trim()),d=this.$el.contents().filter(function(){return"#text"===this.nodeName&&""!==a.trim(a(this).text())||"br"===this.nodeName.toLowerCase()}),d.each(function(){a(this).wrap("
"),e.moveCaret(a(this).parent(),a(this).text().length)}),this.addButtons(),b=this.$el.find(".medium-insert-buttons"),c=b.prev(),c.attr("class")&&c.attr("class").match(/medium\-insert(?!\-active)/)&&b.before(this.templates["src/js/templates/core-empty-line.hbs"]().trim()))},f.prototype.getButtons=function(){if(!1!==this.options.enabled)return this.templates["src/js/templates/core-buttons.hbs"]({addons:this.options.addons}).trim()},f.prototype.addButtons=function(){0===this.$el.find(".medium-insert-buttons").length&&this.$el.append(this.getButtons())},f.prototype.toggleButtons=function(c){var d,e,f,g,h=a(c.target),i=b.getSelection(),j=this;!1!==this.options.enabled&&(i&&0!==i.rangeCount?(d=i.getRangeAt(0),e=a(d.commonAncestorContainer)):e=h,e.hasClass("medium-editor-insert-plugin")&&(e=e.find("p:first")),f=e.is("p")?e:e.closest("p"),this.clean(),!1===h.hasClass("medium-editor-placeholder")&&0===h.closest(".medium-insert-buttons").length&&0===e.closest(".medium-insert-buttons").length&&(this.$el.find(".medium-insert-active").removeClass("medium-insert-active"),a.each(this.options.addons,function(a){if(h.closest(".medium-insert-"+a).length&&(e=h),e.closest(".medium-insert-"+a).length)return f=e.closest(".medium-insert-"+a),void(g=a)}),f.length&&(""===f.text().trim()&&!g||"images"===g)?(f.addClass("medium-insert-active"),"images"===g?this.$el.find(".medium-insert-buttons").attr("data-active-addon",g):this.$el.find(".medium-insert-buttons").removeAttr("data-active-addon"),setTimeout(function(){j.positionButtons(g),j.showButtons(g)},g?100:0)):this.hideButtons()))},f.prototype.showButtons=function(a){var b=this.$el.find(".medium-insert-buttons");b.show(),b.find("li").show(),a&&(b.find("li").hide(),b.find('button[data-addon="'+a+'"]').parent().show())},f.prototype.hideButtons=function(a){a=a||this.$el,a.find(".medium-insert-buttons").hide(),a.find(".medium-insert-buttons-addons").hide(),a.find(".medium-insert-buttons-show").removeClass("medium-insert-buttons-rotate")},f.prototype.positionButtons=function(c){var d=this.$el.find(".medium-insert-buttons"),e=this.$el.find(".medium-insert-active"),f=e.hasClass("medium-insert-images-grid")?[]:e.find("figure:last figcaption"),g=this.getEditor()?this.getEditor().options.elementsContainer:a("body").get(0),h=["absolute","fixed"].indexOf(b.getComputedStyle(g).getPropertyValue("position"))>-1,i={};e.length&&(i.left=e.position().left,i.top=e.position().top,c?(i.left+=e.width()-d.find(".medium-insert-buttons-show").width()-10,i.top+=e.height()-20+(f.length?-f.height()-parseInt(f.css("margin-top"),10):10)):(i.left+=-parseInt(d.find(".medium-insert-buttons-addons").css("left"),10)-parseInt(d.find(".medium-insert-buttons-addons button:first").css("margin-left"),10),i.top+=parseInt(e.css("margin-top"),10)),h&&(i.top+=g.scrollTop),!1===this.$el.hasClass("medium-editor-placeholder")&&i.left<0&&(i.left=e.position().left),d.css(i))},f.prototype.toggleAddons=function(){if("images"===this.$el.find(".medium-insert-buttons").attr("data-active-addon"))return void this.$el.find(".medium-insert-buttons").find('button[data-addon="images"]').click();this.$el.find(".medium-insert-buttons-addons").fadeToggle(),this.$el.find(".medium-insert-buttons-show").toggleClass("medium-insert-buttons-rotate")},f.prototype.hideAddons=function(){this.$el.find(".medium-insert-buttons-addons").hide(),this.$el.find(".medium-insert-buttons-show").removeClass("medium-insert-buttons-rotate")},f.prototype.addonAction=function(b){var c=a(b.currentTarget),d=c.data("addon"),f=c.data("action");this.$el.data("plugin_"+g+e(d))[f]()},f.prototype.moveCaret=function(a,d){var e,f,g,h;d=d||0,e=c.createRange(),f=b.getSelection(),g=a.get(0),g.childNodes.length||(h=c.createTextNode(" "),g.appendChild(h)),e.setStart(g.childNodes[0],d),e.collapse(!0),f.removeAllRanges(),f.addRange(e)},f.prototype.addCaption=function(a,b){0===a.find("figcaption").length&&a.append(this.templates["src/js/templates/core-caption.hbs"]({placeholder:b}))},f.prototype.removeCaptions=function(b){var c=this.$el.find("figcaption");b&&(c=c.not(b)),c.each(function(){(a(this).hasClass("medium-insert-caption-placeholder")||""===a(this).text().trim())&&a(this).remove()})},f.prototype.removeCaptionPlaceholder=function(a){var b=a.is("figcaption")?a:a.find("figcaption");b.length&&b.removeClass("medium-insert-caption-placeholder").removeAttr("data-placeholder")},a.fn[g]=function(b){return this.each(function(){var c,d=this;a(d).is("textarea")&&(c=a(d).attr("medium-editor-textarea-id"),d=a(d).siblings('[medium-editor-textarea-id="'+c+'"]').get(0)),a.data(d,"plugin_"+g)?"string"==typeof b&&a.data(d,"plugin_"+g)[b]&&a.data(d,"plugin_"+g)[b]():(a.data(d,"plugin_"+g,new f(d,b)),a.data(d,"plugin_"+g).init())})}}(jQuery,window,document),function(a,b,c,d){"use strict";function e(c,d){this.el=c,this.$el=a(c),this.templates=b.MediumInsert.Templates,this.core=this.$el.data("plugin_"+f),this.options=a.extend(!0,{},h,d),this._defaults=h,this._name=f,this.core.getEditor()&&(this.core.getEditor()._serializePreEmbeds=this.core.getEditor().serialize,this.core.getEditor().serialize=this.editorSerialize),this.init()}var f="mediumInsert",g="Embeds",h={label:' ',placeholder:"Paste a YouTube, Vimeo, Facebook, Twitter or Instagram link and press Enter",oembedProxy:"http://medium.iframe.ly/api/oembed?iframe=1",captions:!0,captionPlaceholder:"Type caption (optional)",storeMeta:!1,styles:{wide:{label:' '},left:{label:' '},right:{label:' '}},actions:{remove:{label:' ',clicked:function(){var b=a.Event("keydown");b.which=8,a(c).trigger(b)}}},parseOnPaste:!1};e.prototype.init=function(){var b=this.$el.find(".medium-insert-embeds");b.attr("contenteditable",!1),b.each(function(){0===a(this).find(".medium-insert-embeds-overlay").length&&a(this).append(a("
").addClass("medium-insert-embeds-overlay"))}),this.events(),this.backwardsCompatibility()},e.prototype.events=function(){a(c).on("click",a.proxy(this,"unselectEmbed")).on("keydown",a.proxy(this,"removeEmbed")).on("click",".medium-insert-embeds-toolbar .medium-editor-action",a.proxy(this,"toolbarAction")).on("click",".medium-insert-embeds-toolbar2 .medium-editor-action",a.proxy(this,"toolbar2Action")),this.$el.on("keyup click paste",a.proxy(this,"togglePlaceholder")).on("keydown",a.proxy(this,"processLink")).on("click",".medium-insert-embeds-overlay",a.proxy(this,"selectEmbed")).on("contextmenu",".medium-insert-embeds-placeholder",a.proxy(this,"fixRightClickOnPlaceholder")),this.options.parseOnPaste&&this.$el.on("paste",a.proxy(this,"processPasted")),a(b).on("resize",a.proxy(this,"autoRepositionToolbars"))},e.prototype.backwardsCompatibility=function(){var b=this;this.$el.find(".mediumInsert-embeds").removeClass("mediumInsert-embeds").addClass("medium-insert-embeds"),this.$el.find(".medium-insert-embeds").each(function(){0===a(this).find(".medium-insert-embed").length&&(a(this).after(b.templates["src/js/templates/embeds-wrapper.hbs"]({html:a(this).html()})),a(this).remove())})},e.prototype.editorSerialize=function(){var b=this._serializePreEmbeds();return a.each(b,function(c){var d=a("
").html(b[c].value);d.find(".medium-insert-embeds").removeAttr("contenteditable"),d.find(".medium-insert-embeds-overlay").remove(),b[c].value=d.html()}),b},e.prototype.add=function(){var a=this.$el.find(".medium-insert-active");a.html(this.templates["src/js/templates/core-empty-line.hbs"]().trim()),a.is("p")&&(a.replaceWith(''+a.html()+"
"),a=this.$el.find(".medium-insert-active"),this.core.moveCaret(a)),a.addClass("medium-insert-embeds medium-insert-embeds-input medium-insert-embeds-active"),this.togglePlaceholder({target:a.get(0)}),a.click(),this.core.hideButtons()},e.prototype.togglePlaceholder=function(c){var d,e,f,g=a(c.target),h=b.getSelection();h&&0!==h.rangeCount&&(d=h.getRangeAt(0),e=a(d.commonAncestorContainer),e.hasClass("medium-insert-embeds-active")?g=e:e.closest(".medium-insert-embeds-active").length&&(g=e.closest(".medium-insert-embeds-active")),g.hasClass("medium-insert-embeds-active")?(f=g.text().trim(),""===f&&!1===g.hasClass("medium-insert-embeds-placeholder")?g.addClass("medium-insert-embeds-placeholder").attr("data-placeholder",this.options.placeholder):""!==f&&g.hasClass("medium-insert-embeds-placeholder")&&g.removeClass("medium-insert-embeds-placeholder").removeAttr("data-placeholder")):this.$el.find(".medium-insert-embeds-active").remove())},e.prototype.fixRightClickOnPlaceholder=function(b){this.core.moveCaret(a(b.target))},e.prototype.processLink=function(a){var b,c=this.$el.find(".medium-insert-embeds-active");if(c.length){if(""===(b=c.text().trim())&&-1!==[8,46,13].indexOf(a.which))return void c.remove();13===a.which&&(a.preventDefault(),a.stopPropagation(),this.options.oembedProxy?this.oembed(b):this.parseUrl(b))}},e.prototype.processPasted=function(b){var c,d;a(".medium-insert-embeds-active").length||(c=b.originalEvent.clipboardData.getData("text"),d=new RegExp("^(http(s?):)?//","i"),d.test(c)&&(this.options.oembedProxy?this.oembed(c,!0):this.parseUrl(c,!0)))},e.prototype.oembed=function(c,d){var e=this;a.support.cors=!0,a.ajax({crossDomain:!0,cache:!1,url:this.options.oembedProxy,dataType:"json",data:{url:c},success:function(b){var f=b&&b.html;if(e.options.storeMeta&&(f+='