window.wp=window.wp||{};(function(e){var a,d,b,c;c=wp.revisions=function(){b=c.Diff=new b()};_.extend(c,{model:{},view:{},controller:{}});c.model.settings=typeof wpRevisionsSettings==="undefined"?{}:wpRevisionsSettings;b=c.controller.Diff=Backbone.Model.extend({rightDiff:1,leftDiff:1,revisions:null,leftHandleRevisions:null,rightHandleRevisions:null,revisionsInteractions:null,autosaves:true,showSplitView:true,singleRevision:true,leftModelLoading:false,rightModelLoading:false,tickmarkView:null,slider:null,constructor:function(){this.slider=new c.view.Slider();if(null===this.revisions){this.revisions=new d();this.startRightModelLoading();var f=this;this.revisions.fetch({success:function(){f.stopRightModelLoading();f.completeApplicationSetup()}})}},loadDiffs:function(i){var f=this,h=i.where({completed:false}),g=0;_.each(h,function(j){if(j.get("ID")==c.model.settings.revision_id){f.rightDiff=f.revisions.indexOf(j)+1}});_.each(h,function(j){_.delay(function(){j.fetch({update:true,add:false,remove:false,success:function(l){l.set("completed",true);if(0===i.where({completed:false}).length){f.stopModelLoadingSpinner()}f.tickmarkView.render();var k=l.get("linesAdded")+l.get("linesDeleted"),m="vsmall";if(k>1&&k<=3){m="small"}else{if(k>3&&k<=5){m="med"}else{if(k>5&&k<=10){m="large"}else{if(k>10){m="vlarge"}}}}l.set("scopeOfChanges",m);if(0!==f.rightDiff&&l.get("ID")===f.revisions.at(f.rightDiff-1).get("ID")){f.revisionView.render()}}})},g);g=g+150})},startLeftModelLoading:function(){this.leftModelLoading=true;e("#revision-diff-container").addClass("left-model-loading")},stopLeftModelLoading:function(){this.leftModelLoading=false},startRightModelLoading:function(){this.rightModelLoading=true;e("#revision-diff-container").addClass("right-model-loading")},stopRightModelLoading:function(){this.rightModelLoading=false},stopModelLoadingSpinner:function(){e("#revision-diff-container").removeClass("right-model-loading");e("#revision-diff-container").removeClass("left-model-loading")},reloadModel:function(){if(this.singleRevision){this.reloadModelSingle()}else{this.reloadLeftRight()}},reloadModelSingle:function(){var f=this;f.startRightModelLoading();f.revisions.reload({options:{showAutosaves:f.autosaves,showSplitView:f.showSplitView},success:function(){var g=f.revisions.length;f.revisionView.model=f.revisions;f.revisionView.render();f.loadDiffs(f.revisions);f.tickmarkView.model=f.revisions;f.tickmarkView.render();f.slider.refresh({max:g-1,value:f.rightDiff-1},true)},error:function(){f.stopRightModelLoading()}})},reloadLeft:function(){var f=this;f.startLeftModelLoading();f.leftHandleRevisions=new d({},{compareTo:f.revisions.at(f.rightDiff-1).get("ID"),showAutosaves:f.autosaves,showSplitView:f.showSplitView,rightHandleAt:f.rightDiff});f.leftHandleRevisions.fetch({success:function(){f.stopLeftModelLoading();f.loadDiffs(f.leftHandleRevisions);f.tickmarkView.model=f.leftHandleRevisions;f.slider.refresh({max:f.revisions.length});if(f.rightDiff>f.revisions.length){f.rightDiff=f.revisions.length}},error:function(){f.stopLeftModelLoading()}})},reloadRight:function(){var f=this;f.startRightModelLoading();f.rightHandleRevisions=new d({},{compareTo:f.revisions.at(f.leftDiff-1).get("ID"),showAutosaves:f.autosaves,showSplitView:f.showSplitView,leftHandleAt:f.leftDiff});f.rightHandleRevisions.fetch({success:function(){f.stopRightModelLoading();f.loadDiffs(f.rightHandleRevisions);f.tickmarkView.model=f.rightHandleRevisions;f.slider.refresh({max:f.revisions.length,values:[f.leftDiff,f.rightDiff]},true)},error:function(g){f.stopRightModelLoading()}})},reloadLeftRight:function(){this.startRightModelLoading();this.startLeftModelLoading();this.reloadLeft();this.reloadRight()},disabledButtonCheck:function(h){var i=this.revisions.length-1,f=e("#next"),g=e("#previous");if(i===h){f.prop("disabled",true)}else{f.prop("disabled",false)}if(0===h){g.prop("disabled",true)}else{g.prop("disabled",false)}},completeApplicationSetup:function(){this.revisionView=new c.view.Diff({model:this.revisions});this.revisionView.render();this.loadDiffs(this.revisions);this.revisionsInteractions=new c.view.Interact({model:this.revisions});this.revisionsInteractions.render();this.tickmarkView=new c.view.Tickmarks({model:this.revisions});this.tickmarkView.render();this.tickmarkView.resetTicks()}});c.view.Slider=Backbone.View.extend({el:e("#diff-slider"),singleRevision:true,initialize:function(f){this.options=_.defaults(f||{},{value:0,min:0,max:1,step:1})},slide:function(f,g){if(this.singleRevision){b.rightDiff=(g.value+1);b.revisionView.render();b.disabledButtonCheck(g.value)}else{if(g.values[0]===g.values[1]){return false}if(e(g.handle).hasClass("left-handle")){if(b.leftModelLoading){return false}b.leftDiff=g.values[0]}else{if(b.rightModelLoading){return false}b.rightDiff=g.values[1]}b.revisionView.render()}},start:function(f,g){if(this.singleRevision){return}if(e(g.handle).hasClass("left-handle")){if(b.leftModelLoading){return false}b.revisionView.draggingLeft=true;if(b.revisionView.model!==b.leftHandleRevisions&&null!==b.leftHandleRevisions){b.revisionView.model=b.leftHandleRevisions;b.tickmarkView.model=b.leftHandleRevisions;b.tickmarkView.render()}b.leftDiffStart=g.values[0]}else{if(b.rightModelLoading||0===b.rightHandleRevisions.length){return false}if(b.revisionView.model!==b.rightHandleRevisions&&null!==b.rightHandleRevisions){b.revisionView.model=b.rightHandleRevisions;b.tickmarkView.model=b.rightHandleRevisions;b.tickmarkView.render()}b.revisionView.draggingLeft=false;b.rightDiffStart=g.values[1]}},stop:function(f,g){if(this.singleRevision){return}if(e(g.handle).hasClass("left-handle")){if(b.leftDiffStart!==g.values[0]){b.reloadRight()}}else{if(b.rightDiffStart!==g.values[1]){b.reloadLeft()}}},addTooltip:function(g,f){g.find(".ui-slider-tooltip").html(f)},width:function(){return e("#diff-slider").width()},setWidth:function(f){e("#diff-slider").width(f)},refresh:function(g,f){e("#diff-slider").slider("option",g);if(!f){e("a.ui-slider-handle").html('')}if(f){e("#diff-slider").trigger("slide")}b.disabledButtonCheck(g.value)},option:function(f){return e("#diff-slider").slider("option",f)},render:function(){var f=this;e("#diff-slider").slider({slide:e.proxy(f.slide,f),start:e.proxy(f.start,f),stop:e.proxy(f.stop,f)});this.refresh(this.options)}});c.view.Tickmarks=Backbone.View.extend({el:e("#diff-slider-ticks"),template:wp.template("revision-ticks"),model:a,resetTicks:function(){var j=b.slider.option("max");var i=b.slider.width();var g=b.singleRevision?0:1;var f=Math.floor(i/(j-g));f=(f>50)?50:f;f=(f<10)?10:f;i=f*(j-g)+1;b.slider.setWidth(i);e(".diff-slider-ticks-wrapper").width(i);e("#diff-slider-ticks").width(i);var h=e(".revision-tick").width();if(f!==h){e(".revision-tick").each(function(){e(this).css("margin-right",f-1+"px")});e(".revision-tick").last().css("margin-right","0")}},render:function(){var f=this;if(null!==f.model){var g="";_.each(f.model.models,function(h){g=g+f.template(h.toJSON())});f.$el.html(g)}f.resetTicks();return f}});c.view.Interact=Backbone.View.extend({el:e("#revision-interact"),template:wp.template("revision-interact"),events:{"click #next":"nextRevision","click #previous":"previousRevision"},render:function(){this.$el.html(this.template);var f=b.revisions.length;b.slider.singleRevision=b.singleRevision;b.slider.render();if(b.singleRevision){b.slider.refresh({value:b.rightDiff-1,min:0,max:f-1});e("#revision-diff-container").removeClass("comparing-two-revisions")}else{b.slider.refresh({values:[b.leftDiff,b.rightDiff+1],min:1,max:f+1,range:true});e("#revision-diff-container").addClass("comparing-two-revisions");e("#diff-slider a.ui-slider-handle").first().addClass("left-handle");e("#diff-slider a.ui-slider-handle").last().addClass("right-handle")}return this},nextRevision:function(){if(b.rightDiff1){b.rightDiff=b.rightDiff-1}b.revisionView.render();b.slider.refresh({value:b.rightDiff-1},true)}});c.view.Diff=Backbone.View.extend({el:e("#revisions-diff"),template:wp.template("revisions-diff"),draggingLeft:false,events:{"click #compare-two-revisions":"compareTwo","click #restore-revision":"restore"},render:function(){var g="";var f;if(!b.singleRevision){if(this.draggingLeft){f=b.leftDiff-1;if(this.model.at(f)){g=this.template(this.model.at(f).toJSON())}}else{f=b.rightDiff-1;if(this.model.at(f)){g=this.template(this.model.at(f).toJSON())}}}else{if(this.model.at(b.rightDiff-1)){g=this.template(this.model.at(b.rightDiff-1).toJSON())}}this.$el.html(g);if(this.model.length<2){e("#diff-slider").hide();e(".diff-slider-ticks-wrapper").hide()}if(!b.singleRevision){b.slider.addTooltip(e("a.ui-slider-handle.left-handle"),(b.leftDiff<0)?"":b.revisions.at(b.leftDiff-1).get("titleTooltip"));b.slider.addTooltip(e("a.ui-slider-handle.right-handle"),(b.rightDiff>b.revisions.length)?"":b.revisions.at(b.rightDiff-1).get("titleTooltip"))}else{b.slider.addTooltip(e("a.ui-slider-handle"),(b.rightDiff>b.revisions.length)?"":b.revisions.at(b.rightDiff-1).get("titleTooltip"))}this.toggleCompareTwoCheckbox();e("#restore-revision").toggle(!b.revisions.at(b.rightDiff-1).get("isCurrent"));return this},toggleCompareTwoCheckbox:function(){if(this.model.length<3){e("#toggle-revision-compare-mode").hide()}e("#compare-two-revisions").prop("checked",!b.singleRevision)},compareTwo:function(){if(e("#compare-two-revisions").is(":checked")){b.singleRevision=false;if(1===b.rightDiff){b.rightDiff=2}b.revisionView.draggingLeft=false;c.model.settings.revision_id="";b.reloadLeftRight();b.revisionView.model=b.rightHandleRevisions}else{b.singleRevision=true;b.revisionView.draggingLeft=false;b.reloadModelSingle()}b.revisionsInteractions.render();b.tickmarkView.render()},restore:function(){document.location=e("#restore-revision").data("restoreLink")}});a=c.model.Revision=Backbone.Model.extend({idAttribute:"ID",defaults:{ID:0,titleTo:"",titleTooltip:"",titleFrom:"",diff:'
',restoreLink:"",completed:false,linesAdded:0,linesDeleted:0,scopeOfChanges:"none",previousID:0,isCurrent:false},url:function(){if(b.singleRevision){return ajaxurl+"?action=revisions-data&show_autosaves=true&show_split_view=true&nonce="+c.model.settings.nonce+"&single_revision_id="+this.id+"&compare_to="+this.get("previousID")+"&post_id="+c.model.settings.post_id}else{return this.collection.url()+"&single_revision_id="+this.id}}});d=c.Revisions=Backbone.Collection.extend({model:a,initialize:function(g,f){this.options=_.defaults(f||{},{compareTo:c.model.settings.post_id,post_id:c.model.settings.post_id,showAutosaves:true,showSplitView:true,rightHandleAt:0,leftHandleAt:0,nonce:c.model.settings.nonce})},url:function(){return ajaxurl+"?action=revisions-data&compare_to="+this.options.compareTo+"&post_id="+this.options.post_id+"&show_autosaves="+this.options.showAutosaves+"&show_split_view="+this.options.showSplitView+"&right_handle_at="+this.options.rightHandleAt+"&left_handle_at="+this.options.leftHandleAt+"&nonce="+this.options.nonce},reload:function(f){this.options=_.defaults(f.options||{},this.options);this.fetch({success:f.success||null,error:f.error||null})}});e(wp.revisions)}(jQuery));