2013-07-05 17:42:57 +02:00
|
|
|
window.wp=window.wp||{};(function(b){var a;a=wp.revisions={model:{},view:{},controller:{},router:{}};a.settings=typeof _wpRevisionsSettings==="undefined"?{}:_wpRevisionsSettings;a.model.Slider=Backbone.Model.extend({defaults:{value:0,min:0,max:1,step:1}});a.model.Revision=Backbone.Model.extend({});a.model.Revisions=Backbone.Collection.extend({model:a.model.Revision,comparator:function(c){return c.id}});a.model.Field=Backbone.Model.extend({});a.model.Fields=Backbone.Collection.extend({model:a.model.Field});a.model.Diff=Backbone.Model.extend({initialize:function(d,e){var c=this.get("fields");this.unset("fields");this.fields=new a.model.Fields(c)}});a.model.Diffs=Backbone.Collection.extend({initialize:function(d,c){this.revisions=c.revisions;this.requests={}},model:a.model.Diff,ensure:function(h,d){var g=this.get(h);var f=this.requests[h];var c=b.Deferred();var e={};if(g){c.resolveWith(d,[g])}else{this.trigger("ensure:load",e);_.each(e,_.bind(function(i){if(this.requests[i]){delete e[i]}},this));if(!f){e[h]=true;f=this.load(_.keys(e))}f.done(_.bind(function(){c.resolveWith(d,[this.get(h)])},this))}return c.promise()},loadNew:function(c){c=_.object(c,c);_.each(c,_.bind(function(d){if(this.get(d)){delete c[d]}},this));c=_.toArray(c);return this.load(c)},load:function(c){return this.fetch({data:{compare:c},remove:false})},loadLast:function(c){c=c||1;var d=this.getProximalDiffIds();d=_.last(d,c);if(d.length){return this.loadNew(d)}},loadLastUnloaded:function(c){c=c||1;var d=this.getUnloadedProximalDiffIds();d=_.last(d,c);if(d.length){return this.loadNew(d)}},getProximalDiffIds:function(){var d=0,c=[];this.revisions.each(_.bind(function(e){c.push(d+":"+e.id);d=e.id},this));return c},getUnloadedProximalDiffIds:function(){var c=this.getProximalDiffIds();c=_.object(c,c);_.each(c,_.bind(function(d){if(this.get(d)){delete c[d]}},this));return _.toArray(c)},loadAllBy:function(d){d=d||20;var c=this.getUnloadedProximalDiffIds();if(c.length){return this.loadLastUnloaded(d).always(_.bind(function(){this.loadAllBy(d)},this))}},sync:function(g,e,d){if("read"===g){d=d||{};d.context=this;d.data=_.extend(d.data||{},{action:"get-revision-diffs",post_id:a.settings.postId});var c=wp.xhr.send(d);var f=this.requests;if(d.data.compare){_.each(d.data.compare,function(h){f[h]=c})}c.always(function(){if(d.data.compare){_.each(d.data.compare,function(h){delete f[h]})}});return c}else{return Backbone.Model.prototype.sync.apply(this,arguments)}}});a.model.FrameState=Backbone.Model.extend({initialize:function(c,d){this.revisions=d.revisions;this.diffs=new a.model.Diffs([],{revisions:this.revisions});this.listenTo(this,"change:from",this.updateDiffFrom);this.listenTo(this,"change:to",this.updateDiffTo);this.revisionsRouter=new a.router.Router({model:this})},updateDiffTo:function(){var c=this.get("from");this.set("diffId",(c?c.id:"0")+":"+this.get("to").id)},updateDiffFrom:function(){if(this.get("compareTwoMode")){this.updateDiffTo()}}});a.view.Frame=wp.Backbone.View.extend({tagName:"div",className:"revisions",template:wp.template("revisions-frame"),initialize:function(){this.model=new a.model.FrameState({},{revisions:this.collection});this.listenTo(this.model,"change:diffId",this.updateDiff);this.views.set(".revisions-control-frame",new a.view.Controls({model:this.model}));if(this.model.revisions.length){var d=this.model.revisions.last(2);var c={to:d.pop()};if(d.length){c.from=d.pop()}this.model.set(c);this.model.diffs.loadLastUnloaded(10).always(_.bind(function(){this.model.diffs.loadAllBy(50)},this))}Backbone.history.start()},render:function(){wp.Backbone.View.prototype.render.apply(this,arguments);b("#wpbody-content .wrap").append(this.el);this.views.ready();return this},updateDiff:function(){this.model.diffs.ensure(this.model.get("diffId"),this).done(function(c){if(this.model.get("diffId")!==c.id){return}this.views.set(".revisions-diff-frame",new a.view.Diff({model:c}));this.model.trigger("renderDiff")})}});a.view.Controls=wp.Backbone.View.extend({tagName:"div",className:"revisions-controls",initialize:function(){this.views.add(new a.view.Buttons({mode
|