WordPress/wp-includes/js/media-views.min.js

1 line
6.1 KiB
JavaScript

(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},initialize:function(){this.createSelection();this.modal=new e.view.Modal({controller:this});this.workspace=new e.view.Workspace({controller:this})},createSelection:function(){var f=this;this.selection=new b();_.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))}})},render:function(){this.workspace.render();this.modal.content(this.workspace).attach();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.$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){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.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.attachmentsView=new e.view.Attachments({controller:this.controller,directions:"Select stuff.",collection:new b(null,{mirror:e.query()})});this.$content=d('<div class="existing-attachments" />');this.$content.append(this.attachmentsView.$el);this.pending=new b([],{query:false});this.pending.on("add remove reset change:percent",function(){this.$el.toggleClass("uploading",!!this.pending.length);if(!this.$bar||!this.pending.length){return}this.$bar.width((this.pending.reduce(function(f,g){if(g.get("uploading")){return f+(g.get("percent")||0)}else{return f+100}},0)/this.pending.length)+"%")},this)},render:function(){this.attachmentsView.render();this.$el.html(this.template(this.options)).append(this.$content);this.$bar=this.$(".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"),added:function(g){g.attachment=c.create(_.extend({file:g,uploading:true,date:new Date()},_.pick(g,"loaded","size","percent")));f.pending.add(g.attachment)},progress:function(g){g.attachment.set(_.pick(g,"loaded","percent"))},success:function(i,h){var g;_.each(["file","loaded","size","uploading","percent"],function(j){h.attachment.unset(j)});h.attachment.set("id",i.id);c.get(i.id,h.attachment).fetch();g=f.pending.all(function(j){return !j.get("uploading")});if(g){f.pending.reset()}},error:function(i,g,h){h.attachment.destroy()}},this.options.uploader))}});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("<ul />");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.$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<this.list.scrollTop+(this.list.clientHeight*this.options.refreshThreshold)){this.collection.more()}},search:function(g){var f=_.clone(this.collection.mirroring.args);if(f.s===g.target.value){return}if(g.target.value){f.s=g.target.value}else{delete f.s}this.collection.mirror(e.query(f))}});e.view.Attachment=Backbone.View.extend({tagName:"li",className:"attachment",template:e.template("attachment"),events:{click:"toggleSelection"},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)},render:function(){var g=this.model.toJSON(),f={orientation:g.orientation||"landscape",thumbnail:g.url||"",uploading:g.uploading};if(g.sizes&&g.sizes.medium){f.orientation=g.sizes.medium.orientation;f.thumbnail=g.sizes.medium.url}this.$el.html(this.template(f));if(g.uploading){this.$bar=this.$(".media-progress-bar div")}else{delete this.$bar}return this},progress:function(){if(this.$bar&&this.$bar.length){this.$bar.width(this.model.get("percent")+"%")}},toggleSelection:function(g){var f=this.controller.selection;f[f.has(this.model)?"remove":"add"](this.model)},select:function(f,g){if(g===this.controller.selection){this.$el.addClass("selected")}},deselect:function(f,g){if(g===this.controller.selection){this.$el.removeClass("selected")}}})}(jQuery));