WordPress/wp-includes/js/media-models.min.js
2012-11-30 15:20:51 +00:00

1 line
9.7 KiB
JavaScript

window.wp=window.wp||{};(function(f){var d,c,a,e,b;media=wp.media=function(g){var h=media.view.MediaFrame,i;if(!h){return}g=_.defaults(g||{},{frame:"select"});if("select"===g.frame&&h.Select){i=new h.Select(g)}else{if("post"===g.frame&&h.Post){i=new h.Post(g)}}delete g.frame;i.setState(i.options.state);return i.render().attach().open()};_.extend(media,{model:{},view:{},controller:{}});b=media.model.l10n=typeof _wpMediaModelsL10n==="undefined"?{}:_wpMediaModelsL10n;media.model.settings=b.settings||{};delete b.settings;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(i){var h,g={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(j){h=h||_.template(f("#tmpl-"+i).html(),null,g);return h(j)}}),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:media.model.settings.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}}}},truncate:function(g,i,h){i=i||30;h=h||"&hellip;";if(g.length<=i){return g}return g.substr(0,i/2)+h+g.substr(-1*i/2)}});d=media.model.Attachment=Backbone.Model.extend({sync:function(i,h,g){if(_.isUndefined(this.id)){return f.Deferred().reject().promise()}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:this.get("nonces").update,post_id:media.model.settings.postId});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)}else{if("delete"===i){g=g||{};g.context=this;g.data=_.extend(g.data||{},{action:"delete-post",id:this.id,_wpnonce:this.get("nonces")["delete"]});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},saveCompat:function(i,h){var g=this;return media.post("save-attachment-compat",_.defaults({id:this.id,nonce:this.get("nonces").update,post_id:media.model.settings.postId},i)).done(function(l,j,k){g.set(g.parse(l,k),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",this._changeFilteredProps,this);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.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)}},_changeFilteredProps:function(h,g){if(this.props.get("query")){return}var i=_.chain(g.changes).map(function(k,m){var l=c.filters[m],j=h.get(m);if(!l){return}if(j&&!this.filters[m]){this.filters[m]=l}else{if(!j&&this.filters[m]===l){delete this.filters[m]}else{return}}return true},this).any().value();if(!i){return}if(!this._source){this._source=new c(this.models)}this.reset(this._source.filter(this.validator,this))},validator:function(g){return _.all(this.filters,function(i,h){return !!i.call(this,g)},this)},validate:function(j,h){var i=this.validator(j),g=!!this.getByCid(j.cid);if(!i&&g){this.remove(j,h)}else{if(i&&!g){this.add(j,h)}}return this},validateAll:function(g,h){h=h||{};_.each(g.models,function(i){this.validate(i,{silent:true})},this);if(!h.silent){this.trigger("reset",this,h)}return this},observe:function(g){this.observers=this.observers||[];this.observers.push(g);g.on("add change remove",this._validateHandler,this);g.on("reset",this._validateAllHandler,this);this.validateAll(g);return this},unobserve:function(g){if(g){g.off(null,null,this);this.observers=_.without(this.observers,g)}else{_.each(this.observers,function(h){h.off(null,null,this)},this);delete this.observers}return this},_validateHandler:function(i,g,h){h=g===this.mirroring?h:{silent:h&&h.silent};return this.validate(i,h)},_validateAllHandler:function(g,h){return this.validateAll(g,h)},mirror:function(g){if(this.mirroring&&this.mirroring===g){return this}this.unmirror();this.mirroring=g;this.reset([],{silent:true});this.observe(g);return this},unmirror:function(){if(!this.mirroring){return}this.unobserve(this.mirroring);delete this.mirroring},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,j){var k=this.props.get("orderby"),g=this.props.get("order")||"DESC",l=i.cid,m=h.cid;i=i.get(k);h=h.get(k);if("date"===k||"modified"===k){i=i||new Date();h=h||new Date()}if(j&&j.ties){l=m=null}return("DESC"===g)?e(i,h,l,m):e(h,i,m,l)},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");return !g||-1!==g.indexOf(h.get("type"))},uploadedTo:function(h){var g=this.props.get("uploadedTo");if(_.isUndefined(g)){return true}return g===h.get("uploadedTo")}}});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(l){var k=this.props.get("orderby"),j=this.props.get("order");if(!this.comparator){return true}if(this.length){return 1!==this.comparator(l,this.last(),{ties:true})}else{if("DESC"===j&&("date"===k||"modified"===k)){return l.get(k)>=this.created}else{if("ASC"===j&&"menuOrder"===k){return l.get(k)===0}}}return false};h=["s","order","orderby","posts_per_page","post_mime_type","post_parent"];if(wp.Uploader&&_(this.args).chain().keys().difference(h).isEmpty().value()){this.observe(wp.Uploader.queue)}},more:function(g){var h=this;if(this._more&&"pending"===this._more.state()){return this._more}if(!this.hasMore){return f.Deferred().resolve().promise()}g=g||{};g.add=true;return this._more=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",post_id:media.model.settings.postId});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","menuOrder"],valuemap:{id:"ID",uploadedTo:"parent",menuOrder:"menu_order ID"}},propmap:{search:"s",type:"post_mime_type",perPage:"posts_per_page",menuOrder:"menu_order",uploadedTo:"post_parent"},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){if(_.isNull(n)){return}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}}())});media.model.Selection=c.extend({initialize:function(h,g){c.prototype.initialize.apply(this,arguments);this.multiple=g&&g.multiple;this.on("add remove reset",_.bind(this.single,this,false))},add:function(h,g){if(!this.multiple){h=_.isArray(h)&&h.length?_.first(h):h;this.clear(g)}return c.prototype.add.call(this,h,g)},clear:function(g){this.remove(this.models,g).single();return this},reset:function(h,g){this.clear(g).add(h,g).single();return this},has:function(g){return !!(this.getByCid(g.cid)||this.get(g.id))},single:function(g){var h=this._single;if(g){this._single=g}if(this._single&&!this.has(this._single)){delete this._single}this._single=this._single||this.last();if(this._single!==h){if(h){h.trigger("selection:unsingle",h,this)}if(this._single){this._single.trigger("selection:single",this._single,this)}}return this._single}});f(window).on("unload",function(){window.wp=null})}(jQuery));