(function(d){var e=wp.media,c=e.model.Attachment,b=e.model.Attachments,a=e.model.Query;e.controller.Workflow=Backbone.Model.extend({defaults:{multiple:false,view:"library",library:{},selection:[]},initialize:function(){this.createSelection();this._views={};this._pendingViews={};this.modal=new e.view.Modal({controller:this});this.add("library",e.view.Workspace.Library,{collection:e.query(this.get("library"))});this.unset("library");this.add("gallery",e.view.Workspace.Gallery,{collection:this.selection})},add:function(h,g,f){this.remove(h);this._pendingViews[h]={view:g,options:f};this.trigger("add add:"+h,g,f);return this},view:function(g){var f;g=g||this.get("view");f=this._pendingViews[g];if(!this._views[g]&&f){this._views[g]=new f.view(_.extend({controller:this},f.options||{}));delete this._pendingViews[g];this.trigger("init init:"+g,this._views[g])}return this._views[g]},remove:function(f){delete this._views[f];delete this._pendingViews[f];this.trigger("remove remove:"+f);return this},render:function(i,h,g){var f;i=i||this.get("view");if(h){this.add(i,h,g)}f=this.view(i);if(!f){return this}f.render();this.modal.content(f);return this},createSelection:function(){var f=this;this.selection=new b(this.get("selection"));this.unset("selection");_.extend(this.selection,{add:function(h,g){if(!f.get("multiple")){h=_.isArray(h)?_.first(h):h;this.clear(g)}return b.prototype.add.call(this,h,g)},clear:function(g){return this.remove(this.models,g)},reset:function(h,g){return this.clear(g).add(h,g)},has:function(g){return !!(this.getByCid(g.cid)||this.get(g.id))}})}});_.each(["attach","detach","open","close"],function(f){e.controller.Workflow.prototype[f]=function(){this.modal[f].apply(this.modal,arguments);return this}});e.view.Modal=Backbone.View.extend({tagName:"div",template:e.template("media-modal"),events:{"click .media-modal-backdrop, .media-modal-close":"closeHandler"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{title:"",container:document.body})},render:function(){this.options.$content=this.options.$content||d("
");this.options.$content.detach();this.$el.html(this.template(this.options));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(f){f.preventDefault();this.close()},content:function(f){if(this.options.$content){this.options.$content.detach()}this.options.$content=(f instanceof Backbone.View)?f.$el:f;return this.render()},title:function(f){this.options.title=f;return this.render()}});e.view.Toolbar=Backbone.View.extend({tagName:"div",className:"media-toolbar",initialize:function(){this._views={};this.$primary=d('
').prependTo(this.$el);this.$secondary=d('
').prependTo(this.$el);if(this.options.items){_.each(this.options.items,function(f,g){this.add(g,f,{silent:true})},this);this.render()}},render:function(){var f=_.chain(this._views).sortBy(function(g){return g.options.priority||10}).groupBy(function(g){return(g.options.priority||10)>0?"primary":"secondary"}).value();d(_.pluck(this._views,"el")).detach();this.$primary.html(_.pluck(f.primary,"el"));this.$secondary.html(_.pluck(f.secondary,"el"));return this},add:function(h,f,g){if(!(f instanceof Backbone.View)){f.classes=[h].concat(f.classes||[]);f=new e.view.Button(f).render()}this._views[h]=f;if(!g||!g.silent){this.render()}return this},remove:function(g,f){delete this._views[g];if(!f||!f.silent){this.render()}return this}});e.view.Button=Backbone.View.extend({tagName:"a",className:"media-button",attributes:{href:"#"},events:{click:"click"},initialize:function(){_.defaults(this.options,{text:"",classes:[]})},render:function(){var f=["button",this.className];if(this.options.style){f.push("button-"+this.options.style)}if(this.options.size){f.push("button-"+this.options.size)}f=f.concat(this.options.classes);this.el.className=f.join(" ");this.$el.text(this.options.text);return this},click:function(f){f.preventDefault();if(this.options.click){this.options.click.apply(this,arguments)}}});e.view.Workspace=Backbone.View.extend({tagName:"div",className:"media-workspace",template:e.template("media-workspace"),events:{dragenter:"maybeInitUploader",mouseenter:"maybeInitUploader"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{selectOne:false,uploader:{}});this.$content=d('
');this.attachmentsView=new e.view.Attachments({controller:this.controller,directions:"Select stuff.",collection:this.collection});this.$content.append(this.attachmentsView.$el);wp.Uploader.queue.on("add remove reset change:percent",this.renderUploadProgress,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 f=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))},renderUploadProgress:function(){var f=wp.Uploader.queue;this.$el.toggleClass("uploading",!!f.length);if(!this.$bar||!f.length){return}this.$bar.width((f.reduce(function(g,h){if(h.get("uploading")){return g+(h.get("percent")||0)}else{return g+100}},0)/f.length)+"%")}});e.view.Workspace.Library=e.view.Workspace.extend({initialize:function(){e.view.Workspace.prototype.initialize.apply(this,arguments);if(this.controller.get("multiple")){this.initToolbarView()}},initToolbarView:function(){var f=this.controller;this.toolbarView=new e.view.Toolbar({items:{"selection-preview":new e.view.SelectionPreview({controller:this.controller,collection:this.controller.selection,priority:-40}),"insert-into-post":{style:"primary",text:"Insert into post",priority:40},"create-new-gallery":{style:"primary",text:"Create a new gallery",priority:30,click:function(){f.render("gallery")}},"add-to-gallery":{text:"Add to gallery",priority:20}}});this.controller.selection.on("add remove",function(){this.$el.toggleClass("with-toolbar",!!this.controller.selection.length)},this);this.$content.append(this.toolbarView.$el)}});e.view.Workspace.Gallery=e.view.Workspace.extend({initialize:function(){e.view.Workspace.prototype.initialize.apply(this,arguments);this.initToolbarView()},initToolbarView:function(){var f=this.controller;this.toolbarView=new e.view.Toolbar({items:{"return-to-library":{text:"Return to media library",priority:-40,click:function(){f.render("library")}},"insert-gallery-into-post":{style:"primary",text:"Insert gallery into post",priority:40,click:function(){f.close()}},"add-images":{text:"Add images from media library",priority:30}}});this.$el.addClass("with-toolbar");this.$content.append(this.toolbarView.$el)}});e.view.Attachments=Backbone.View.extend({tagName:"div",className:"attachments",template:e.template("attachments"),events:{"keyup input":"search"},initialize:function(){this.controller=this.options.controller;_.defaults(this.options,{refreshSensitivity:200,refreshThreshold:3});_.each(["add","remove"],function(f){this.collection.on(f,function(i,g,h){this[f](i,h.index)},this)},this);this.collection.on("reset",this.refresh,this);this.$list=d("
    ");this.list=this.$list[0];this.scroll=_.chain(this.scroll).bind(this).throttle(this.options.refreshSensitivity).value();this.$list.on("scroll.attachments",this.scroll)},render:function(){this.$list.detach();this.$el.html(this.template(this.options)).append(this.$list);this.refresh();return this},refresh:function(){if(!this.collection.length){this.collection.more();this.$list.empty();return this}this.$list.html(this.collection.map(function(f){return new e.view.Attachment({controller:this.controller,model:f}).render().$el},this));this.scroll();return this},add:function(i,g){var f,h;f=new e.view.Attachment({controller:this.controller,model:i}).render();h=this.$list.children();if(h.length>g){h.eq(g).before(f.$el)}else{this.$list.append(f.$el)}},remove:function(h,f){var g=this.$list.children();if(g.length){g.eq(f).detach()}},scroll:function(f){if(!this.$list.is(":visible")){return}if(this.list.scrollHeight