1 line
5.9 KiB
JavaScript
1 line
5.9 KiB
JavaScript
if(typeof wp==="undefined"){var wp={}}(function(e){var c,b,a,d;media=wp.media=function(f){if(media.controller.Workflow){return new media.controller.Workflow(f).attach().render()}};_.extend(media,{model:{},view:{},controller:{}});d=function(g,f,h,i){if(_.isEqual(g,f)){return h===i?0:(h>i?-1:1)}else{return g>f?-1:1}};_.extend(media,{template:_.memoize(function(g){var f;return function(h){f=f||_.template(e("#tmpl-"+g).html());return f(h)}}),post:function(g,f){return media.ajax({data:_.isObject(g)?g:_.extend(f||{},{action:g})})},ajax:function(g,f){if(_.isObject(g)){f=g}else{f=f||{};f.data=_.extend(f.data||{},{action:g})}f=_.defaults(f||{},{type:"POST",url:ajaxurl,context:this});return e.Deferred(function(h){if(f.success){h.done(f.success)}if(f.error){h.fail(f.error)}delete f.success;delete f.error;e.ajax(f).done(function(i){if(_.isObject(i)&&!_.isUndefined(i.success)){h[i.success?"resolveWith":"rejectWith"](this,[i.data])}else{h.rejectWith(this,[i])}}).fail(function(){h.rejectWith(this,arguments)})}).promise()}});c=media.model.Attachment=Backbone.Model.extend({sync:function(h,g,f){if("read"===h){f=f||{};f.context=this;f.data=_.extend(f.data||{},{action:"get-attachment",id:this.id});return media.ajax(f)}else{return Backbone.sync.apply(this,arguments)}},parse:function(g,f){g.date=new Date(g.date);g.modified=new Date(g.modified);return g}},{create:function(f){return b.all.push(f)},get:_.memoize(function(g,f){return b.all.push(f||{id:g})})});b=media.model.Attachments=Backbone.Collection.extend({model:c,initialize:function(g,f){f=f||{};this.props=new Backbone.Model();this.filters=f.filters||{};this.props.on("change:order",this._changeOrder,this);this.props.on("change:orderby",this._changeOrderby,this);this.props.on("change:query",this._changeQuery,this);this.props.on("change:search",this._changeSearch,this);this.props.set(_.defaults(f.props||{},{order:"DESC"}));if(f.observe){this.observe(f.observe)}},_changeOrder:function(g,f){if(this.comparator){this.sort()}},_changeOrderby:function(f,g){if(this.comparator&&this.comparator!==b.comparator){return}if(g){this.comparator=b.comparator}else{delete this.comparator}},_changeQuery:function(f,g){if(g){this.props.on("change",this._requery,this);this._requery()}else{this.props.off("change",this._requery,this)}},_changeSearch:function(f,g){if(this.props.get("search")===g){return}if(g&&!this.filters.search){this.filters.search=b.filters.search}else{if(!g&&this.filters.search===b.filters.search){delete this.filters.search}}if(!this.props.get("source")){this.props.set("source",new b(this.models))}this.reset(this.props.get("source").filter(this.validator))},validator:function(f){return _.all(this.filters,function(h,g){return !!h.call(this,f)},this)},validate:function(g,f){return this[this.validator(g)?"add":"remove"](g,f)},observe:function(f){f.on("add change",this.validate,this)},unobserve:function(f){f.off("add change",this.validate,this)},mirror:function(f){if(this.mirroring&&this.mirroring===f){return}this.unmirror();this.mirroring=f;this.reset(f.models);f.on("add",this._mirrorAdd,this);f.on("remove",this._mirrorRemove,this);f.on("reset",this._mirrorReset,this)},unmirror:function(){if(!this.mirroring){return}this.mirroring.off("add",this._mirrorAdd,this);this.mirroring.off("remove",this._mirrorRemove,this);this.mirroring.off("reset",this._mirrorReset,this);delete this.mirroring},_mirrorAdd:function(h,f,g){this.add(h,{at:g.index})},_mirrorRemove:function(f){this.remove(f)},_mirrorReset:function(f){this.reset(f.models)},more:function(f){if(this.mirroring&&this.mirroring.more){return this.mirroring.more(f)}},parse:function(g,f){return _.map(g,function(h){var i=c.get(h.id);return i.set(i.parse(h,f))})},_requery:function(){if(this.props.get("query")){this.mirror(a.get(this.props.toJSON()))}}},{comparator:function(h,g){var i=this.props.get("orderby"),f=this.props.get("order"),j=h.cid,k=g.cid;h=h.get(i);g=g.get(i);if("date"===i||"modified"===i){h=h||new Date();g=g||new Date()}return("DESC"===f)?d(h,g,j,k):d(g,h,k,j)},filters:{search:function(f){if(!this.searching){return true}return _.any(["title","filename","description","caption","name"],function(g){var h=f.get(g);return h&&-1!==h.search(this.searching)},this)}}});b.all=new b();media.query=function(f){return new b(null,{props:_.extend(_.defaults(f||{},{orderby:"date"}),{query:true})})};a=media.model.Query=b.extend({initialize:function(g,f){f=f||{};b.prototype.initialize.apply(this,arguments);this.args=f.args;this.hasMore=true;this.created=new Date();this.filters.order=function(h){if(!this.comparator){return true}if(this.length){return 1!==this.comparator(h,this.last())}else{if("DESC"===this.args.order&&("date"===this.args.orderby||"modified"===this.args.orderby)){return h.get(this.args.orderby)>=this.created}}return false};this.observe(b.all)},more:function(f){var g=this;if(!this.hasMore){return}f=f||{};f.add=true;return this.fetch(f).done(function(h){if(_.isEmpty(h)||h.length<this.args.posts_per_page){g.hasMore=false}})},sync:function(i,g,f){var h;if("read"===i){f=f||{};f.context=this;f.data=_.extend(f.data||{},{action:"query-attachments"});args=_.clone(this.args);args.paged=Math.floor(this.length/args.posts_per_page)+1;f.data.query=args;return media.ajax(f)}else{h=b.prototype.sync?b.prototype:Backbone;return h.sync.apply(this,arguments)}}},{defaultProps:{orderby:"date",order:"DESC"},defaultArgs:{posts_per_page:40},orderby:{allowed:["name","author","date","title","modified","uploadedTo","id"],valuemap:{id:"ID",uploadedTo:"parent"}},propmap:{search:"s"},get:(function(){var f=[];return function(i,h){var g={},k=a.orderby,l=a.defaultProps,j;delete i.query;_.defaults(i,l);i.order=i.order.toUpperCase();if("DESC"!==i.order&&"ASC"!==i.order){i.order=l.order.toUpperCase()}if(!_.contains(k.allowed,i.orderby)){i.orderby=l.orderby}_.each(i,function(m,n){g[a.propmap[n]||n]=m});_.defaults(g,a.defaultArgs);g.orderby=k.valuemap[i.orderby]||i.orderby;j=_.find(f,function(m){return _.isEqual(m.args,g)});if(!j){j=new a([],_.extend(h||{},{props:i,args:g}));f.push(j)}return j}}())})}(jQuery)); |