a73c2969dd
git-svn-id: https://develop.svn.wordpress.org/trunk@24665 602fd350-edb4-49c9-b593-d223f7449a82
1 line
16 KiB
JavaScript
1 line
16 KiB
JavaScript
window.wp=window.wp||{};(function(b){var a;a=wp.revisions={model:{},view:{},controller:{}};a.settings=_.isUndefined(_wpRevisionsSettings)?{}:_wpRevisionsSettings;a.debug=true;a.log=function(){if(a.debug){console.log.apply(console,arguments)}};if(a.settings.to){a.settings.to=parseInt(a.settings.to,10)}if(a.settings.from){a.settings.from=parseInt(a.settings.from,10)}if(a.settings.compareTwoMode){a.settings.compareTwoMode=a.settings.compareTwoMode==="1"}a.model.Slider=Backbone.Model.extend({defaults:{value:0,min:0,max:1,step:1,compareTwoMode:false},initialize:function(c){this.frame=c.frame;this.revisions=c.revisions;this.set({max:this.revisions.length-1,value:this.revisions.indexOf(this.revisions.get(a.settings.to)),compareTwoMode:this.frame.get("compareTwoMode")});this.listenTo(this.frame,"update:revisions",this.receiveRevisions);this.listenTo(this.frame,"change:compareTwoMode",this.updateMode);this.listenTo(this,"change:from",this.handleLocalChanges);this.listenTo(this,"change:to",this.handleLocalChanges);this.listenTo(this,"change:hoveredRevision",this.hoverRevision)},hoverRevision:function(c,d){this.trigger("hovered:revision",d)},updateMode:function(c,d){this.set({compareTwoMode:d})},handleLocalChanges:function(){this.frame.set({from:this.get("from"),to:this.get("to")})},receiveRevisions:function(d,c){if(this.get("from")===d&&this.get("to")===c){return}this.set({from:d,to:c},{silent:true});this.trigger("update:revisions",d,c)}});a.model.Tooltip=Backbone.Model.extend({defaults:{revision:null,hovering:false,scrubbing:false},initialize:function(c){this.revisions=c.revisions;this.slider=c.slider;this.listenTo(this.slider,"hovered:revision",this.updateRevision);this.listenTo(this.slider,"change:hovering",this.setHovering);this.listenTo(this.slider,"change:scrubbing",this.setScrubbing)},updateRevision:function(c){this.set({revision:c})},setHovering:function(c,d){this.set({hovering:d})},setScrubbing:function(c,d){this.set({scrubbing:d})}});a.model.Revision=Backbone.Model.extend({});a.model.Revisions=Backbone.Collection.extend({model:a.model.Revision,initialize:function(){_.bindAll(this,"next","prev")},comparator:function(e,c){var g=e.get("dateUnix");var d=c.get("dateUnix");var f=(g>d)-(g<d);if(f===0&&e.id!=c.id){f=e.id<c.id?-1:1}return f},next:function(d){var c=this.indexOf(d);if(c!==-1&&c!==this.length-1){return this.at(c+1)}},prev:function(d){var c=this.indexOf(d);if(c!==-1&&c!==0){return this.at(c-1)}}});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(d){var c=this;_.each(d,function(f,e){if(c.get(f)){delete d[e]}});wp.revisions.log("loadNew",d);if(d.length){return this.load(d)}else{return b.Deferred().resolve().promise()}},load:function(c){wp.revisions.log("load",c);return this.fetch({data:{compare:c},remove:false})},loadLast:function(c){var d;c=c||1;d=_.last(this.getProximalDiffIds(),c);if(d.length){return this.loadNew(d)}else{return b.Deferred().resolve().promise()}},loadLastUnloaded:function(c){var d;c=c||1;d=_.last(this.getUnloadedProximalDiffIds(),c);if(d.length){return this.loadNew(d)}else{return b.Deferred().resolve().promise()}},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.ajax.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({defaults:{compareTwoMode:false},initialize:function(c,d){var e={};this._debouncedEnsureDiff=_.debounce(this._ensureDiff,200);this.revisions=d.revisions;this.diffs=new a.model.Diffs([],{revisions:this.revisions});this.diffs.set(a.settings.diffData);e.to=this.revisions.get(a.settings.to);e.from=this.revisions.get(a.settings.from)||this.revisions.prev(e.to);e.compareTwoMode=a.settings.compareTwoMode;e.baseUrl=a.settings.baseUrl;this.set(e);this.router=new a.Router({model:this});Backbone.history.start({pushState:true});this.listenTo(this,"change:from",this.changeRevisionHandler);this.listenTo(this,"change:to",this.changeRevisionHandler);this.listenTo(this,"update:revisions",this.loadSurrounding);this.listenTo(this,"change:compareTwoMode",this.changedMode)},changedMode:function(){this.loadSurrounding(this.get("from"),this.get("to"))},loadSurrounding:function(d,c){if(this.get("compareTwoMode")){}else{if(this.revisions.length){this.diffs.loadLastUnloaded(10).always(_.bind(function(){this.diffs.loadAllBy(50)},this))}}},diff:function(){return this.diffs.get(this._diffId)},updateDiff:function(c){var g,f,d,e;c=c||{};g=this.get("from");f=this.get("to");d=(g?g.id:0)+":"+f.id;if(this._diffId===d){return b.Deferred().fail().promise()}this._diffId=d;this.trigger("update:revisions",g,f);e=this.diffs.get(d);if(e){this.trigger("update:diff",e);return b.Deferred().resolve().promise()}else{if(c.immediate){return this._ensureDiff()}else{this._debouncedEnsureDiff();return b.Deferred().fail().promise()}}},changeRevisionHandler:function(d,e,c){this.updateDiff()},_ensureDiff:function(){return this.diffs.ensure(this._diffId,this).done(function(c){if(this._diffId===c.id){this.trigger("update:diff",c)}})}});a.view.Frame=wp.Backbone.View.extend({className:"revisions",template:wp.template("revisions-frame"),initialize:function(){this.model=new a.model.FrameState({},{revisions:this.collection});this.listenTo(this.model,"update:diff",this.renderDiff);this.listenTo(this.model,"change:compareTwoMode",this.updateCompareTwoMode);this.views.set(".revisions-control-frame",new a.view.Controls({model:this.model}))},render:function(){this.model.updateDiff({immediate:true}).done(_.bind(function(){wp.Backbone.View.prototype.render.apply(this,arguments);b("#wpbody-content .wrap").append(this.el);this.updateCompareTwoMode();this.views.ready()},this));return this},renderDiff:function(c){this.views.set(".revisions-diff-frame",new a.view.Diff({model:c}))},updateCompareTwoMode:function(){this.$el.toggleClass("comparing-two-revisions",this.model.get("compareTwoMode"))}});a.view.Controls=wp.Backbone.View.extend({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}));var c=new a.model.Slider({frame:this.model,revisions:this.model.revisions});this.views.add(new a.view.Tooltip({model:new a.model.Tooltip({revisions:this.model.revisions,slider:c})}));this.views.add(new a.view.Tickmarks({model:this.model}));this.views.add(new a.view.Slider({model:c}));this.views.add(new a.view.Meta({model:this.model}))}});a.view.Tickmarks=wp.Backbone.View.extend({className:"revisions-tickmarks",render:function(){var d,c;d=this.model.revisions.length-1;c=1/d;this.$el.html("");_(d).times(function(){this.$el.append("<div></div>")},this);this.$("div").css("width",(100*c)+"%")},ready:function(){this.render()}});a.view.Meta=wp.Backbone.View.extend({className:"revisions-meta",template:wp.template("revisions-meta"),events:{"click .restore-revision":"restoreRevision"},initialize:function(){this.listenTo(this.model,"update:revisions",this.updateMeta)},restoreRevision:function(){var c=this.model.get("to").attributes.restoreUrl.replace(/&/g,"&");document.location=c},updateMeta:function(d,c){this.$el.html(this.template(this.model.toJSON()));this.$(".restore-revision").prop("disabled",c.attributes.current)}});a.view.Checkbox=wp.Backbone.View.extend({className:"revisions-checkbox",template:wp.template("revisions-checkbox"),events:{"click .compare-two-revisions":"compareTwoToggle"},initialize:function(){this.$el.html(this.template())},updateCompareTwoMode:function(){this.$(".compare-two-revisions").prop("checked",this.model.get("compareTwoMode"))},compareTwoToggle:function(c){this.model.set({compareTwoMode:b(".compare-two-revisions").prop("checked")});this.model.router.updateUrl()},ready:function(){if(this.model.revisions.length<3){b(".revision-toggle-compare-mode").hide()}this.listenTo(this.model,"change:compareTwoMode",this.updateCompareTwoMode);this.updateCompareTwoMode()}});a.view.Tooltip=wp.Backbone.View.extend({className:"revisions-tooltip",template:wp.template("revisions-tooltip"),initialize:function(c){this.listenTo(this.model,"change:revision",this.render);this.listenTo(this.model,"change:hovering",this.toggleVisibility);this.listenTo(this.model,"change:scrubbing",this.toggleVisibility)},ready:function(){this.toggleVisibility({immediate:true})},visible:function(){return this.model.get("scrubbing")||this.model.get("hovering")},toggleVisibility:function(c){c=c||{};var d=this.visible();if(d){this.$el.fadeIn(200)}else{if(c.immediate){this.$el.fadeOut(200)}else{_.delay(function(e){if(!e.visible()){e.toggleVisibility({immediate:true})}},500,this)}}},render:function(){var c;if(_.isNull(this.model.get("revision"))){return}this.$el.html(this.template(this.model.get("revision").toJSON()));c=this.model.revisions.indexOf(this.model.get("revision"))/(this.model.revisions.length-1);c=15+(0.7*c*100);this.$el.css("left",c+"%")}});a.view.Buttons=wp.Backbone.View.extend({className:"revisions-buttons",template:wp.template("revisions-buttons"),events:{"click #next":"nextRevision","click #previous":"previousRevision"},initialize:function(){this.$el.html(this.template());this.listenTo(this.model,"update:revisions",this.disabledButtonCheck)},ready:function(){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.router.updateUrl()},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"));d.prop("disabled",(g===f));e.prop("disabled",(c===f))}});a.view.Slider=wp.Backbone.View.extend({className:"wp-slider",events:{mousemove:"mouseMove",mouseleave:"mouseLeave",mouseenter:"mouseEnter"},initialize:function(){_.bindAll(this,"start","slide","stop","mouseMove");this.listenTo(this.model,"change:compareTwoMode",this.updateSliderSettings);this.listenTo(this.model,"update:revisions",this.updateSliderSettings)},ready:function(){this.$el.slider(_.extend(this.model.toJSON(),{start:this.start,slide:this.slide,stop:this.stop}));this.listenTo(this,"slide:stop",this.updateSliderSettings)},mouseMove:function(j){var d=this.model.revisions.length-1,h=this.$el.offset().left,g=this.$el.width(),c=g/d,i=j.clientX-h,f=Math.floor((i+(c/2))/c);if(isRtl){f=this.model.revisions.length-f-1}if(f<0){f=0}else{if(f>=this.model.revisions.length){f=this.model.revisions.length-1}}this.model.set({hoveredRevision:this.model.revisions.at(f)})},mouseLeave:function(){this.model.set({hovering:false})},mouseEnter:function(){this.model.set({hovering:true})},updateSliderSettings:function(){var d,e,c;if(this.model.get("compareTwoMode")){e=isRtl?this.model.revisions.length-this.model.revisions.indexOf(this.model.get("to"))-1:this.model.revisions.indexOf(this.model.get("from")),c=isRtl?this.model.revisions.length-this.model.revisions.indexOf(this.model.get("from"))-1:this.model.revisions.indexOf(this.model.get("to"));this.$el.slider({values:[e,c],value:null,range:true});d=this.$("a.ui-slider-handle");d.first().toggleClass("right-handle",!!isRtl).toggleClass("left-handle",!isRtl);d.last().toggleClass("left-handle",!!isRtl).toggleClass("right-handle",!isRtl)}else{this.$el.slider({value:isRtl?this.model.revisions.length-this.model.revisions.indexOf(this.model.get("to"))-1:this.model.revisions.indexOf(this.model.get("to")),values:null,range:false});this.$("a.ui-slider-handle").removeClass("left-handle right-handle")}},getSliderPosition:function(c){return isRtl?this.model.revisions.length-c.value-1:c.value},start:function(c,d){this.model.set({scrubbing:true});b(window).on("mousemove.wp.revisions",{view:this},function(i){var j=i.data.view,n=j.$el.offset().left,f=n,g=n+j.$el.width(),l=g,m=0,o=g-f;if(j.model.frame.get("compareTwoMode")){var k=b(d.handle).parent().find(".right-handle"),h=b(d.handle).parent().find(".left-handle");if(b(d.handle).hasClass("left-handle")){if(isRtl){n=k.offset().left+k.width();m=n-f}else{l=k.offset().left;o=l-f}}else{if(isRtl){l=h.offset().left;o=l-f}else{n=h.offset().left+h.width();m=n-f}}}if(i.clientX<n){b(d.handle).css("left",m)}else{if(i.clientX>l){b(d.handle).css("left",o)}else{b(d.handle).css("left",i.clientX-f)}}})},slide:function(e,f){var d;if(!_.isUndefined(f.values)&&this.model.frame.get("compareTwoMode")){if(f.values[1]===f.values[0]){return false}d={to:this.model.revisions.at(isRtl?this.model.revisions.length-f.values[0]-1:f.values[1]),from:this.model.revisions.at(isRtl?this.model.revisions.length-f.values[1]-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{d.from=undefined}}this.model.set(d);if(this.model.get("scrubbing")){this.model.set({hoveredRevision:d.to})}},stop:function(c,d){b(window).off("mousemove.wp.revisions");this.updateSliderSettings();this.model.set({scrubbing:false})}});a.view.Diff=wp.Backbone.View.extend({className:"revisions-diff",template:wp.template("revisions-diff"),prepare:function(){return _.extend({fields:this.model.fields.toJSON()},this.options)}});a.Router=Backbone.Router.extend({initialize:function(c){this.model=c.model;this.routes=this.getRoutes();this.listenTo(this.model,"update:diff",_.debounce(this.updateUrl,250))},getRoutes:function(){var c={};c[this.baseUrl("?from=:from&to=:to")]="handleRoute";c[this.baseUrl("?revision=:to")]="handleRoute";return c},baseUrl:function(c){return this.model.get("baseUrl")+c},updateUrl:function(){var d=this.model.has("from")?this.model.get("from").id:0;var c=this.model.get("to").id;if(this.model.get("compareTwoMode")){this.navigate(this.baseUrl("?from="+d+"&to="+c))}else{this.navigate(this.baseUrl("?revision="+c))}},handleRoute:function(e,d){var i,h,g;if(_.isUndefined(d)){d=this.model.revisions.get(e);e=this.model.revisions.prev(d);d=d?d.id:0;e=e?e.id:0;g=false}else{g=true}i=parseInt(e,10);h=parseInt(d,10);this.model.set({compareTwoMode:g});if(!_.isUndefined(this.model)){var f=this.model.revisions.get(h),c=this.model.revisions.get(i);this.model.set({to:f,from:c})}a.settings.to=h}});a.init=function(){a.view.frame=new a.view.Frame({collection:new a.model.Revisions(a.settings.revisionData)}).render()};b(a.init)}(jQuery)); |