Wordpress/wp-includes/js/media-models.min.js
2013-03-03 22:58:36 +00:00

1 line
11 KiB
JavaScript

window.wp=window.wp||{};(function(g){var e,d,b,f,c,h,a;h=wp.media=function(i){var j=h.view.MediaFrame,k;if(!j){return}i=_.defaults(i||{},{frame:"select"});if("select"===i.frame&&j.Select){k=new j.Select(i)}else{if("post"===i.frame&&j.Post){k=new j.Post(i)}}delete i.frame;return k};_.extend(h,{model:{},view:{},controller:{},frames:{}});c=h.model.l10n=typeof _wpMediaModelsL10n==="undefined"?{}:_wpMediaModelsL10n;h.model.settings=c.settings||{};delete c.settings;f=function(j,i,k,l){if(_.isEqual(j,i)){return k===l?0:(k>l?-1:1)}else{return j>i?-1:1}};a=function(k,j){var l=j.success,i=j.error;j.success=function(m){if(l){l(k,m,j)}k.trigger("sync",k,m,j)};j.error=function(m){if(i){i(k,m,j)}k.trigger("error",k,m,j)};return j};_.extend(h,{template:_.memoize(function(k){var j,i={evaluate:/<#([\s\S]+?)#>/g,interpolate:/\{\{\{([\s\S]+?)\}\}\}/g,escape:/\{\{([^\}]+?)\}\}(?!\})/g,variable:"data"};return function(l){j=j||_.template(g("#tmpl-"+k).html(),null,i);return j(l)}}),post:function(j,i){return h.ajax({data:_.isObject(j)?j:_.extend(i||{},{action:j})})},ajax:function(j,i){if(_.isObject(j)){i=j}else{i=i||{};i.data=_.extend(i.data||{},{action:j})}i=_.defaults(i||{},{type:"POST",url:h.model.settings.ajaxurl,context:this});return g.Deferred(function(k){if(i.success){k.done(i.success)}if(i.error){k.fail(i.error)}delete i.success;delete i.error;g.ajax(i).done(function(l){if(l==="1"||l===1){l={success:true}}if(_.isObject(l)&&!_.isUndefined(l.success)){k[l.success?"resolveWith":"rejectWith"](this,[l.data])}else{k.rejectWith(this,[l])}}).fail(function(){k.rejectWith(this,arguments)})}).promise()},fit:function(m){var j=m.width,i=m.height,l=m.maxWidth,k=m.maxHeight,n;if(!_.isUndefined(l)&&!_.isUndefined(k)){n=(j/i>l/k)?"width":"height"}else{if(_.isUndefined(k)){n="width"}else{if(_.isUndefined(l)&&i>k){n="height"}}}if("width"===n&&j>l){return{width:l,height:Math.round(l*i/j)}}else{if("height"===n&&i>k){return{width:Math.round(k*j/i),height:k}}else{return{width:j,height:i}}}},truncate:function(i,k,j){k=k||30;j=j||"&hellip;";if(i.length<=k){return i}return i.substr(0,k/2)+j+i.substr(-1*k/2)}});h.attachment=function(i){return e.get(i)};e=h.model.Attachment=Backbone.Model.extend({sync:function(k,j,i){if(_.isUndefined(this.id)){return g.Deferred().rejectWith(this).promise()}if("read"===k){i=i||{};i.context=this;i.data=_.extend(i.data||{},{action:"get-attachment",id:this.id});a(j,i);return h.ajax(i)}else{if("update"===k){if(!this.get("nonces")||!this.get("nonces").update){return g.Deferred().rejectWith(this).promise()}i=i||{};i.context=this;i.data=_.extend(i.data||{},{action:"save-attachment",id:this.id,nonce:this.get("nonces").update,post_id:h.model.settings.post.id});if(j.hasChanged()){i.data.changes={};_.each(j.changed,function(m,l){i.data.changes[l]=this.get(l)},this)}a(j,i);return h.ajax(i)}else{if("delete"===k){i=i||{};if(!i.wait){this.destroyed=true}i.context=this;i.data=_.extend(i.data||{},{action:"delete-post",id:this.id,_wpnonce:this.get("nonces")["delete"]});a(j,i);return h.ajax(i).done(function(){this.destroyed=true}).fail(function(){this.destroyed=false})}else{return Backbone.Model.prototype.sync.apply(this,arguments)}}}},parse:function(j,i){if(!j){return j}j.date=new Date(j.date);j.modified=new Date(j.modified);return j},saveCompat:function(k,j){var i=this;if(!this.get("nonces")||!this.get("nonces").update){return g.Deferred().rejectWith(this).promise()}return h.post("save-attachment-compat",_.defaults({id:this.id,nonce:this.get("nonces").update,post_id:h.model.settings.post.id},k)).done(function(n,l,m){i.set(i.parse(n,m),j)})}},{create:function(i){return d.all.push(i)},get:_.memoize(function(j,i){return d.all.push(i||{id:j})})});d=h.model.Attachments=Backbone.Collection.extend({model:e,initialize:function(j,i){i=i||{};this.props=new Backbone.Model();this.filters=i.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(i.props||{}));if(i.observe){this.observe(i.observe)}},_changeOrder:function(j,i){if(this.comparator){this.sort()}},_changeOrderby:function(i,j){if(this.comparator&&this.comparator!==d.comparator){return}if(j&&"post__in"!==j){this.comparator=d.comparator}else{delete this.comparator}},_changeQuery:function(i,j){if(j){this.props.on("change",this._requery,this);this._requery()}else{this.props.off("change",this._requery,this)}},_changeFilteredProps:function(j,i){if(this.props.get("query")){return}var k=_.chain(j.changed).map(function(m,o){var n=d.filters[o],l=j.get(o);if(!n){return}if(l&&!this.filters[o]){this.filters[o]=n}else{if(!l&&this.filters[o]===n){delete this.filters[o]}else{return}}return true},this).any().value();if(!k){return}if(!this._source){this._source=new d(this.models)}this.reset(this._source.filter(this.validator,this))},validateDestroyed:false,validator:function(i){if(!this.validateDestroyed&&i.destroyed){return false}return _.all(this.filters,function(k,j){return !!k.call(this,i)},this)},validate:function(l,j){var k=this.validator(l),i=!!this.get(l.cid);if(!k&&i){this.remove(l,j)}else{if(k&&!i){this.add(l,j)}}return this},validateAll:function(i,j){j=j||{};_.each(i.models,function(k){this.validate(k,{silent:true})},this);if(!j.silent){this.trigger("reset",this,j)}return this},observe:function(i){this.observers=this.observers||[];this.observers.push(i);i.on("add change remove",this._validateHandler,this);i.on("reset",this._validateAllHandler,this);this.validateAll(i);return this},unobserve:function(i){if(i){i.off(null,null,this);this.observers=_.without(this.observers,i)}else{_.each(this.observers,function(j){j.off(null,null,this)},this);delete this.observers}return this},_validateHandler:function(k,i,j){j=i===this.mirroring?j:{silent:j&&j.silent};return this.validate(k,j)},_validateAllHandler:function(i,j){return this.validateAll(i,j)},mirror:function(i){if(this.mirroring&&this.mirroring===i){return this}this.unmirror();this.mirroring=i;this.reset([],{silent:true});this.observe(i);return this},unmirror:function(){if(!this.mirroring){return}this.unobserve(this.mirroring);delete this.mirroring},more:function(k){var j=g.Deferred(),l=this.mirroring,i=this;if(!l||!l.more){return j.resolveWith(this).promise()}l.more(k).done(function(){if(this===i.mirroring){j.resolveWith(this)}});return j.promise()},hasMore:function(){return this.mirroring?this.mirroring.hasMore():false},parse:function(j,i){return _.map(j,function(k){var l=e.get(k.id);return l.set(l.parse(k,i))})},_requery:function(){if(this.props.get("query")){this.mirror(b.get(this.props.toJSON()))}},saveMenuOrder:function(){if("menuOrder"!==this.props.get("orderby")){return}var i=this.chain().filter(function(j){return !_.isUndefined(j.id)}).map(function(k,j){j=j+1;k.set("menuOrder",j);return[k.id,j]}).object().value();if(_.isEmpty(i)){return}return h.post("save-attachment-order",{nonce:h.model.settings.post.nonce,post_id:h.model.settings.post.id,attachments:i})}},{comparator:function(k,j,l){var m=this.props.get("orderby"),i=this.props.get("order")||"DESC",n=k.cid,o=j.cid;k=k.get(m);j=j.get(m);if("date"===m||"modified"===m){k=k||new Date();j=j||new Date()}if(l&&l.ties){n=o=null}return("DESC"===i)?f(k,j,n,o):f(j,k,o,n)},filters:{search:function(i){if(!this.props.get("search")){return true}return _.any(["title","filename","description","caption","name"],function(j){var k=i.get(j);return k&&-1!==k.search(this.props.get("search"))},this)},type:function(j){var i=this.props.get("type");return !i||-1!==i.indexOf(j.get("type"))},uploadedTo:function(j){var i=this.props.get("uploadedTo");if(_.isUndefined(i)){return true}return i===j.get("uploadedTo")}}});d.all=new d();h.query=function(i){return new d(null,{props:_.extend(_.defaults(i||{},{orderby:"date"}),{query:true})})};b=h.model.Query=d.extend({initialize:function(k,i){var j;i=i||{};d.prototype.initialize.apply(this,arguments);this.args=i.args;this._hasMore=true;this.created=new Date();this.filters.order=function(n){var m=this.props.get("orderby"),l=this.props.get("order");if(!this.comparator){return true}if(this.length){return 1!==this.comparator(n,this.last(),{ties:true})}else{if("DESC"===l&&("date"===m||"modified"===m)){return n.get(m)>=this.created}else{if("ASC"===l&&"menuOrder"===m){return n.get(m)===0}}}return false};j=["s","order","orderby","posts_per_page","post_mime_type","post_parent"];if(wp.Uploader&&_(this.args).chain().keys().difference(j).isEmpty().value()){this.observe(wp.Uploader.queue)}},hasMore:function(){return this._hasMore},more:function(i){var j=this;if(this._more&&"pending"===this._more.state()){return this._more}if(!this.hasMore()){return g.Deferred().resolveWith(this).promise()}i=i||{};i.add=true;return this._more=this.fetch(i).done(function(k){if(_.isEmpty(k)||-1===this.args.posts_per_page||k.length<this.args.posts_per_page){j._hasMore=false}})},sync:function(l,j,i){var k;if("read"===l){i=i||{};i.context=this;i.data=_.extend(i.data||{},{action:"query-attachments",post_id:h.model.settings.post.id});args=_.clone(this.args);if(-1!==args.posts_per_page){args.paged=Math.floor(this.length/args.posts_per_page)+1}i.data.query=args;a(j,i);return h.ajax(i)}else{k=d.prototype.sync?d.prototype:Backbone;return k.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 i=[];return function(l,k){var j={},n=b.orderby,o=b.defaultProps,m;delete l.query;_.defaults(l,o);l.order=l.order.toUpperCase();if("DESC"!==l.order&&"ASC"!==l.order){l.order=o.order.toUpperCase()}if(!_.contains(n.allowed,l.orderby)){l.orderby=o.orderby}_.each(l,function(p,q){if(_.isNull(p)){return}j[b.propmap[q]||q]=p});_.defaults(j,b.defaultArgs);j.orderby=n.valuemap[l.orderby]||l.orderby;m=_.find(i,function(p){return _.isEqual(p.args,j)});if(!m){m=new b([],_.extend(k||{},{props:l,args:j}));i.push(m)}return m}}())});h.model.Selection=d.extend({initialize:function(j,i){d.prototype.initialize.apply(this,arguments);this.multiple=i&&i.multiple;this.on("add remove reset",_.bind(this.single,this,false))},add:function(j,i){if(!this.multiple){this.remove(this.models)}return d.prototype.add.call(this,j,i)},single:function(i){var j=this._single;if(i){this._single=i}if(this._single&&!this.get(this._single.cid)){delete this._single}this._single=this._single||this.last();if(this._single!==j){if(j){j.trigger("selection:unsingle",j,this);if(!this.get(j.cid)){this.trigger("selection:unsingle",j,this)}}if(this._single){this._single.trigger("selection:single",this._single,this)}}return this._single}});g(window).on("unload",function(){window.wp=null})}(jQuery));