window.wp=window.wp||{};(function(){wp.html=_.extend(wp.html||{},{attrs:function(c){var a,b;if("/"===c[c.length-1]){c=c.slice(0,-1)}a=wp.shortcode.attrs(c);b=a.named;_.each(a.numeric,function(d){if(/\s/.test(d)){return}b[d]=""});return b},string:function(a){var c="<"+a.tag,b=a.content||"";_.each(a.attrs,function(e,d){c+=" "+d;if(""===e){return}if(_.isBoolean(e)){e=e?"true":"false"}c+='="'+e+'"'});if(a.single){return c+" />"}c+=">";c+=_.isObject(b)?wp.html.string(b):b;return c+""+a.tag+">"}})}());(function(b){var a={},c={};wp.mce=wp.mce||{};wp.mce.view={defaults:{pattern:{view:Backbone.View,text:function(d){return d.options.original},toView:function(e){if(!this.pattern){return}this.pattern.lastIndex=0;var d=this.pattern.exec(e);if(!d){return}return{index:d.index,content:d[0],options:{original:d[0],results:d}}}},shortcode:{view:Backbone.View,text:function(d){return d.options.shortcode.string()},toView:function(e){var d=wp.shortcode.next(this.shortcode,e);if(!d){return}return{index:d.index,content:d.content,options:{shortcode:d.shortcode}}}}},add:function(i,e){var g,d,h,f;if(e.extend){g=wp.mce.view.get(e.extend)}else{if(e.shortcode){g=wp.mce.view.defaults.shortcode}else{g=wp.mce.view.defaults.pattern}}_.defaults(e,g);e.id=i;f={remove:function(){delete c[this.el.id];this.$el.parent().remove();if(d){d.apply(this,arguments)}return this}};if(_.isFunction(e.view)){h=e.view}else{h=g.view;d=e.view.remove;_.defaults(f,e.view)}if(!d&&!h._mceview){d=h.prototype.remove}e.view=h.extend(f,{_mceview:true});a[i]=e},get:function(d){return a[d]},remove:function(d){delete a[d]},toViews:function(e){var d=[{content:e}],f;_.each(a,function(h,g){f=d.slice();d=[];_.each(f,function(k){var j=k.content,i;if(k.processed){d.push(k);return}while(j&&(i=h.toView(j))){if(i.index){d.push({content:j.substring(0,i.index)})}d.push({content:wp.mce.view.toView(g,i.options),processed:true});j=j.slice(i.index+i.content.length)}if(j){d.push({content:j})}})});return _.pluck(d,"content").join("")},toView:function(f,h){var g=wp.mce.view.get(f),e,i,d;if(!g){return""}e=new g.view(_.extend(h||{},{viewType:f}));i=e.el.id=e.el.id||_.uniqueId("__wpmce-");c[i]=e;d="span"===e.tagName?"span":"div";return"<"+d+' class="wp-view-wrap" data-wp-view="'+i+'" contenteditable="false">'+d+">"},render:function(d){b(".wp-view-wrap",d).each(function(){var g=b(this),f=g.data("wp-view"),e=c[f];if(!e){return}e.render();e.$el.detach();g.empty().append(e.el).append('')})},toText:function(d){return d.replace(/<(?:div|span)[^>]+data-wp-view="([^"]+)"[^>]*>.*?]+data-wp-view-end[^>]*><\/span><\/(?:div|span)>/g,function(g,h){var e=c[h],f;if(e){f=wp.mce.view.get(e.options.viewType)}return e&&f?f.text(e):""})},removeInternalAttrs:function(e){var d={};_.each(e,function(g,f){if(-1===f.indexOf("data-mce")){d[f]=g}});return d},attrs:function(d){return wp.mce.view.removeInternalAttrs(wp.html.attrs(d))},l10n:_.isUndefined(_wpMceViewL10n)?{}:_wpMceViewL10n}}(jQuery));(function(b){var a=wp.mce.view;wp.media.string={};wp.media.string.link=function(e){var d=getUserSetting("urlbutton","post"),c={tag:"a",content:e.get("title")||e.get("filename"),attrs:{rel:"attachment wp-att-"+e.id}};c.attrs.href=(d==="file")?e.get("url"):e.get("link");return wp.html.string(c)};wp.media.string.image=function(h,g){var f,c,d,e;h=h.toJSON();g=_.defaults(g||{},{img:{},align:getUserSetting("align","none"),size:getUserSetting("imgsize","medium"),link:getUserSetting("urlbutton","post")});c=_.clone(g.img);f=c["class"]?c["class"].split(/\s+/):[];e=h.sizes?h.sizes[g.size]:{};if(!e){delete g.size;e=h}c.width=e.width;c.height=e.height;c.src=e.url;if(g.align){f.push("align"+g.align)}if(g.size){f.push("size-"+g.size)}f.push("wp-image-"+h.id);c["class"]=_.compact(f).join(" ");d={tag:"img",attrs:c,single:true};if(g.anchor){d={tag:"a",attrs:g.anchor,content:d}}return wp.html.string(d)};a.add("attachment",{pattern:new RegExp("(?:]*)>)?]*class=(?:\"[^\"]*|'[^']*)\\bwp-image-(\\d+)[^>]*)>(?:)?"),text:function(c){var d=_.pick(c,"align","size","link","img","anchor");return wp.media.string.image(c.model,d)},view:{className:"editor-attachment",template:media.template("editor-attachment"),events:{"click .close":"remove"},initialize:function(){var c=this,d=this.options.results,f=d[3],e;this.model=wp.media.model.Attachment.get(f);if(d[1]){this.anchor=a.attrs(d[1])}this.img=a.attrs(d[2]);e=this.img["class"];e=e.replace(/(?:^|\s)wp-image-\d+/,"");e=e.replace(/(?:^|\s)size-(\S+)/,function(g,h){c.size=h;return""});e=e.replace(/(?:^|\s)align(left|center|right|none)(?:\s|$)/,function(g,h){c.align=h;return""});this.img["class"]=e;this.$el.addClass("spinner");this.model.fetch().done(_.bind(this.render,this))},render:function(){var d=this.model.toJSON(),c;if(!d.url){return}c={url:"image"===d.type?d.url:d.icon,uploading:d.uploading};_.extend(c,wp.media.fit({width:d.width,height:d.height,maxWidth:a.l10n.contentWidth}));if(this.size&&d.sizes&&d.sizes[this.size]){_.extend(c,_.pick(d.sizes[this.size],"url","width","height"))}this.$el.html(this.template(c))}}});a.add("gallery",{shortcode:"gallery",gallery:(function(){var c={};return{attachments:function(h,g){var i=h.string(),d=c[i],f,e;delete c[i];if(d){return d}f=h.attrs.named;e=_.pick(f,"orderby","order");e.type="image";e.perPage=-1;if(f.ids){e.post__in=f.ids.split(",");e.orderby="post__in"}else{if(f.include){e.post__in=f.include.split(",")}}if(f.exclude){e.post__not_in=f.exclude.split(",")}if(!e.post__in){e.parent=f.id||g}return media.query(e)},shortcode:function(d){var f=d.props.toJSON(),e=_.pick(f,"include","exclude","orderby","order"),g;e.ids=d.pluck("id");g=new wp.shortcode({tag:"gallery",attrs:e,type:"single"});c[g.string()]=new wp.media.model.Attachments(d.models,{props:f});return g}}}()),view:{className:"editor-gallery",template:media.template("editor-gallery"),parent:b("#post_ID").val(),events:{"click .close":"remove","click .edit":"edit"},initialize:function(){this.update()},update:function(){var c=a.get("gallery");this.attachments=c.gallery.attachments(this.options.shortcode,this.parent);this.attachments.more().done(_.bind(this.render,this))},render:function(){var c,e,d;if(!this.attachments.length){return}e=this.attachments.first().toJSON();d=e.sizes&&e.sizes.thumbnail?e.sizes.thumbnail:e;c={url:d.url,orientation:d.orientation,count:this.attachments.length};this.$el.html(this.template(c))},edit:function(){if(!wp.media.view||this.workflow){return}this.workflow=wp.media({view:"gallery",selection:this.attachments.models,title:a.l10n.editGallery,editing:true,multiple:true,describe:true});this.workflow.on("close",function(){this.workflow.detach();delete this.workflow},this);this.workflow.on("update:gallery",function(d){var c=a.get("gallery");this.options.shortcode=c.gallery.shortcode(d);this.update()},this)}}})}(jQuery));