3 * @file Revisions interface functions, Backbone classes and
4 * the revisions.php document.ready bootstrap.
8 window.wp = window.wp || {};
13 * Expose the module in window.wp.revisions.
15 revisions = wp.revisions = { model: {}, view: {}, controller: {} };
17 // Link post revisions data served from the back end.
18 revisions.settings = window._wpRevisionsSettings || {};
21 revisions.debug = false;
26 * A debugging utility for revisions. Works only when a
27 * debug flag is on and the browser supports it.
29 revisions.log = function() {
30 if ( window.console && revisions.debug ) {
31 window.console.log.apply( window.console, arguments );
35 // Handy functions to help with positioning
36 $.fn.allOffsets = function() {
37 var offset = this.offset() || {top: 0, left: 0}, win = $(window);
38 return _.extend( offset, {
39 right: win.width() - offset.left - this.outerWidth(),
40 bottom: win.height() - offset.top - this.outerHeight()
44 $.fn.allPositions = function() {
45 var position = this.position() || {top: 0, left: 0}, parent = this.parent();
46 return _.extend( position, {
47 right: parent.outerWidth() - position.left - this.outerWidth(),
48 bottom: parent.outerHeight() - position.top - this.outerHeight()
53 * ========================================================================
55 * ========================================================================
57 revisions.model.Slider = Backbone.Model.extend({
68 initialize: function( options ) {
69 this.frame = options.frame;
70 this.revisions = options.revisions;
72 // Listen for changes to the revisions or mode from outside
73 this.listenTo( this.frame, 'update:revisions', this.receiveRevisions );
74 this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode );
76 // Listen for internal changes
77 this.on( 'change:from', this.handleLocalChanges );
78 this.on( 'change:to', this.handleLocalChanges );
79 this.on( 'change:compareTwoMode', this.updateSliderSettings );
80 this.on( 'update:revisions', this.updateSliderSettings );
82 // Listen for changes to the hovered revision
83 this.on( 'change:hoveredRevision', this.hoverRevision );
86 max: this.revisions.length - 1,
87 compareTwoMode: this.frame.get('compareTwoMode'),
88 from: this.frame.get('from'),
89 to: this.frame.get('to')
91 this.updateSliderSettings();
94 getSliderValue: function( a, b ) {
95 return isRtl ? this.revisions.length - this.revisions.indexOf( this.get(a) ) - 1 : this.revisions.indexOf( this.get(b) );
98 updateSliderSettings: function() {
99 if ( this.get('compareTwoMode') ) {
102 this.getSliderValue( 'to', 'from' ),
103 this.getSliderValue( 'from', 'to' )
106 range: true // ensures handles cannot cross
110 value: this.getSliderValue( 'to', 'to' ),
115 this.trigger( 'update:slider' );
118 // Called when a revision is hovered
119 hoverRevision: function( model, value ) {
120 this.trigger( 'hovered:revision', value );
123 // Called when `compareTwoMode` changes
124 updateMode: function( model, value ) {
125 this.set({ compareTwoMode: value });
128 // Called when `from` or `to` changes in the local model
129 handleLocalChanges: function() {
131 from: this.get('from'),
136 // Receives revisions changes from outside the model
137 receiveRevisions: function( from, to ) {
138 // Bail if nothing changed
139 if ( this.get('from') === from && this.get('to') === to ) {
143 this.set({ from: from, to: to }, { silent: true });
144 this.trigger( 'update:revisions', from, to );
149 revisions.model.Tooltip = Backbone.Model.extend({
153 hovering: false, // Whether the mouse is hovering
154 scrubbing: false // Whether the mouse is scrubbing
157 initialize: function( options ) {
158 this.frame = options.frame;
159 this.revisions = options.revisions;
160 this.slider = options.slider;
162 this.listenTo( this.slider, 'hovered:revision', this.updateRevision );
163 this.listenTo( this.slider, 'change:hovering', this.setHovering );
164 this.listenTo( this.slider, 'change:scrubbing', this.setScrubbing );
168 updateRevision: function( revision ) {
169 this.set({ revision: revision });
172 setHovering: function( model, value ) {
173 this.set({ hovering: value });
176 setScrubbing: function( model, value ) {
177 this.set({ scrubbing: value });
181 revisions.model.Revision = Backbone.Model.extend({});
184 * wp.revisions.model.Revisions
186 * A collection of post revisions.
188 revisions.model.Revisions = Backbone.Collection.extend({
189 model: revisions.model.Revision,
191 initialize: function() {
192 _.bindAll( this, 'next', 'prev' );
195 next: function( revision ) {
196 var index = this.indexOf( revision );
198 if ( index !== -1 && index !== this.length - 1 ) {
199 return this.at( index + 1 );
203 prev: function( revision ) {
204 var index = this.indexOf( revision );
206 if ( index !== -1 && index !== 0 ) {
207 return this.at( index - 1 );
212 revisions.model.Field = Backbone.Model.extend({});
214 revisions.model.Fields = Backbone.Collection.extend({
215 model: revisions.model.Field
218 revisions.model.Diff = Backbone.Model.extend({
219 initialize: function() {
220 var fields = this.get('fields');
221 this.unset('fields');
223 this.fields = new revisions.model.Fields( fields );
227 revisions.model.Diffs = Backbone.Collection.extend({
228 initialize: function( models, options ) {
229 _.bindAll( this, 'getClosestUnloaded' );
230 this.loadAll = _.once( this._loadAll );
231 this.revisions = options.revisions;
232 this.postId = options.postId;
236 model: revisions.model.Diff,
238 ensure: function( id, context ) {
239 var diff = this.get( id ),
240 request = this.requests[ id ],
241 deferred = $.Deferred(),
243 from = id.split(':')[0],
244 to = id.split(':')[1];
247 wp.revisions.log( 'ensure', id );
249 this.trigger( 'ensure', ids, from, to, deferred.promise() );
252 deferred.resolveWith( context, [ diff ] );
254 this.trigger( 'ensure:load', ids, from, to, deferred.promise() );
255 _.each( ids, _.bind( function( id ) {
256 // Remove anything that has an ongoing request
257 if ( this.requests[ id ] ) {
260 // Remove anything we already have
261 if ( this.get( id ) ) {
266 // Always include the ID that started this ensure
268 request = this.load( _.keys( ids ) );
271 request.done( _.bind( function() {
272 deferred.resolveWith( context, [ this.get( id ) ] );
273 }, this ) ).fail( _.bind( function() {
278 return deferred.promise();
281 // Returns an array of proximal diffs
282 getClosestUnloaded: function( ids, centerId ) {
284 return _.chain([0].concat( ids )).initial().zip( ids ).sortBy( function( pair ) {
285 return Math.abs( centerId - pair[1] );
286 }).map( function( pair ) {
287 return pair.join(':');
288 }).filter( function( diffId ) {
289 return _.isUndefined( self.get( diffId ) ) && ! self.requests[ diffId ];
293 _loadAll: function( allRevisionIds, centerId, num ) {
294 var self = this, deferred = $.Deferred(),
295 diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
296 if ( _.size( diffs ) > 0 ) {
297 this.load( diffs ).done( function() {
298 self._loadAll( allRevisionIds, centerId, num ).done( function() {
301 }).fail( function() {
302 if ( 1 === num ) { // Already tried 1. This just isn't working. Give up.
304 } else { // Request fewer diffs this time
305 self._loadAll( allRevisionIds, centerId, Math.ceil( num / 2 ) ).done( function() {
316 load: function( comparisons ) {
317 wp.revisions.log( 'load', comparisons );
318 // Our collection should only ever grow, never shrink, so remove: false
319 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() {
320 wp.revisions.log( 'load:complete', comparisons );
324 sync: function( method, model, options ) {
325 if ( 'read' === method ) {
326 options = options || {};
327 options.context = this;
328 options.data = _.extend( options.data || {}, {
329 action: 'get-revision-diffs',
333 var deferred = wp.ajax.send( options ),
334 requests = this.requests;
336 // Record that we're requesting each diff.
337 if ( options.data.compare ) {
338 _.each( options.data.compare, function( id ) {
339 requests[ id ] = deferred;
343 // When the request completes, clear the stored request.
344 deferred.always( function() {
345 if ( options.data.compare ) {
346 _.each( options.data.compare, function( id ) {
347 delete requests[ id ];
354 // Otherwise, fall back to `Backbone.sync()`.
356 return Backbone.Model.prototype.sync.apply( this, arguments );
363 * wp.revisions.model.FrameState
367 * @see wp.revisions.view.Frame
369 * @param {object} attributes Model attributes - none are required.
370 * @param {object} options Options for the model.
371 * @param {revisions.model.Revisions} options.revisions A collection of revisions.
373 revisions.model.FrameState = Backbone.Model.extend({
377 compareTwoMode: false
380 initialize: function( attributes, options ) {
381 var state = this.get( 'initialDiffState' );
382 _.bindAll( this, 'receiveDiff' );
383 this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
385 this.revisions = options.revisions;
387 this.diffs = new revisions.model.Diffs( [], {
388 revisions: this.revisions,
389 postId: this.get( 'postId' )
392 // Set the initial diffs collection.
393 this.diffs.set( this.get( 'diffData' ) );
395 // Set up internal listeners
396 this.listenTo( this, 'change:from', this.changeRevisionHandler );
397 this.listenTo( this, 'change:to', this.changeRevisionHandler );
398 this.listenTo( this, 'change:compareTwoMode', this.changeMode );
399 this.listenTo( this, 'update:revisions', this.updatedRevisions );
400 this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus );
401 this.listenTo( this, 'update:diff', this.updateLoadingStatus );
403 // Set the initial revisions, baseUrl, and mode as provided through attributes.
406 to : this.revisions.get( state.to ),
407 from : this.revisions.get( state.from ),
408 compareTwoMode : state.compareTwoMode
411 // Start the router if browser supports History API
412 if ( window.history && window.history.pushState ) {
413 this.router = new revisions.Router({ model: this });
414 Backbone.history.start({ pushState: true });
418 updateLoadingStatus: function() {
419 this.set( 'error', false );
420 this.set( 'loading', ! this.diff() );
423 changeMode: function( model, value ) {
424 var toIndex = this.revisions.indexOf( this.get( 'to' ) );
426 // If we were on the first revision before switching to two-handled mode,
427 // bump the 'to' position over one
428 if ( value && 0 === toIndex ) {
430 from: this.revisions.at( toIndex ),
431 to: this.revisions.at( toIndex + 1 )
435 // When switching back to single-handled mode, reset 'from' model to
436 // one position before the 'to' model
437 if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode
439 from: this.revisions.at( toIndex - 1 ),
440 to: this.revisions.at( toIndex )
445 updatedRevisions: function( from, to ) {
446 if ( this.get( 'compareTwoMode' ) ) {
447 // TODO: compare-two loading strategy
449 this.diffs.loadAll( this.revisions.pluck('id'), to.id, 40 );
453 // Fetch the currently loaded diff.
455 return this.diffs.get( this._diffId );
458 // So long as `from` and `to` are changed at the same time, the diff
459 // will only be updated once. This is because Backbone updates all of
460 // the changed attributes in `set`, and then fires the `change` events.
461 updateDiff: function( options ) {
462 var from, to, diffId, diff;
464 options = options || {};
465 from = this.get('from');
467 diffId = ( from ? from.id : 0 ) + ':' + to.id;
469 // Check if we're actually changing the diff id.
470 if ( this._diffId === diffId ) {
471 return $.Deferred().reject().promise();
474 this._diffId = diffId;
475 this.trigger( 'update:revisions', from, to );
477 diff = this.diffs.get( diffId );
479 // If we already have the diff, then immediately trigger the update.
481 this.receiveDiff( diff );
482 return $.Deferred().resolve().promise();
483 // Otherwise, fetch the diff.
485 if ( options.immediate ) {
486 return this._ensureDiff();
488 this._debouncedEnsureDiff();
489 return $.Deferred().reject().promise();
494 // A simple wrapper around `updateDiff` to prevent the change event's
495 // parameters from being passed through.
496 changeRevisionHandler: function() {
500 receiveDiff: function( diff ) {
501 // Did we actually get a diff?
502 if ( _.isUndefined( diff ) || _.isUndefined( diff.id ) ) {
507 } else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change
508 this.trigger( 'update:diff', diff );
512 _ensureDiff: function() {
513 return this.diffs.ensure( this._diffId, this ).always( this.receiveDiff );
519 * ========================================================================
521 * ========================================================================
525 * wp.revisions.view.Frame
527 * Top level frame that orchestrates the revisions experience.
529 * @param {object} options The options hash for the view.
530 * @param {revisions.model.FrameState} options.model The frame state model.
532 revisions.view.Frame = wp.Backbone.View.extend({
533 className: 'revisions',
534 template: wp.template('revisions-frame'),
536 initialize: function() {
537 this.listenTo( this.model, 'update:diff', this.renderDiff );
538 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
539 this.listenTo( this.model, 'change:loading', this.updateLoadingStatus );
540 this.listenTo( this.model, 'change:error', this.updateErrorStatus );
542 this.views.set( '.revisions-control-frame', new revisions.view.Controls({
548 wp.Backbone.View.prototype.render.apply( this, arguments );
550 $('html').css( 'overflow-y', 'scroll' );
551 $('#wpbody-content .wrap').append( this.el );
552 this.updateCompareTwoMode();
553 this.renderDiff( this.model.diff() );
559 renderDiff: function( diff ) {
560 this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
565 updateLoadingStatus: function() {
566 this.$el.toggleClass( 'loading', this.model.get('loading') );
569 updateErrorStatus: function() {
570 this.$el.toggleClass( 'diff-error', this.model.get('error') );
573 updateCompareTwoMode: function() {
574 this.$el.toggleClass( 'comparing-two-revisions', this.model.get('compareTwoMode') );
579 * wp.revisions.view.Controls
583 * Contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
585 revisions.view.Controls = wp.Backbone.View.extend({
586 className: 'revisions-controls',
588 initialize: function() {
589 _.bindAll( this, 'setWidth' );
591 // Add the button view
592 this.views.add( new revisions.view.Buttons({
596 // Add the checkbox view
597 this.views.add( new revisions.view.Checkbox({
601 // Prep the slider model
602 var slider = new revisions.model.Slider({
604 revisions: this.model.revisions
607 // Prep the tooltip model
608 tooltip = new revisions.model.Tooltip({
610 revisions: this.model.revisions,
614 // Add the tooltip view
615 this.views.add( new revisions.view.Tooltip({
619 // Add the tickmarks view
620 this.views.add( new revisions.view.Tickmarks({
624 // Add the slider view
625 this.views.add( new revisions.view.Slider({
629 // Add the Metabox view
630 this.views.add( new revisions.view.Metabox({
636 this.top = this.$el.offset().top;
637 this.window = $(window);
638 this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) {
639 var controls = e.data.controls,
640 container = controls.$el.parent(),
641 scrolled = controls.window.scrollTop(),
642 frame = controls.views.parent;
644 if ( scrolled >= controls.top ) {
645 if ( ! frame.$el.hasClass('pinned') ) {
647 container.css('height', container.height() + 'px' );
648 controls.window.on('resize.wp.revisions.pinning click.wp.revisions.pinning', {controls: controls}, function(e) {
649 e.data.controls.setWidth();
652 frame.$el.addClass('pinned');
653 } else if ( frame.$el.hasClass('pinned') ) {
654 controls.window.off('.wp.revisions.pinning');
655 controls.$el.css('width', 'auto');
656 frame.$el.removeClass('pinned');
657 container.css('height', 'auto');
658 controls.top = controls.$el.offset().top;
660 controls.top = controls.$el.offset().top;
665 setWidth: function() {
666 this.$el.css('width', this.$el.parent().width() + 'px');
670 // The tickmarks view
671 revisions.view.Tickmarks = wp.Backbone.View.extend({
672 className: 'revisions-tickmarks',
673 direction: isRtl ? 'right' : 'left',
675 initialize: function() {
676 this.listenTo( this.model, 'change:revision', this.reportTickPosition );
679 reportTickPosition: function( model, revision ) {
680 var offset, thisOffset, parentOffset, tick, index = this.model.revisions.indexOf( revision );
681 thisOffset = this.$el.allOffsets();
682 parentOffset = this.$el.parent().allOffsets();
683 if ( index === this.model.revisions.length - 1 ) {
686 rightPlusWidth: thisOffset.left - parentOffset.left + 1,
687 leftPlusWidth: thisOffset.right - parentOffset.right + 1
691 tick = this.$('div:nth-of-type(' + (index + 1) + ')');
692 offset = tick.allPositions();
694 left: offset.left + thisOffset.left - parentOffset.left,
695 right: offset.right + thisOffset.right - parentOffset.right
698 leftPlusWidth: offset.left + tick.outerWidth(),
699 rightPlusWidth: offset.right + tick.outerWidth()
702 this.model.set({ offset: offset });
706 var tickCount, tickWidth;
707 tickCount = this.model.revisions.length - 1;
708 tickWidth = 1 / tickCount;
709 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
711 _(tickCount).times( function( index ){
712 this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' );
718 revisions.view.Metabox = wp.Backbone.View.extend({
719 className: 'revisions-meta',
721 initialize: function() {
722 // Add the 'from' view
723 this.views.add( new revisions.view.MetaFrom({
725 className: 'diff-meta diff-meta-from'
729 this.views.add( new revisions.view.MetaTo({
735 // The revision meta view (to be extended)
736 revisions.view.Meta = wp.Backbone.View.extend({
737 template: wp.template('revisions-meta'),
740 'click .restore-revision': 'restoreRevision'
743 initialize: function() {
744 this.listenTo( this.model, 'update:revisions', this.render );
747 prepare: function() {
748 return _.extend( this.model.toJSON()[this.type] || {}, {
753 restoreRevision: function() {
754 document.location = this.model.get('to').attributes.restoreUrl;
758 // The revision meta 'from' view
759 revisions.view.MetaFrom = revisions.view.Meta.extend({
760 className: 'diff-meta diff-meta-from',
764 // The revision meta 'to' view
765 revisions.view.MetaTo = revisions.view.Meta.extend({
766 className: 'diff-meta diff-meta-to',
770 // The checkbox view.
771 revisions.view.Checkbox = wp.Backbone.View.extend({
772 className: 'revisions-checkbox',
773 template: wp.template('revisions-checkbox'),
776 'click .compare-two-revisions': 'compareTwoToggle'
779 initialize: function() {
780 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
784 if ( this.model.revisions.length < 3 ) {
785 $('.revision-toggle-compare-mode').hide();
789 updateCompareTwoMode: function() {
790 this.$('.compare-two-revisions').prop( 'checked', this.model.get('compareTwoMode') );
793 // Toggle the compare two mode feature when the compare two checkbox is checked.
794 compareTwoToggle: function() {
795 // Activate compare two mode?
796 this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
801 // Encapsulates the tooltip.
802 revisions.view.Tooltip = wp.Backbone.View.extend({
803 className: 'revisions-tooltip',
804 template: wp.template('revisions-meta'),
806 initialize: function() {
807 this.listenTo( this.model, 'change:offset', this.render );
808 this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
809 this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
812 prepare: function() {
813 if ( _.isNull( this.model.get('revision') ) ) {
816 return _.extend( { type: 'tooltip' }, {
817 attributes: this.model.get('revision').toJSON()
828 position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
830 flipped = ( position / this.model.revisions.length ) > 0.5;
832 direction = flipped ? 'left' : 'right';
833 directionVal = flipped ? 'leftPlusWidth' : direction;
835 direction = flipped ? 'right' : 'left';
836 directionVal = flipped ? 'rightPlusWidth' : direction;
838 otherDirection = 'right' === direction ? 'left': 'right';
839 wp.Backbone.View.prototype.render.apply( this, arguments );
840 css[direction] = this.model.get('offset')[directionVal] + 'px';
841 css[otherDirection] = '';
842 this.$el.toggleClass( 'flipped', flipped ).css( css );
845 visible: function() {
846 return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
849 toggleVisibility: function() {
850 if ( this.visible() ) {
851 this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 );
853 this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } );
860 // Encapsulates all of the configuration for the previous/next buttons.
861 revisions.view.Buttons = wp.Backbone.View.extend({
862 className: 'revisions-buttons',
863 template: wp.template('revisions-buttons'),
866 'click .revisions-next .button': 'nextRevision',
867 'click .revisions-previous .button': 'previousRevision'
870 initialize: function() {
871 this.listenTo( this.model, 'update:revisions', this.disabledButtonCheck );
875 this.disabledButtonCheck();
878 // Go to a specific model index
879 gotoModel: function( toIndex ) {
881 to: this.model.revisions.at( toIndex )
883 // If we're at the first revision, unset 'from'.
885 attributes.from = this.model.revisions.at( toIndex - 1 );
887 this.model.unset('from', { silent: true });
890 this.model.set( attributes );
893 // Go to the 'next' revision
894 nextRevision: function() {
895 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) + 1;
896 this.gotoModel( toIndex );
899 // Go to the 'previous' revision
900 previousRevision: function() {
901 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) - 1;
902 this.gotoModel( toIndex );
905 // Check to see if the Previous or Next buttons need to be disabled or enabled.
906 disabledButtonCheck: function() {
907 var maxVal = this.model.revisions.length - 1,
909 next = $('.revisions-next .button'),
910 previous = $('.revisions-previous .button'),
911 val = this.model.revisions.indexOf( this.model.get('to') );
913 // Disable "Next" button if you're on the last node.
914 next.prop( 'disabled', ( maxVal === val ) );
916 // Disable "Previous" button if you're on the first node.
917 previous.prop( 'disabled', ( minVal === val ) );
923 revisions.view.Slider = wp.Backbone.View.extend({
924 className: 'wp-slider',
925 direction: isRtl ? 'right' : 'left',
928 'mousemove' : 'mouseMove'
931 initialize: function() {
932 _.bindAll( this, 'start', 'slide', 'stop', 'mouseMove', 'mouseEnter', 'mouseLeave' );
933 this.listenTo( this.model, 'update:slider', this.applySliderSettings );
937 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
938 this.$el.slider( _.extend( this.model.toJSON(), {
944 this.$el.hoverIntent({
945 over: this.mouseEnter,
946 out: this.mouseLeave,
950 this.applySliderSettings();
953 mouseMove: function( e ) {
954 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models
955 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider
956 sliderWidth = this.$el.width(), // Width of slider
957 tickWidth = sliderWidth / zoneCount, // Calculated width of zone
958 actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom;
959 currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index
961 // Ensure sane value for currentModelIndex.
962 if ( currentModelIndex < 0 ) {
963 currentModelIndex = 0;
964 } else if ( currentModelIndex >= this.model.revisions.length ) {
965 currentModelIndex = this.model.revisions.length - 1;
968 // Update the tooltip mode
969 this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) });
972 mouseLeave: function() {
973 this.model.set({ hovering: false });
976 mouseEnter: function() {
977 this.model.set({ hovering: true });
980 applySliderSettings: function() {
981 this.$el.slider( _.pick( this.model.toJSON(), 'value', 'values', 'range' ) );
982 var handles = this.$('a.ui-slider-handle');
984 if ( this.model.get('compareTwoMode') ) {
985 // in RTL mode the 'left handle' is the second in the slider, 'right' is first
987 .toggleClass( 'to-handle', !! isRtl )
988 .toggleClass( 'from-handle', ! isRtl );
990 .toggleClass( 'from-handle', !! isRtl )
991 .toggleClass( 'to-handle', ! isRtl );
993 handles.removeClass('from-handle to-handle');
997 start: function( event, ui ) {
998 this.model.set({ scrubbing: true });
1000 // Track the mouse position to enable smooth dragging,
1001 // overrides default jQuery UI step behavior.
1002 $( window ).on( 'mousemove.wp.revisions', { view: this }, function( e ) {
1005 leftDragBoundary = view.$el.offset().left,
1006 sliderOffset = leftDragBoundary,
1007 sliderRightEdge = leftDragBoundary + view.$el.width(),
1008 rightDragBoundary = sliderRightEdge,
1009 leftDragReset = '0',
1010 rightDragReset = '100%',
1011 handle = $( ui.handle );
1013 // In two handle mode, ensure handles can't be dragged past each other.
1014 // Adjust left/right boundaries and reset points.
1015 if ( view.model.get('compareTwoMode') ) {
1016 handles = handle.parent().find('.ui-slider-handle');
1017 if ( handle.is( handles.first() ) ) { // We're the left handle
1018 rightDragBoundary = handles.last().offset().left;
1019 rightDragReset = rightDragBoundary - sliderOffset;
1020 } else { // We're the right handle
1021 leftDragBoundary = handles.first().offset().left + handles.first().width();
1022 leftDragReset = leftDragBoundary - sliderOffset;
1026 // Follow mouse movements, as long as handle remains inside slider.
1027 if ( e.pageX < leftDragBoundary ) {
1028 handle.css( 'left', leftDragReset ); // Mouse to left of slider.
1029 } else if ( e.pageX > rightDragBoundary ) {
1030 handle.css( 'left', rightDragReset ); // Mouse to right of slider.
1032 handle.css( 'left', e.pageX - sliderOffset ); // Mouse in slider.
1037 getPosition: function( position ) {
1038 return isRtl ? this.model.revisions.length - position - 1: position;
1041 // Responds to slide events
1042 slide: function( event, ui ) {
1043 var attributes, movedRevision;
1044 // Compare two revisions mode
1045 if ( this.model.get('compareTwoMode') ) {
1046 // Prevent sliders from occupying same spot
1047 if ( ui.values[1] === ui.values[0] ) {
1051 ui.values.reverse();
1054 from: this.model.revisions.at( this.getPosition( ui.values[0] ) ),
1055 to: this.model.revisions.at( this.getPosition( ui.values[1] ) )
1059 to: this.model.revisions.at( this.getPosition( ui.value ) )
1061 // If we're at the first revision, unset 'from'.
1062 if ( this.getPosition( ui.value ) > 0 ) {
1063 attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 );
1065 attributes.from = undefined;
1068 movedRevision = this.model.revisions.at( this.getPosition( ui.value ) );
1070 // If we are scrubbing, a scrub to a revision is considered a hover
1071 if ( this.model.get('scrubbing') ) {
1072 attributes.hoveredRevision = movedRevision;
1075 this.model.set( attributes );
1079 $( window ).off('mousemove.wp.revisions');
1080 this.model.updateSliderSettings(); // To snap us back to a tick mark
1081 this.model.set({ scrubbing: false });
1086 // This is the view for the current active diff.
1087 revisions.view.Diff = wp.Backbone.View.extend({
1088 className: 'revisions-diff',
1089 template: wp.template('revisions-diff'),
1091 // Generate the options to be passed to the template.
1092 prepare: function() {
1093 return _.extend({ fields: this.model.fields.toJSON() }, this.options );
1097 // The revisions router.
1098 // Maintains the URL routes so browser URL matches state.
1099 revisions.Router = Backbone.Router.extend({
1100 initialize: function( options ) {
1101 this.model = options.model;
1103 // Maintain state and history when navigating
1104 this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
1105 this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl );
1108 baseUrl: function( url ) {
1109 return this.model.get('baseUrl') + url;
1112 updateUrl: function() {
1113 var from = this.model.has('from') ? this.model.get('from').id : 0,
1114 to = this.model.get('to').id;
1115 if ( this.model.get('compareTwoMode' ) ) {
1116 this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ), { replace: true } );
1118 this.navigate( this.baseUrl( '?revision=' + to ), { replace: true } );
1122 handleRoute: function( a, b ) {
1123 var compareTwo = _.isUndefined( b );
1125 if ( ! compareTwo ) {
1126 b = this.model.revisions.get( a );
1127 a = this.model.revisions.prev( b );
1135 * Initialize the revisions UI for revision.php.
1137 revisions.init = function() {
1140 // Bail if the current page is not revision.php.
1141 if ( ! window.adminpage || 'revision-php' !== window.adminpage ) {
1145 state = new revisions.model.FrameState({
1147 // wp_localize_script doesn't stringifies ints, so cast them.
1148 to: parseInt( revisions.settings.to, 10 ),
1149 from: parseInt( revisions.settings.from, 10 ),
1150 // wp_localize_script does not allow for top-level booleans so do a comparator here.
1151 compareTwoMode: ( revisions.settings.compareTwoMode === '1' )
1153 diffData: revisions.settings.diffData,
1154 baseUrl: revisions.settings.baseUrl,
1155 postId: parseInt( revisions.settings.postId, 10 )
1157 revisions: new revisions.model.Revisions( revisions.settings.revisionData )
1160 revisions.view.frame = new revisions.view.Frame({
1165 $( revisions.init );