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.selectedRevision){a.settings.selectedRevision=parseInt(a.settings.selectedRevision,10)}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.selectedRevision)),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")},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.clientXl){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.listenTo(this.model,"update:diff",_.debounce(this.updateUrl,250))},routes:{"from/:from/to/:to":"handleRoute","at/:to":"handleRoute"},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("from/"+d+"/to/"+c)}else{this.navigate("at/"+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.selectedRevision=h}});a.init=function(){a.view.frame=new a.view.Frame({collection:new a.model.Revisions(a.settings.revisionData)}).render()};b(a.init)}(jQuery));