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

1 line
9.2 KiB
JavaScript

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