62f79fd6e3
git-svn-id: https://develop.svn.wordpress.org/trunk@21690 602fd350-edb4-49c9-b593-d223f7449a82
1 line
4.5 KiB
JavaScript
1 line
4.5 KiB
JavaScript
if(typeof wp==="undefined"){var wp={}}(function(d){var e=wp.media={model:{},view:{},controller:{}},c,b,a;_.extend(e,{template:_.memoize(function(g){var f;return function(h){f=f||_.template(d("#tmpl-"+g).html());return f(h)}}),post:function(g,f){return e.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 d.Deferred(function(h){if(f.success){h.done(f.success)}if(f.error){h.fail(f.error)}delete f.success;delete f.error;d.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=e.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 e.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=e.model.Attachments=Backbone.Collection.extend({model:c,initialize:function(g,f){f=f||{};this.filters=f.filters||{};if(f.observe){this.observe(f.observe)}if(f.mirror){this.mirror(f.mirror)}},validator:function(f){return _.all(this.filters,function(g){return !!g.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))})}});b.all=new b();e.query=(function(){var f=[];return function(h,g){h=_.defaults(h||{},a.defaultArgs);var i=_.find(f,function(j){return _.isEqual(j.args,h)});if(!i){i=new a([],_.extend(g||{},{args:h}));f.push(i)}return i}}());a=e.model.Query=b.extend({initialize:function(i,f){var g,h=a.defaultArgs;f=f||{};b.prototype.initialize.apply(this,arguments);this.args=_.defaults(f.args||{},h);this.args.order=this.args.order.toUpperCase();if("DESC"!==this.args.order&&"ASC"!==this.args.order){this.args.order=h.order.toUpperCase()}g={allowed:["name","author","date","title","modified","parent","ID"],keymap:{ID:"id",parent:"uploadedTo"}};if(!_.contains(g.allowed,this.args.orderby)){this.args.orderby=h.orderby}this.orderkey=g.keymap[this.args.orderby]||this.args.orderby;this.hasMore=true;this.created=new Date();this.filters.order=function(j){if(this.length){return 1!==this.comparator(j,this.last())}else{if("DESC"===this.args.order&&("date"===this.orderkey||"modified"===this.orderkey)){return j.get(this.orderkey)>=this.created}}return false};if(this.args.s){this.filters.search=function(j){return _.any(["title","filename","description","caption","name"],function(k){var l=j.get(k);return l&&-1!==l.search(this.args.s)},this)}}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 e.ajax(f)}else{h=b.prototype.sync?b.prototype:Backbone;return h.sync.apply(this,arguments)}},comparator:(function(){var f=function(h,g,i,j){if(_.isEqual(h,g)){return i===j?0:(i>j?-1:1)}else{return h>g?-1:1}};return function(i,h){var j=this.orderkey,g=this.args.order,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)?f(i,h,k,l):f(h,i,l,k)}}())},{defaultArgs:{posts_per_page:40,orderby:"date",order:"DESC"}})}(jQuery)); |