2fd98a828c
git-svn-id: https://develop.svn.wordpress.org/trunk@24561 602fd350-edb4-49c9-b593-d223f7449a82
1 line
13 KiB
JavaScript
1 line
13 KiB
JavaScript
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({model:this.model}));this.views.add(new a.view.Checkbox({model:this.model}));this.views.add(new a.view.Tooltip({model:this.model}));this.views.add(new a.view.Slider({model:this.model}));this.views.add(new a.view.Meta({model:this.model}))}});a.view.Meta=wp.Backbone.View.extend({tagName:"div",className:"revisions-meta",template:wp.template("revisions-meta"),events:{"click #restore-revision":"restoreRevision"},initialize:function(){this.listenTo(this.model,"change:diffId",this.updateMeta)},restoreRevision:function(){var c=this.model.get("to").attributes.restoreUrl.replace(/&/g,"&");document.location=c},updateMeta:function(){this.$el.html(this.template(this.model.toJSON()));if(this.model.get("to").attributes.current){b("#restore-revision").prop("disabled",true)}else{b("#restore-revision").prop("disabled",false)}}});a.view.Checkbox=wp.Backbone.View.extend({tagName:"div",className:"revisions-checkbox",template:wp.template("revisions-checkbox"),events:{"click .compare-two-revisions":"compareTwoToggle"},initialize:function(){this.$el.html(this.template());this.listenTo(this.model,"change:compareTwoMode",this.updateCompareTwoMode)},updateCompareTwoMode:function(){if(this.model.get("compareTwoMode")){b(".compare-two-revisions").parent().css("border","1px solid #f00;").prop("checked",true);b(".revisions-control-frame").addClass("comparing-two-revisions");b(".wp-slider a.ui-slider-handle").first().addClass(isRtl?"right-handle":"left-handle");b(".wp-slider a.ui-slider-handle").last().addClass(isRtl?"left-handle":"right-handle")}else{b(".compare-two-revisions").prop("checked",false);b(".revisions-control-frame").removeClass("comparing-two-revisions");b(".wp-slider a.ui-slider-handle").removeClass("left-handle").removeClass("right-handle")}},compareTwoToggle:function(c){if(b(".compare-two-revisions").is(":checked")){this.model.set({compareTwoMode:true})}else{this.model.set({compareTwoMode:false})}this.model.revisionsRouter.navigateRoute(this.model.get("to").id,this.model.get("from").id)},ready:function(){if(this.model.revisions.length<3){b(".revision-toggle-compare-mode").hide()}}});a.view.Tooltip=wp.Backbone.View.extend({tagName:"div",className:"revisions-tooltip",template:wp.template("revisions-tooltip"),initialize:function(){this.listenTo(this.model,"change:sliderHovering",this.sliderHoveringChanged);this.listenTo(this.model,"change:tooltipPosition",this.tooltipPositionChanged)},ready:function(){},sliderHoveringChanged:function(){if(this.model.get("sliderHovering")){this.$el.show()}else{this.$el.hide()}},tooltipPositionChanged:function(){this.$el.html(this.template(this.model.revisions.at(this.model.get("hoveringAt")).toJSON()));this.setTooltip(this.model.get("tooltipPosition"))},setTooltip:function(d){var e=b(".revisions-buttons").offset().left,c=d-e;this.$el.find(".ui-slider-tooltip").css("left",c);this.$el.find(".arrow").css("left",c)}});a.view.Buttons=wp.Backbone.View.extend({tagName:"div",className:"revisions-buttons",template:wp.template("revisions-buttons"),events:{"click #next":"nextRevision","click #previous":"previousRevision"},initialize:function(){this.$el.html(this.template())},ready:function(){this.listenTo(this.model,"change:diffId",this.disabledButtonCheck)},gotoModel:function(d){var c={to:this.model.revisions.at(isRtl?this.model.revisions.length-d-1:d)};if(isRtl?this.model.revisions.length-d-1:d){c.from=this.model.revisions.at(isRtl?this.model.revisions.length-d-2:d-1)}else{this.model.unset("from",{silent:true})}this.model.set(c);this.model.revisionsRouter.navigateRoute(c.to.id,c.from?c.from.id:0)},nextRevision:function(){var c=isRtl?this.model.revisions.length-this.model.revisions.indexOf(this.model.get("to"))-1:this.model.revisions.indexOf(this.model.get("to"));c=isRtl?c-1:c+1;this.gotoModel(c)},previousRevision:function(){var c=isRtl?this.model.revisions.length-this.model.revisions.indexOf(this.model.get("to"))-1:this.model.revisions.indexOf(this.model.get("to"));c=isRtl?c+1:c-1;this.gotoModel(c)},disabledButtonCheck:function(){var g=this.model.revisions.length-1,c=0,d=b(".revisions-next .button"),e=b(".revisions-previous .button"),f=this.model.revisions.indexOf(this.model.get("to"));if(g===f){d.prop("disabled",true)}else{d.prop("disabled",false)}if(c===f){e.prop("disabled",true)}else{e.prop("disabled",false)}}});a.view.Slider=wp.Backbone.View.extend({tagName:"div",className:"wp-slider",events:{mousemove:"mousemove",mouseenter:"mouseenter",mouseleave:"mouseleave"},initialize:function(){_.bindAll(this,"start","slide","stop");var c=this.model.revisions.length-1;var d=this.model.revisions.indexOf(this.model.revisions.findWhere({id:Number(a.settings.selectedRevision)}));this.settings=new a.model.Slider({max:c,value:d,start:this.start,slide:this.slide,stop:this.stop})},ready:function(){this.$el.slider(this.settings.toJSON());this.listenTo(this.model,"change:compareTwoMode",this.updateSliderSettings);this.settings.on("change",function(d,c){this.updateSliderSettings()},this);this.listenTo(this.model,"change:diffId",this.diffIdChanged);this.slide("",this.settings.attributes)},mousemove:function(i){var f=Math.ceil(this.$el.offset().left),d=Math.ceil(this.$el.width())+2,c=Math.ceil((d)/this.model.revisions.length),h=i.clientX-f,g=Math.floor(h/c);if(isRtl){g=this.model.revisions.length-g-1}if(g<0){g=0}else{if(g>=this.model.revisions.length){g=this.model.revisions.length-1}}this.model.set("hoveringAt",g);this.model.set("tooltipPosition",i.clientX)},mouseenter:function(c){this.model.set("sliderHovering",true)},mouseleave:function(c){this.model.set("sliderHovering",false)},updateSliderSettings:function(){if(isRtl){this.$el.slider({value:this.model.revisions.length-this.model.revisions.indexOf(this.model.get("to"))-1})}else{if(this.model.get("compareTwoMode")){this.$el.slider({values:[this.model.revisions.indexOf(this.model.get("from")),this.model.revisions.indexOf(this.model.get("to"))],value:null,range:true})}else{this.$el.slider({value:this.model.revisions.indexOf(this.model.get("to")),values:null,range:false})}}if(this.model.get("compareTwoMode")){b(".revisions").addClass("comparing-two-revisions");b("a.ui-slider-handle",this.$el).first().addClass(isRtl?"right-handle":"left-handle").removeClass(isRtl?"left-handle":"right-handle");b("a.ui-slider-handle",this.$el).last().addClass(isRtl?"left-handle":"right-handle").removeClass(isRtl?"right-handle":"left-handle")}else{b(".revisions").removeClass("comparing-two-revisions")}},diffIdChanged:function(){if(this.model.get("compareTwoMode")){this.settings.set({values:[this.model.revisions.indexOf(this.model.get("from")),this.model.revisions.indexOf(this.model.get("to"))]})}else{this.settings.set({value:this.model.revisions.indexOf(this.model.get("to"))})}},getSliderPosition:function(c){return isRtl?this.model.revisions.length-c.value-1:c.value},start:function(c,d){if(!this.model.get("compareTwoMode")){return}b(window).mousemove(function(h){var g=this.$el.offset().left,f=g+this.$el.width();if(h.clientX<g){b(d.handle).css("left",0)}else{if(h.clientX>f){b(d.handle).css("left",f-g)}else{b(d.handle).css("left",h.clientX-g)}}})},slide:function(e,f){var d;if("undefined"!==typeof f.values&&this.model.get("compareTwoMode")){if(f.values[1]===f.values[0]){return false}d={to:this.model.revisions.at(isRtl?this.model.revisions.length-f.values[1]-1:f.values[1]),from:this.model.revisions.at(isRtl?this.model.revisions.length-f.values[0]-1:f.values[0])}}else{var c=this.getSliderPosition(f);d={to:this.model.revisions.at(c)};if(c){d.from=this.model.revisions.at(c-1)}else{this.model.unset("from",{silent:true})}}this.model.set(d)},stop:function(c,d){if(!this.model.get("compareTwoMode")){return}b(window).unbind("mousemove");this.settings.trigger("change")}});a.view.Diff=wp.Backbone.View.extend({tagName:"div",className:"revisions-diff",template:wp.template("revisions-diff"),prepare:function(){return _.extend({fields:this.model.fields.toJSON()},this.options)}});a.router.Router=Backbone.Router.extend({initialize:function(c){this.model=c.model;this.listenTo(this.model,"renderDiff",this.updateURL)},routes:{"revision/from/:from/to/:to/handles/:handles":"gotoRevisionId"},navigateRoute:function(e,d){var c="/revision/from/"+d+"/to/"+e+"/handles/";if(this.model.get("compareTwoMode")){c=c+"2"}else{c=c+"1"}this.navigate(c)},updateURL:_.debounce(function(){var c=this.model.get("from");this.navigateRoute(this.model.get("to").id,c?c.id:0)},250),gotoRevisionId:function(g,f,e){if("2"===e){this.model.set({compareTwoMode:true})}else{this.model.set({compareTwoMode:false})}if("undefined"!==typeof this.model){var d=this.model.revisions.findWhere({id:Number(f)}),c=this.model.revisions.findWhere({id:Number(g)});this.model.set({to:d,from:c})}}});a.init=function(){a.view.frame=new a.view.Frame({collection:new a.model.Revisions(a.settings.revisionData)}).render()};b(a.init)}(jQuery)); |