WordPress/wp-includes/js/media-models.min.js
2012-10-12 22:58:48 +00:00

1 line
7.2 KiB
JavaScript

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