Wordpress/wp-admin/js/revisions.min.js

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=false;a.log=function(){if(window.console&&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:null,values:null,min:0,max:1,step:1,range:false,compareTwoMode:false},initialize:function(c){this.frame=c.frame;this.revisions=c.revisions;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:compareTwoMode",this.updateSliderSettings);this.listenTo(this,"update:revisions",this.updateSliderSettings);this.listenTo(this,"change:hoveredRevision",this.hoverRevision);this.set({max:this.revisions.length-1,compareTwoMode:this.frame.get("compareTwoMode"),from:this.frame.get("from"),to:this.frame.get("to")});this.updateSliderSettings()},getSliderValue:function(d,c){return isRtl?this.revisions.length-this.revisions.indexOf(this.get(d))-1:this.revisions.indexOf(this.get(c))},updateSliderSettings:function(){if(this.get("compareTwoMode")){this.set({values:[this.getSliderValue("to","from"),this.getSliderValue("from","to")],value:null,range:true})}else{this.set({value:this.getSliderValue("to","to"),values:null,range:false})}this.trigger("update:slider")},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")},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){_.bindAll(this,"getClosestUnloaded");this.loadAll=_.once(this._loadAll);this.revisions=c.revisions;this.requests={}},model:a.model.Diff,ensure:function(j,d){var g=this.get(j);var f=this.requests[j];var c=b.Deferred();var e={};var i=j.split(":")[0];var h=j.split(":")[1];e[j]=true;wp.revisions.log("ensure",j);this.trigger("ensure",e,i,h,c.promise());if(g){c.resolveWith(d,[g])}else{this.trigger("ensure:load",e,i,h,c.promise());_.each(e,_.bind(function(k){if(this.requests[k]){delete e[k]}if(this.get(k)){delete e[k]}},this));if(!f){e[j]=true;f=this.load(_.keys(e))}f.done(_.bind(function(){c.resolveWith(d,[this.get(j)])},this))}return c.promise()},getClosestUnloaded:function(e,c){var d=this;return _.chain([0].concat(e)).initial().zip(e).sortBy(function(f){return Math.abs(c-f[1])}).map(function(f){return f.join(":")}).filter(function(f){return _.isUndefined(d.get(f))&&!d.requests[f]}).value()},_loadAll:function(g,c,f){var e=this,d=b.Deferred();diffs=_.first(this.getClosestUnloaded(g,c),f);if(_.size(diffs)>0){this.load(diffs).done(function(){d.resolve();e._loadAll(g,c,f)});return d.promise()}else{return d.reject().promise()}},load:function(c){wp.revisions.log("load",c);return this.fetch({data:{compare:c},remove:false}).done(function(){wp.revisions.log("load:complete",c)})},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:{loading:false,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);this.listenTo(this,"change:from",this.changeRevisionHandler);this.listenTo(this,"change:to",this.changeRevisionHandler);this.listenTo(this,"change:compareTwoMode",this.changeMode);this.listenTo(this,"update:revisions",this.updatedRevisions);this.listenTo(this.diffs,"ensure:load",this.updateLoadingStatus);this.listenTo(this,"update:diff",this.updateLoadingStatus);e.to=this.revisions.get(a.settings.to);e.from=this.revisions.get(a.settings.from);e.compareTwoMode=a.settings.compareTwoMode;e.baseUrl=a.settings.baseUrl;this.set(e);if(window.history&&window.history.pushState){this.router=new a.Router({model:this});Backbone.history.start({pushState:true})}},updateLoadingStatus:function(){this.set("loading",!this.diff())},changeMode:function(c,d){if(d&&0===this.revisions.indexOf(this.get("to"))){this.set({from:this.revisions.at(0),to:this.revisions.at(1)})}},updatedRevisions:function(d,c){if(this.get("compareTwoMode")){}else{this.diffs.loadAll(this.revisions.pluck("id"),c.id,40)}},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().reject().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().reject().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.listenTo(this.model,"update:diff",this.renderDiff);this.listenTo(this.model,"change:compareTwoMode",this.updateCompareTwoMode);this.listenTo(this.model,"change:loading",this.updateLoadingStatus);this.views.set(".revisions-control-frame",new a.view.Controls({model:this.model}))},render:function(){wp.Backbone.View.prototype.render.apply(this,arguments);b("#wpbody-content .wrap").append(this.el);this.updateCompareTwoMode();this.renderDiff(this.model.diff());this.views.ready();return this},renderDiff:function(c){this.views.set(".revisions-diff-frame",new a.view.Diff({model:c}))},updateLoadingStatus:function(){this.$el.toggleClass("loading",this.model.get("loading"))},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",ready:function(){var d,c;d=this.model.revisions.length-1;c=1/d;_(d).times(function(){this.$el.append("<div></div>")},this);this.$("div").css("width",(100*c)+"%")}});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.ready)},prepare:function(){return this.model.toJSON()},ready:function(){this.$(".restore-revision").prop("disabled",this.model.get("to").get("current"))},restoreRevision:function(){var c=this.model.get("to").attributes.restoreUrl.replace(/&amp;/g,"&");document.location=c}});a.view.Checkbox=wp.Backbone.View.extend({className:"revisions-checkbox",template:wp.template("revisions-checkbox"),events:{"click .compare-two-revisions":"compareTwoToggle"},initialize:function(){this.listenTo(this.model,"change:compareTwoMode",this.updateCompareTwoMode)},ready:function(){if(this.model.revisions.length<3){b(".revision-toggle-compare-mode").hide()}},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")})}});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)},visible:function(){return this.model.get("scrubbing")||this.model.get("hovering")},toggleVisibility:function(c){if(this.visible()){this.$el.stop().show().fadeTo(100-this.el.style.opacity*100,1)}else{this.$el.stop().fadeTo(this.el.style.opacity*300,0,function(){b(this).hide()})}return},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(isRtl?"right":"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.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)},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"},initialize:function(){_.bindAll(this,"start","slide","stop","mouseMove","mouseEnter","mouseLeave");this.listenTo(this.model,"update:slider",this.applySliderSettings)},ready:function(){this.$el.slider(_.extend(this.model.toJSON(),{start:this.start,slide:this.slide,stop:this.stop}));this.$el.hoverIntent({over:this.mouseEnter,out:this.mouseLeave,timeout:800});this.applySliderSettings()},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})},applySliderSettings:function(){this.$el.slider(_.pick(this.model.toJSON(),"value","values","range"));var c=this.$("a.ui-slider-handle");if(this.model.get("compareTwoMode")){c.first().toggleClass("right-handle",!!isRtl).toggleClass("left-handle",!isRtl);c.last().toggleClass("left-handle",!!isRtl).toggleClass("right-handle",!isRtl)}else{c.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.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,g){var d,f,c;if(this.model.get("compareTwoMode")){if(g.values[1]===g.values[0]){return false}d={to:this.model.revisions.at(isRtl?this.model.revisions.length-g.values[0]-1:g.values[1]),from:this.model.revisions.at(isRtl?this.model.revisions.length-g.values[1]-1:g.values[0])};if(isRtl){f=g.value===g.values[1]?d.from:d.to}else{f=g.value===g.values[0]?d.from:d.to}}else{c=this.getSliderPosition(g);d={to:this.model.revisions.at(c)};f=d.to;if(c){d.from=this.model.revisions.at(c-1)}else{d.from=undefined}}if(this.model.get("scrubbing")){d.hoveredRevision=f}this.model.set(d)},stop:function(c,d){b(window).off("mousemove.wp.revisions");this.model.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=_.object([[this.baseUrl("?from=:from&to=:to"),"handleRoute"],[this.baseUrl("?from=:from&to=:to"),"handleRoute"]]);this.listenTo(this.model,"update:diff",_.debounce(this.updateUrl,250));this.listenTo(this.model,"change:compareTwoMode",this.updateUrl)},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(d,c){var g,f,e=_.isUndefined(c);if(!e){c=this.model.revisions.get(d);d=this.model.revisions.prev(c);c=c?c.id:0;d=d?d.id:0}this.model.set({from:this.model.revisions.get(parseInt(d,10)),to:this.model.revisions.get(parseInt(d,10)),compareTwoMode:e})}});a.init=function(){a.view.frame=new a.view.Frame({model:new a.model.FrameState({},{revisions:new a.model.Revisions(a.settings.revisionData)})}).render()};b(a.init)}(jQuery));