223966a366
git-svn-id: https://develop.svn.wordpress.org/trunk@24176 602fd350-edb4-49c9-b593-d223f7449a82
1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
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(){var f=this;this.slider=new c.view.Slider();if(null===this.revisions){this.revisions=new d();this.startRightModelLoading();this.revisions.fetch({success:function(){f.stopRightModelLoading();f.completeApplicationSetup()}})}},loadDiffs:function(j){var g=this,i=j.where({completed:false}),h=0,f;_.each(i,function(k){if(k.get("ID")==c.model.settings.revision_id){g.rightDiff=g.revisions.indexOf(k)+1}});_.each(i,function(k){_.delay(function(){k.fetch({update:true,add:false,remove:false,success:function(l){l.set("completed",true);if(0===j.where({completed:false}).length){g.stopModelLoadingSpinner()}f=l.get("linesAdded")+l.get("linesDeleted"),scopeOfChanges="vsmall";if(f>1&&f<=3){scopeOfChanges="small"}else{if(f>3&&f<=5){scopeOfChanges="med"}else{if(f>5&&f<=10){scopeOfChanges="large"}else{if(f>10){scopeOfChanges="vlarge"}}}}l.set("scopeOfChanges",scopeOfChanges);if(0!==g.rightDiff&&l.get("ID")===g.revisions.at(g.rightDiff-1).get("ID")){g.revisionView.render()}g.tickmarkView.render()}})},h);h=h+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()}});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("#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 g,j,m,k,f=0,n,l,o=this,h,i;g=b.slider.option("max");j=b.slider.width();m=b.singleRevision?0:1;k=Math.floor(j/(g-m));k=(k>50)?50:k;k=(k<10)?10:k;j=k*(g-m);n=e(".revision-tick").width();if(k!==n){e(".revision-tick").each(function(){l=Math.floor((k-e(this).width())/2)+1;e(this).css("border-left",l+"px solid #f7f7f7");e(this).css("border-right",(k-l-e(this).width())+"px solid #f7f7f7")});h=e(".revision-tick").first();i=e(".revision-tick").last();j=j+Math.ceil((k-(i.outerWidth()-i.innerWidth()))/2);j=j+Math.ceil((k-(h.outerWidth()-h.innerWidth()))/2);h.css("border-left","none");i.css("border-right","none")}b.slider.setWidth(j);e(".diff-slider-ticks-wrapper").width(j);e("#diff-slider-ticks").width(j);e(".revision-tick").each(function(){b.slider.addTooltip(e(this),b.revisions.at(f++).get("titleTooltip"));e(this).hover(function(){e(this).find(".ui-slider-tooltip").show().append('<div class="arrow"></div>')},function(){e(this).find(".ui-slider-tooltip").hide().find(".arrow").remove()});e(this).on("click",{tickCount:f},function(p){if(b.slider.singleRevision){b.rightDiff=p.data.tickCount;b.slider.refresh({value:b.rightDiff-1})}else{if(p.data.tickCount<b.leftDiff||isRtl&&p.data.tickCount>b.leftDiff){b.leftDiff=p.data.tickCount;b.reloadRight()}else{b.rightDiff=p.data.tickCount;b.reloadLeft()}b.slider.refresh({values:[b.leftDiff,b.rightDiff]})}b.revisionView.render()})})},render:function(){var f=this,g;if(null!==f.model){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(){var f;this.$el.html(this.template);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.rightDiff<this.model.length){b.rightDiff=b.rightDiff+1}b.revisionView.render();b.slider.refresh({value:b.rightDiff-1},true)},previousRevision:function(){if(b.rightDiff>1){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="",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()}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:'<div class="diff-loading"><div class="spinner"></div></div>',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)); |