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)}};b.fn.allOffsets=function(){var d=this.offset()||{top:0,left:0},c=b(window);return _.extend(d,{right:c.width()-d.left-this.outerWidth(),bottom:c.height()-d.top-this.outerHeight()})};b.fn.allPositions=function(){var c=this.position()||{top:0,left:0},d=this.parent();return _.extend(c,{right:d.outerWidth()-c.left-this.outerWidth(),bottom:d.outerHeight()-c.top-this.outerHeight()})};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,offset:{},hovering:false,scrubbing:false},initialize:function(c){this.frame=c.frame;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);this.set({revision:this.frame.diff()})},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)).fail(_.bind(function(){c.reject()}))}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(){e._loadAll(g,c,f).done(function(){d.resolve()})}).fail(function(){if(1===f){d.reject()}else{e._loadAll(g,c,Math.ceil(f/2)).done(function(){d.resolve()})}})}else{d.resolve()}return d},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,error:false,compareTwoMode:false},initialize:function(c,d){var e={};_.bindAll(this,"receiveDiff");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("error",false);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.receiveDiff(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()},receiveDiff:function(c){if(_.isUndefined(c)||_.isUndefined(c.id)){this.set({loading:false,error:true})}else{if(this._diffId===c.id){this.trigger("update:diff",c)}}},_ensureDiff:function(){return this.diffs.ensure(this._diffId,this).always(this.receiveDiff)}});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.listenTo(this.model,"change:error",this.updateErrorStatus);this.views.set(".revisions-control-frame",new a.view.Controls({model:this.model}))},render:function(){wp.Backbone.View.prototype.render.apply(this,arguments);b("html").css("overflow-y","scroll");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"))},updateErrorStatus:function(){this.$el.toggleClass("diff-error",this.model.get("error"))},updateCompareTwoMode:function(){this.$el.toggleClass("comparing-two-revisions",this.model.get("compareTwoMode"))}});a.view.Controls=wp.Backbone.View.extend({className:"revisions-controls",initialize:function(){_.bindAll(this,"setWidth");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});var d=new a.model.Tooltip({frame:this.model,revisions:this.model.revisions,slider:c});this.views.add(new a.view.Tooltip({model:d}));this.views.add(new a.view.Tickmarks({model:d}));this.views.add(new a.view.Slider({model:c}));this.views.add(new a.view.Meta({model:this.model}))},ready:function(){this.top=this.$el.offset().top;this.window=b(window);this.window.on("scroll.wp.revisions",{controls:this},function(g){var d=g.data.controls;var c=d.$el.parent();var f=d.window.scrollTop();var h=d.views.parent;if(f>=d.top){if(!h.$el.hasClass("pinned")){d.setWidth();c.css("height",c.height()+"px");d.window.on("resize.wp.revisions.pinning click.wp.revisions.pinning",{controls:d},function(i){i.data.controls.setWidth()})}h.$el.addClass("pinned")}else{if(h.$el.hasClass("pinned")){d.window.off(".wp.revisions.pinning");d.$el.css("width","auto");h.$el.removeClass("pinned");c.css("height","auto");d.top=d.$el.offset().top}else{d.top=d.$el.offset().top}}})},setWidth:function(){this.$el.css("width",this.$el.parent().width()+"px")}});a.view.Tickmarks=wp.Backbone.View.extend({className:"revisions-tickmarks",direction:isRtl?"right":"left",initialize:function(){this.listenTo(this.model,"change:revision",this.reportTickPosition)},reportTickPosition:function(f,e){var i,h,c,g,d=this.model.revisions.indexOf(e);h=this.$el.allOffsets();c=this.$el.parent().allOffsets();if(d===this.model.revisions.length-1){i={rightPlusWidth:h.left-c.left+1,leftPlusWidth:h.right-c.right+1}}else{g=this.$("div:nth-of-type("+(d+1)+")");i=g.allPositions();_.extend(i,{left:i.left+h.left-c.left,right:i.right+h.right-c.right});_.extend(i,{leftPlusWidth:i.left+g.outerWidth(),rightPlusWidth:i.right+g.outerWidth()})}this.model.set({offset:i})},ready:function(){var d,c;d=this.model.revisions.length-1;c=1/d;_(d).times(function(e){this.$el.append('
')},this)}});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.render)},prepare:function(){return this.model.toJSON()},restoreRevision:function(){document.location=this.model.get("to").attributes.restoreUrl}});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:offset",this.render);this.listenTo(this.model,"change:hovering",this.toggleVisibility);this.listenTo(this.model,"change:scrubbing",this.toggleVisibility)},prepare:function(){return this.model.get("revision").toJSON()},render:function(){var g,e,f,d={},c=this.model.revisions.indexOf(this.model.get("revision"))+1;f=(c/this.model.revisions.length)>0.5;if(isRtl){g=f?"left":"right";e=f?"leftPlusWidth":g}else{g=f?"right":"left";e=f?"rightPlusWidth":g}otherDirection="right"===g?"left":"right";wp.Backbone.View.prototype.render.apply(this,arguments);d[g]=this.model.get("offset")[e]+"px";d[otherDirection]="";this.$el.toggleClass("flipped",f).css(d)},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}});a.view.Buttons=wp.Backbone.View.extend({className:"revisions-buttons",template:wp.template("revisions-buttons"),events:{"click .revisions-next .button":"nextRevision","click .revisions-previous .button":"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(d)};if(d){c.from=this.model.revisions.at(d-1)}else{this.model.unset("from",{silent:true})}this.model.set(c)},nextRevision:function(){var c=this.model.revisions.indexOf(this.model.get("to"))+1;this.gotoModel(c)},previousRevision:function(){var c=this.model.revisions.indexOf(this.model.get("to"))-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",direction:isRtl?"right":"left",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,i=this.$el.allOffsets()[this.direction],g=this.$el.width(),c=g/d,h=isRtl?b(window).width()-j.pageX:j.pageX;h=h-i;var f=Math.floor((h+(c/2))/c);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("to-handle",!!isRtl).toggleClass("from-handle",!isRtl);c.last().toggleClass("from-handle",!!isRtl).toggleClass("to-handle",!isRtl)}else{c.removeClass("from-handle to-handle")}},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(),k=g,l="0",o="100%",h=b(d.handle);if(j.model.get("compareTwoMode")){var m=h.parent().find(".ui-slider-handle");if(h.is(m.first())){k=m.last().offset().left;o=k-f}else{n=m.first().offset().left+m.first().width();l=n-f}}if(i.pageX