(function(e){var f=wp.media,d=f.model.Attachment,c=f.model.Attachments,a=f.model.Query,b;b=f.view.l10n=_.isUndefined(_wpMediaViewsL10n)?{}:_wpMediaViewsL10n;f.controller.Workflow=Backbone.Model.extend({defaults:{title:"",multiple:false,view:"library",library:{},selection:[]},initialize:function(){this.createSelection();this._views={};this._pendingViews={};this.modal=new f.view.Modal({controller:this});this.add("library",f.view.Workspace.Library,{collection:f.query(this.get("library"))});this.unset("library");this.add("gallery",f.view.Workspace.Gallery,{collection:this.selection})},add:function(i,h,g){this.remove(i);this._pendingViews[i]={view:h,options:g};this.trigger("add add:"+i,h,g);return this},view:function(h){var g;h=h||this.get("view");g=this._pendingViews[h];if(!this._views[h]&&g){this._views[h]=new g.view(_.extend({controller:this},g.options||{}));delete this._pendingViews[h];this.trigger("init init:"+h,this._views[h])}return this._views[h]},remove:function(g){delete this._views[g];delete this._pendingViews[g];this.trigger("remove remove:"+g);return this},render:function(j,i,h){var g;j=j||this.get("view");if(i){this.add(j,i,h)}g=this.view(j);if(!g){return this}g.render();this.modal.content(g);return this},update:function(){this.close();this.trigger("update",this.selection);this.selection.clear()},createSelection:function(){var g=this;this.selection=new c(this.get("selection"));this.unset("selection");_.extend(this.selection,{add:function(i,h){if(!g.get("multiple")){i=_.isArray(i)?_.first(i):i;this.clear(h)}return c.prototype.add.call(this,i,h)},clear:function(h){return this.remove(this.models,h)},reset:function(i,h){return this.clear(h).add(i,h)},has:function(h){return !!(this.getByCid(h.cid)||this.get(h.id))}})}});_.each(["attach","detach","open","close"],function(g){f.controller.Workflow.prototype[g]=function(){this.modal[g].apply(this.modal,arguments);return this}});f.view.Modal=Backbone.View.extend({tagName:"div",template:f.template("media-modal"),events:{"click .media-modal-backdrop, .media-modal-close":"closeHandler"},initialize:function(){this.controller=this.options.controller;this.controller.on("change:title",this.render,this);_.defaults(this.options,{container:document.body})},render:function(){this.options.$content=this.options.$content||e("
");this.options.$content.detach();this.$el.html(this.template(this.controller.toJSON()));this.$(".media-modal-content").append(this.options.$content);return this},attach:function(){this.$el.appendTo(this.options.container)},detach:function(){this.$el.detach()},open:function(){this.$el.show()},close:function(){this.$el.hide()},closeHandler:function(g){g.preventDefault();this.close()},content:function(g){if(this.options.$content){this.options.$content.detach()}this.options.$content=(g instanceof Backbone.View)?g.$el:g;return this.render()}});f.view.Toolbar=Backbone.View.extend({tagName:"div",className:"media-toolbar",initialize:function(){this._views={};this.$primary=e('').prependTo(this.$el);this.$secondary=e('').prependTo(this.$el);if(this.options.items){_.each(this.options.items,function(g,h){this.add(h,g,{silent:true})},this);this.render()}},render:function(){var g=_.chain(this._views).sortBy(function(h){return h.options.priority||10}).groupBy(function(h){return(h.options.priority||10)>0?"primary":"secondary"}).value();e(_.pluck(this._views,"el")).detach();this.$primary.html(_.pluck(g.primary,"el"));this.$secondary.html(_.pluck(g.secondary,"el"));return this},add:function(i,g,h){if(!(g instanceof Backbone.View)){g.classes=[i].concat(g.classes||[]);g=new f.view.Button(g).render()}this._views[i]=g;if(!h||!h.silent){this.render()}return this},get:function(g){return this._views[g]},remove:function(h,g){delete this._views[h];if(!g||!g.silent){this.render()}return this}});f.view.Button=Backbone.View.extend({tagName:"a",className:"media-button",attributes:{href:"#"},events:{click:"click"},defaults:{text:"",style:"",size:"large"},initialize:function(){this.model=new Backbone.Model(this.defaults);_.each(this.defaults,function(i,g){var h=this.options[g];if(_.isUndefined(h)){return}this.model.set(g,h);delete this.options[g]},this);this.model.on("change",this.render,this)},render:function(){var g=["button",this.className];if(this.model.get("style")){g.push("button-"+this.model.get("style"))}if(this.model.get("size")){g.push("button-"+this.model.get("size"))}g=g.concat(this.options.classes);this.el.className=g.join(" ");this.$el.text(this.model.get("text"));return this},click:function(g){g.preventDefault();if(this.options.click){this.options.click.apply(this,arguments)}}});f.view.Attachment=Backbone.View.extend({tagName:"li",className:"attachment",template:f.template("attachment"),events:{click:"toggleSelection"},buttons:{},initialize:function(){this.controller=this.options.controller;this.model.on("change:sizes change:uploading",this.render,this);this.model.on("change:percent",this.progress,this);this.model.on("add",this.select,this);this.model.on("remove",this.deselect,this);this.$el.on("click","a",this.preventDefault)},render:function(){var h=this.model.toJSON(),g={thumbnail:"image"===h.type?h.url:h.icon,uploading:h.uploading,orientation:h.orientation||"landscape",type:h.type,subtype:h.subtype,buttons:this.buttons};if(h.sizes&&h.sizes.medium){g.orientation=h.sizes.medium.orientation;g.thumbnail=h.sizes.medium.url}this.$el.html(this.template(g));if(h.uploading){this.$bar=this.$(".media-progress-bar div")}else{delete this.$bar}if(this.controller.selection.has(this.model)){this.select()}return this},progress:function(){if(this.$bar&&this.$bar.length){this.$bar.width(this.model.get("percent")+"%")}},toggleSelection:function(h){var g=this.controller.selection;g[g.has(this.model)?"remove":"add"](this.model)},select:function(g,h){if(h&&h!==this.controller.selection){return}this.$el.addClass("selected")},deselect:function(g,h){if(h&&h!==this.controller.selection){return}this.$el.removeClass("selected")},preventDefault:function(g){g.preventDefault()}});f.view.Attachment.Library=f.view.Attachment.extend({className:"attachment library",buttons:{insert:true},events:_.defaults({"click .insert":"insert"},f.view.Attachment.prototype.events),insert:function(){this.controller.selection.reset([this.model]);this.controller.update()}});f.view.Attachment.Gallery=f.view.Attachment.extend({buttons:{close:true},events:{"click .close":"toggleSelection"}});f.view.Workspace=Backbone.View.extend({tagName:"div",className:"media-workspace",template:f.template("media-workspace"),AttachmentView:f.view.Attachment,events:{dragenter:"maybeInitUploader",mouseenter:"maybeInitUploader"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{selectOne:false,uploader:{}});this.$content=e('');this.attachmentsView=new f.view.Attachments({controller:this.controller,directions:this.controller.get("multiple")?b.selectMediaMultiple:b.selectMediaSingular,collection:this.collection,AttachmentView:this.AttachmentView});this.$content.append(this.attachmentsView.$el);wp.Uploader.queue.on("add remove reset change:percent",this.renderUploadProgress,this);wp.Uploader.queue.on("add",this.selectUpload,this)},render:function(){this.$content.detach();this.attachmentsView.render();this.renderUploadProgress();this.$el.html(this.template(this.options)).append(this.$content);this.$bar=this.$(".upload-attachments .media-progress-bar div");return this},maybeInitUploader:function(){var g=this;if(this.uploader||!this.$el.closest("body").length){return}this.uploader=new wp.Uploader(_.extend({container:this.$el,dropzone:this.$el,browser:this.$(".upload-attachments a")},this.options.uploader))},selectUpload:function(g){this.controller.selection.add(g)},renderUploadProgress:function(){var g=wp.Uploader.queue;this.$el.toggleClass("uploading",!!g.length);if(!this.$bar||!g.length){return}this.$bar.width((g.reduce(function(h,i){if(i.get("uploading")){return h+(i.get("percent")||0)}else{return h+100}},0)/g.length)+"%")}});f.view.Workspace.Library=f.view.Workspace.extend({AttachmentView:f.view.Attachment.Library,initialize:function(){f.view.Workspace.prototype.initialize.apply(this,arguments);if(this.controller.get("multiple")){this.initToolbarView()}},initToolbarView:function(){var g=this.controller;this.toolbarView=new f.view.Toolbar({items:{"selection-preview":new f.view.SelectionPreview({controller:this.controller,collection:this.controller.selection,priority:-40}),"create-new-gallery":{style:"primary",text:b.createNewGallery,priority:40,click:function(){g.render("gallery")}},"insert-into-post":{text:b.insertIntoPost,priority:30,click:_.bind(g.update,g)},"add-to-gallery":{text:b.addToGallery,priority:20}}});this.controller.selection.on("add remove",function(){var i=this.controller.selection.length,h;this.$el.toggleClass("with-toolbar",!!i);h=i>1&&this.controller.selection.all(function(j){return"image"===j.get("type")});this.toolbarView.get("create-new-gallery").$el.toggle(h);insert=this.toolbarView.get("insert-into-post");insert.model.set("style",h?"":"primary")},this);this.$content.append(this.toolbarView.$el)}});f.view.Workspace.Gallery=f.view.Workspace.extend({AttachmentView:f.view.Attachment.Gallery,initialize:function(){f.view.Workspace.prototype.initialize.apply(this,arguments);this.initToolbarView()},initToolbarView:function(){var g=this.controller;this.toolbarView=new f.view.Toolbar({items:{"return-to-library":{text:b.returnToLibrary,priority:-40,click:function(){g.render("library")}},"insert-gallery-into-post":{style:"primary",text:b.insertGalleryIntoPost,priority:40,click:_.bind(g.update,g)},"add-images-from-library":{text:b.addImagesFromLibrary,priority:30}}});this.$el.addClass("with-toolbar");this.$content.append(this.toolbarView.$el)}});f.view.Attachments=Backbone.View.extend({tagName:"div",className:"attachments",template:f.template("attachments"),events:{"keyup input":"search"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{refreshSensitivity:200,refreshThreshold:3,AttachmentView:f.view.Attachment});_.each(["add","remove"],function(g){this.collection.on(g,function(j,h,i){this[g](j,i.index)},this)},this);this.collection.on("reset",this.refresh,this);this.$list=e("