1 window.wp = window.wp || {};
6 revisions = wp.revisions = { model: {}, view: {}, controller: {} };
9 revisions.settings = _.isUndefined( _wpRevisionsSettings ) ? {} : _wpRevisionsSettings;
12 revisions.debug = false;
14 revisions.log = function() {
15 if ( window.console && revisions.debug )
16 console.log.apply( console, arguments );
19 // Handy functions to help with positioning
20 $.fn.allOffsets = function() {
21 var offset = this.offset() || {top: 0, left: 0}, win = $(window);
22 return _.extend( offset, {
23 right: win.width() - offset.left - this.outerWidth(),
24 bottom: win.height() - offset.top - this.outerHeight()
28 $.fn.allPositions = function() {
29 var position = this.position() || {top: 0, left: 0}, parent = this.parent();
30 return _.extend( position, {
31 right: parent.outerWidth() - position.left - this.outerWidth(),
32 bottom: parent.outerHeight() - position.top - this.outerHeight()
36 // wp_localize_script transforms top-level numbers into strings. Undo that.
37 if ( revisions.settings.to )
38 revisions.settings.to = parseInt( revisions.settings.to, 10 );
39 if ( revisions.settings.from )
40 revisions.settings.from = parseInt( revisions.settings.from, 10 );
42 // wp_localize_script does not allow for top-level booleans. Fix that.
43 if ( revisions.settings.compareTwoMode )
44 revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1';
47 * ========================================================================
49 * ========================================================================
51 revisions.model.Slider = Backbone.Model.extend({
62 initialize: function( options ) {
63 this.frame = options.frame;
64 this.revisions = options.revisions;
66 // Listen for changes to the revisions or mode from outside
67 this.listenTo( this.frame, 'update:revisions', this.receiveRevisions );
68 this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode );
70 // Listen for internal changes
71 this.listenTo( this, 'change:from', this.handleLocalChanges );
72 this.listenTo( this, 'change:to', this.handleLocalChanges );
73 this.listenTo( this, 'change:compareTwoMode', this.updateSliderSettings );
74 this.listenTo( this, 'update:revisions', this.updateSliderSettings );
76 // Listen for changes to the hovered revision
77 this.listenTo( this, 'change:hoveredRevision', this.hoverRevision );
80 max: this.revisions.length - 1,
81 compareTwoMode: this.frame.get('compareTwoMode'),
82 from: this.frame.get('from'),
83 to: this.frame.get('to')
85 this.updateSliderSettings();
88 getSliderValue: function( a, b ) {
89 return isRtl ? this.revisions.length - this.revisions.indexOf( this.get(a) ) - 1 : this.revisions.indexOf( this.get(b) );
92 updateSliderSettings: function() {
93 if ( this.get('compareTwoMode') ) {
96 this.getSliderValue( 'to', 'from' ),
97 this.getSliderValue( 'from', 'to' )
100 range: true // ensures handles cannot cross
104 value: this.getSliderValue( 'to', 'to' ),
109 this.trigger( 'update:slider' );
112 // Called when a revision is hovered
113 hoverRevision: function( model, value ) {
114 this.trigger( 'hovered:revision', value );
117 // Called when `compareTwoMode` changes
118 updateMode: function( model, value ) {
119 this.set({ compareTwoMode: value });
122 // Called when `from` or `to` changes in the local model
123 handleLocalChanges: function() {
125 from: this.get('from'),
130 // Receives revisions changes from outside the model
131 receiveRevisions: function( from, to ) {
132 // Bail if nothing changed
133 if ( this.get('from') === from && this.get('to') === to )
136 this.set({ from: from, to: to }, { silent: true });
137 this.trigger( 'update:revisions', from, to );
142 revisions.model.Tooltip = Backbone.Model.extend({
146 hovering: false, // Whether the mouse is hovering
147 scrubbing: false // Whether the mouse is scrubbing
150 initialize: function( options ) {
151 this.frame = options.frame;
152 this.revisions = options.revisions;
153 this.slider = options.slider;
155 this.listenTo( this.slider, 'hovered:revision', this.updateRevision );
156 this.listenTo( this.slider, 'change:hovering', this.setHovering );
157 this.listenTo( this.slider, 'change:scrubbing', this.setScrubbing );
161 updateRevision: function( revision ) {
162 this.set({ revision: revision });
165 setHovering: function( model, value ) {
166 this.set({ hovering: value });
169 setScrubbing: function( model, value ) {
170 this.set({ scrubbing: value });
174 revisions.model.Revision = Backbone.Model.extend({});
176 revisions.model.Revisions = Backbone.Collection.extend({
177 model: revisions.model.Revision,
179 initialize: function() {
180 _.bindAll( this, 'next', 'prev' );
183 next: function( revision ) {
184 var index = this.indexOf( revision );
186 if ( index !== -1 && index !== this.length - 1 )
187 return this.at( index + 1 );
190 prev: function( revision ) {
191 var index = this.indexOf( revision );
193 if ( index !== -1 && index !== 0 )
194 return this.at( index - 1 );
198 revisions.model.Field = Backbone.Model.extend({});
200 revisions.model.Fields = Backbone.Collection.extend({
201 model: revisions.model.Field
204 revisions.model.Diff = Backbone.Model.extend({
205 initialize: function( attributes, options ) {
206 var fields = this.get('fields');
207 this.unset('fields');
209 this.fields = new revisions.model.Fields( fields );
213 revisions.model.Diffs = Backbone.Collection.extend({
214 initialize: function( models, options ) {
215 _.bindAll( this, 'getClosestUnloaded' );
216 this.loadAll = _.once( this._loadAll );
217 this.revisions = options.revisions;
221 model: revisions.model.Diff,
223 ensure: function( id, context ) {
224 var diff = this.get( id );
225 var request = this.requests[ id ];
226 var deferred = $.Deferred();
228 var from = id.split(':')[0];
229 var to = id.split(':')[1];
232 wp.revisions.log( 'ensure', id );
234 this.trigger( 'ensure', ids, from, to, deferred.promise() );
237 deferred.resolveWith( context, [ diff ] );
239 this.trigger( 'ensure:load', ids, from, to, deferred.promise() );
240 _.each( ids, _.bind( function( id ) {
241 // Remove anything that has an ongoing request
242 if ( this.requests[ id ] )
244 // Remove anything we already have
245 if ( this.get( id ) )
249 // Always include the ID that started this ensure
251 request = this.load( _.keys( ids ) );
254 request.done( _.bind( function() {
255 deferred.resolveWith( context, [ this.get( id ) ] );
256 }, this ) ).fail( _.bind( function() {
261 return deferred.promise();
264 // Returns an array of proximal diffs
265 getClosestUnloaded: function( ids, centerId ) {
267 return _.chain([0].concat( ids )).initial().zip( ids ).sortBy( function( pair ) {
268 return Math.abs( centerId - pair[1] );
269 }).map( function( pair ) {
270 return pair.join(':');
271 }).filter( function( diffId ) {
272 return _.isUndefined( self.get( diffId ) ) && ! self.requests[ diffId ];
276 _loadAll: function( allRevisionIds, centerId, num ) {
277 var self = this, deferred = $.Deferred();
278 diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
279 if ( _.size( diffs ) > 0 ) {
280 this.load( diffs ).done( function() {
281 self._loadAll( allRevisionIds, centerId, num ).done( function() {
284 }).fail( function() {
285 if ( 1 === num ) { // Already tried 1. This just isn't working. Give up.
287 } else { // Request fewer diffs this time
288 self._loadAll( allRevisionIds, centerId, Math.ceil( num / 2 ) ).done( function() {
299 load: function( comparisons ) {
300 wp.revisions.log( 'load', comparisons );
301 // Our collection should only ever grow, never shrink, so remove: false
302 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function(){
303 wp.revisions.log( 'load:complete', comparisons );
307 sync: function( method, model, options ) {
308 if ( 'read' === method ) {
309 options = options || {};
310 options.context = this;
311 options.data = _.extend( options.data || {}, {
312 action: 'get-revision-diffs',
313 post_id: revisions.settings.postId
316 var deferred = wp.ajax.send( options );
317 var requests = this.requests;
319 // Record that we're requesting each diff.
320 if ( options.data.compare ) {
321 _.each( options.data.compare, function( id ) {
322 requests[ id ] = deferred;
326 // When the request completes, clear the stored request.
327 deferred.always( function() {
328 if ( options.data.compare ) {
329 _.each( options.data.compare, function( id ) {
330 delete requests[ id ];
337 // Otherwise, fall back to `Backbone.sync()`.
339 return Backbone.Model.prototype.sync.apply( this, arguments );
345 revisions.model.FrameState = Backbone.Model.extend({
349 compareTwoMode: false
352 initialize: function( attributes, options ) {
355 _.bindAll( this, 'receiveDiff' );
356 this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
358 this.revisions = options.revisions;
359 this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions });
361 // Set the initial diffs collection provided through the settings
362 this.diffs.set( revisions.settings.diffData );
364 // Set up internal listeners
365 this.listenTo( this, 'change:from', this.changeRevisionHandler );
366 this.listenTo( this, 'change:to', this.changeRevisionHandler );
367 this.listenTo( this, 'change:compareTwoMode', this.changeMode );
368 this.listenTo( this, 'update:revisions', this.updatedRevisions );
369 this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus );
370 this.listenTo( this, 'update:diff', this.updateLoadingStatus );
372 // Set the initial revisions, baseUrl, and mode as provided through settings
373 properties.to = this.revisions.get( revisions.settings.to );
374 properties.from = this.revisions.get( revisions.settings.from );
375 properties.compareTwoMode = revisions.settings.compareTwoMode;
376 properties.baseUrl = revisions.settings.baseUrl;
377 this.set( properties );
379 // Start the router if browser supports History API
380 if ( window.history && window.history.pushState ) {
381 this.router = new revisions.Router({ model: this });
382 Backbone.history.start({ pushState: true });
386 updateLoadingStatus: function() {
387 this.set( 'error', false );
388 this.set( 'loading', ! this.diff() );
391 changeMode: function( model, value ) {
392 // If we were on the first revision before switching, we have to bump them over one
393 if ( value && 0 === this.revisions.indexOf( this.get('to') ) ) {
395 from: this.revisions.at(0),
396 to: this.revisions.at(1)
401 updatedRevisions: function( from, to ) {
402 if ( this.get( 'compareTwoMode' ) ) {
403 // TODO: compare-two loading strategy
405 this.diffs.loadAll( this.revisions.pluck('id'), to.id, 40 );
409 // Fetch the currently loaded diff.
411 return this.diffs.get( this._diffId );
414 // So long as `from` and `to` are changed at the same time, the diff
415 // will only be updated once. This is because Backbone updates all of
416 // the changed attributes in `set`, and then fires the `change` events.
417 updateDiff: function( options ) {
418 var from, to, diffId, diff;
420 options = options || {};
421 from = this.get('from');
423 diffId = ( from ? from.id : 0 ) + ':' + to.id;
425 // Check if we're actually changing the diff id.
426 if ( this._diffId === diffId )
427 return $.Deferred().reject().promise();
429 this._diffId = diffId;
430 this.trigger( 'update:revisions', from, to );
432 diff = this.diffs.get( diffId );
434 // If we already have the diff, then immediately trigger the update.
436 this.receiveDiff( diff );
437 return $.Deferred().resolve().promise();
438 // Otherwise, fetch the diff.
440 if ( options.immediate ) {
441 return this._ensureDiff();
443 this._debouncedEnsureDiff();
444 return $.Deferred().reject().promise();
449 // A simple wrapper around `updateDiff` to prevent the change event's
450 // parameters from being passed through.
451 changeRevisionHandler: function( model, value, options ) {
455 receiveDiff: function( diff ) {
456 // Did we actually get a diff?
457 if ( _.isUndefined( diff ) || _.isUndefined( diff.id ) ) {
462 } else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change
463 this.trigger( 'update:diff', diff );
467 _ensureDiff: function() {
468 return this.diffs.ensure( this._diffId, this ).always( this.receiveDiff );
474 * ========================================================================
476 * ========================================================================
479 // The frame view. This contains the entire page.
480 revisions.view.Frame = wp.Backbone.View.extend({
481 className: 'revisions',
482 template: wp.template('revisions-frame'),
484 initialize: function() {
485 this.listenTo( this.model, 'update:diff', this.renderDiff );
486 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
487 this.listenTo( this.model, 'change:loading', this.updateLoadingStatus );
488 this.listenTo( this.model, 'change:error', this.updateErrorStatus );
490 this.views.set( '.revisions-control-frame', new revisions.view.Controls({
496 wp.Backbone.View.prototype.render.apply( this, arguments );
498 $('html').css( 'overflow-y', 'scroll' );
499 $('#wpbody-content .wrap').append( this.el );
500 this.updateCompareTwoMode();
501 this.renderDiff( this.model.diff() );
507 renderDiff: function( diff ) {
508 this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
513 updateLoadingStatus: function() {
514 this.$el.toggleClass( 'loading', this.model.get('loading') );
517 updateErrorStatus: function() {
518 this.$el.toggleClass( 'diff-error', this.model.get('error') );
521 updateCompareTwoMode: function() {
522 this.$el.toggleClass( 'comparing-two-revisions', this.model.get('compareTwoMode') );
527 // This contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
528 revisions.view.Controls = wp.Backbone.View.extend({
529 className: 'revisions-controls',
531 initialize: function() {
532 _.bindAll( this, 'setWidth' );
534 // Add the button view
535 this.views.add( new revisions.view.Buttons({
539 // Add the checkbox view
540 this.views.add( new revisions.view.Checkbox({
544 // Prep the slider model
545 var slider = new revisions.model.Slider({
547 revisions: this.model.revisions
550 // Prep the tooltip model
551 var tooltip = new revisions.model.Tooltip({
553 revisions: this.model.revisions,
557 // Add the tooltip view
558 this.views.add( new revisions.view.Tooltip({
562 // Add the tickmarks view
563 this.views.add( new revisions.view.Tickmarks({
567 // Add the slider view
568 this.views.add( new revisions.view.Slider({
572 // Add the Metabox view
573 this.views.add( new revisions.view.Metabox({
579 this.top = this.$el.offset().top;
580 this.window = $(window);
581 this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) {
582 var controls = e.data.controls;
583 var container = controls.$el.parent();
584 var scrolled = controls.window.scrollTop();
585 var frame = controls.views.parent;
587 if ( scrolled >= controls.top ) {
588 if ( ! frame.$el.hasClass('pinned') ) {
590 container.css('height', container.height() + 'px' );
591 controls.window.on('resize.wp.revisions.pinning click.wp.revisions.pinning', {controls: controls}, function(e) {
592 e.data.controls.setWidth();
595 frame.$el.addClass('pinned');
596 } else if ( frame.$el.hasClass('pinned') ) {
597 controls.window.off('.wp.revisions.pinning');
598 controls.$el.css('width', 'auto');
599 frame.$el.removeClass('pinned');
600 container.css('height', 'auto');
601 controls.top = controls.$el.offset().top;
603 controls.top = controls.$el.offset().top;
608 setWidth: function() {
609 this.$el.css('width', this.$el.parent().width() + 'px');
613 // The tickmarks view
614 revisions.view.Tickmarks = wp.Backbone.View.extend({
615 className: 'revisions-tickmarks',
616 direction: isRtl ? 'right' : 'left',
618 initialize: function() {
619 this.listenTo( this.model, 'change:revision', this.reportTickPosition );
622 reportTickPosition: function( model, revision ) {
623 var offset, thisOffset, parentOffset, tick, index = this.model.revisions.indexOf( revision );
624 thisOffset = this.$el.allOffsets();
625 parentOffset = this.$el.parent().allOffsets();
626 if ( index === this.model.revisions.length - 1 ) {
629 rightPlusWidth: thisOffset.left - parentOffset.left + 1,
630 leftPlusWidth: thisOffset.right - parentOffset.right + 1
634 tick = this.$('div:nth-of-type(' + (index + 1) + ')');
635 offset = tick.allPositions();
637 left: offset.left + thisOffset.left - parentOffset.left,
638 right: offset.right + thisOffset.right - parentOffset.right
641 leftPlusWidth: offset.left + tick.outerWidth(),
642 rightPlusWidth: offset.right + tick.outerWidth()
645 this.model.set({ offset: offset });
649 var tickCount, tickWidth;
650 tickCount = this.model.revisions.length - 1;
651 tickWidth = 1 / tickCount;
652 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
654 _(tickCount).times( function( index ){
655 this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' );
661 revisions.view.Metabox = wp.Backbone.View.extend({
662 className: 'revisions-meta',
664 initialize: function() {
665 // Add the 'from' view
666 this.views.add( new revisions.view.MetaFrom({
668 className: 'diff-meta diff-meta-from'
672 this.views.add( new revisions.view.MetaTo({
678 // The revision meta view (to be extended)
679 revisions.view.Meta = wp.Backbone.View.extend({
680 template: wp.template('revisions-meta'),
683 'click .restore-revision': 'restoreRevision'
686 initialize: function() {
687 this.listenTo( this.model, 'update:revisions', this.render );
690 prepare: function() {
691 return _.extend( this.model.toJSON()[this.type] || {}, {
696 restoreRevision: function() {
697 document.location = this.model.get('to').attributes.restoreUrl;
701 // The revision meta 'from' view
702 revisions.view.MetaFrom = revisions.view.Meta.extend({
703 className: 'diff-meta diff-meta-from',
707 // The revision meta 'to' view
708 revisions.view.MetaTo = revisions.view.Meta.extend({
709 className: 'diff-meta diff-meta-to',
713 // The checkbox view.
714 revisions.view.Checkbox = wp.Backbone.View.extend({
715 className: 'revisions-checkbox',
716 template: wp.template('revisions-checkbox'),
719 'click .compare-two-revisions': 'compareTwoToggle'
722 initialize: function() {
723 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
727 if ( this.model.revisions.length < 3 )
728 $('.revision-toggle-compare-mode').hide();
731 updateCompareTwoMode: function() {
732 this.$('.compare-two-revisions').prop( 'checked', this.model.get('compareTwoMode') );
735 // Toggle the compare two mode feature when the compare two checkbox is checked.
736 compareTwoToggle: function( event ) {
737 // Activate compare two mode?
738 this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
743 // Encapsulates the tooltip.
744 revisions.view.Tooltip = wp.Backbone.View.extend({
745 className: 'revisions-tooltip',
746 template: wp.template('revisions-meta'),
748 initialize: function( options ) {
749 this.listenTo( this.model, 'change:offset', this.render );
750 this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
751 this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
754 prepare: function() {
755 if ( _.isNull( this.model.get('revision') ) )
758 return _.extend( { type: 'tooltip' }, {
759 attributes: this.model.get('revision').toJSON()
764 var direction, directionVal, flipped, css = {}, position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
765 flipped = ( position / this.model.revisions.length ) > 0.5;
767 direction = flipped ? 'left' : 'right';
768 directionVal = flipped ? 'leftPlusWidth' : direction;
770 direction = flipped ? 'right' : 'left';
771 directionVal = flipped ? 'rightPlusWidth' : direction;
773 otherDirection = 'right' === direction ? 'left': 'right';
774 wp.Backbone.View.prototype.render.apply( this, arguments );
775 css[direction] = this.model.get('offset')[directionVal] + 'px';
776 css[otherDirection] = '';
777 this.$el.toggleClass( 'flipped', flipped ).css( css );
780 visible: function() {
781 return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
784 toggleVisibility: function( options ) {
785 if ( this.visible() )
786 this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 );
788 this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } );
794 // Encapsulates all of the configuration for the previous/next buttons.
795 revisions.view.Buttons = wp.Backbone.View.extend({
796 className: 'revisions-buttons',
797 template: wp.template('revisions-buttons'),
800 'click .revisions-next .button': 'nextRevision',
801 'click .revisions-previous .button': 'previousRevision'
804 initialize: function() {
805 this.listenTo( this.model, 'update:revisions', this.disabledButtonCheck );
809 this.disabledButtonCheck();
812 // Go to a specific model index
813 gotoModel: function( toIndex ) {
815 to: this.model.revisions.at( toIndex )
817 // If we're at the first revision, unset 'from'.
819 attributes.from = this.model.revisions.at( toIndex - 1 );
821 this.model.unset('from', { silent: true });
823 this.model.set( attributes );
826 // Go to the 'next' revision
827 nextRevision: function() {
828 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) + 1;
829 this.gotoModel( toIndex );
832 // Go to the 'previous' revision
833 previousRevision: function() {
834 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) - 1;
835 this.gotoModel( toIndex );
838 // Check to see if the Previous or Next buttons need to be disabled or enabled.
839 disabledButtonCheck: function() {
840 var maxVal = this.model.revisions.length - 1,
842 next = $('.revisions-next .button'),
843 previous = $('.revisions-previous .button'),
844 val = this.model.revisions.indexOf( this.model.get('to') );
846 // Disable "Next" button if you're on the last node.
847 next.prop( 'disabled', ( maxVal === val ) );
849 // Disable "Previous" button if you're on the first node.
850 previous.prop( 'disabled', ( minVal === val ) );
856 revisions.view.Slider = wp.Backbone.View.extend({
857 className: 'wp-slider',
858 direction: isRtl ? 'right' : 'left',
861 'mousemove' : 'mouseMove'
864 initialize: function() {
865 _.bindAll( this, 'start', 'slide', 'stop', 'mouseMove', 'mouseEnter', 'mouseLeave' );
866 this.listenTo( this.model, 'update:slider', this.applySliderSettings );
870 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
871 this.$el.slider( _.extend( this.model.toJSON(), {
877 this.$el.hoverIntent({
878 over: this.mouseEnter,
879 out: this.mouseLeave,
883 this.applySliderSettings();
886 mouseMove: function( e ) {
887 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models
888 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider
889 sliderWidth = this.$el.width(), // Width of slider
890 tickWidth = sliderWidth / zoneCount, // Calculated width of zone
891 actualX = isRtl? $(window).width() - e.pageX : e.pageX; // Flipped for RTL - sliderFrom;
892 actualX = actualX - sliderFrom; // Offset of mouse position in slider
893 var currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index
895 // Ensure sane value for currentModelIndex.
896 if ( currentModelIndex < 0 )
897 currentModelIndex = 0;
898 else if ( currentModelIndex >= this.model.revisions.length )
899 currentModelIndex = this.model.revisions.length - 1;
901 // Update the tooltip mode
902 this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) });
905 mouseLeave: function() {
906 this.model.set({ hovering: false });
909 mouseEnter: function() {
910 this.model.set({ hovering: true });
913 applySliderSettings: function() {
914 this.$el.slider( _.pick( this.model.toJSON(), 'value', 'values', 'range' ) );
915 var handles = this.$('a.ui-slider-handle');
917 if ( this.model.get('compareTwoMode') ) {
918 // in RTL mode the 'left handle' is the second in the slider, 'right' is first
920 .toggleClass( 'to-handle', !! isRtl )
921 .toggleClass( 'from-handle', ! isRtl );
923 .toggleClass( 'from-handle', !! isRtl )
924 .toggleClass( 'to-handle', ! isRtl );
926 handles.removeClass('from-handle to-handle');
930 start: function( event, ui ) {
931 this.model.set({ scrubbing: true });
933 // Track the mouse position to enable smooth dragging,
934 // overrides default jQuery UI step behavior.
935 $( window ).on( 'mousemove.wp.revisions', { view: this }, function( e ) {
936 var view = e.data.view,
937 leftDragBoundary = view.$el.offset().left,
938 sliderOffset = leftDragBoundary,
939 sliderRightEdge = leftDragBoundary + view.$el.width(),
940 rightDragBoundary = sliderRightEdge,
942 rightDragReset = '100%',
943 handle = $( ui.handle );
945 // In two handle mode, ensure handles can't be dragged past each other.
946 // Adjust left/right boundaries and reset points.
947 if ( view.model.get('compareTwoMode') ) {
948 var handles = handle.parent().find('.ui-slider-handle');
949 if ( handle.is( handles.first() ) ) { // We're the left handle
950 rightDragBoundary = handles.last().offset().left;
951 rightDragReset = rightDragBoundary - sliderOffset;
952 } else { // We're the right handle
953 leftDragBoundary = handles.first().offset().left + handles.first().width();
954 leftDragReset = leftDragBoundary - sliderOffset;
958 // Follow mouse movements, as long as handle remains inside slider.
959 if ( e.pageX < leftDragBoundary ) {
960 handle.css( 'left', leftDragReset ); // Mouse to left of slider.
961 } else if ( e.pageX > rightDragBoundary ) {
962 handle.css( 'left', rightDragReset ); // Mouse to right of slider.
964 handle.css( 'left', e.pageX - sliderOffset ); // Mouse in slider.
969 getPosition: function( position ) {
970 return isRtl ? this.model.revisions.length - position - 1: position;
973 // Responds to slide events
974 slide: function( event, ui ) {
975 var attributes, movedRevision;
976 // Compare two revisions mode
977 if ( this.model.get('compareTwoMode') ) {
978 // Prevent sliders from occupying same spot
979 if ( ui.values[1] === ui.values[0] )
984 from: this.model.revisions.at( this.getPosition( ui.values[0] ) ),
985 to: this.model.revisions.at( this.getPosition( ui.values[1] ) )
989 to: this.model.revisions.at( this.getPosition( ui.value ) )
991 // If we're at the first revision, unset 'from'.
992 if ( this.getPosition( ui.value ) > 0 )
993 attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 );
995 attributes.from = undefined;
997 movedRevision = this.model.revisions.at( this.getPosition( ui.value ) );
999 // If we are scrubbing, a scrub to a revision is considered a hover
1000 if ( this.model.get('scrubbing') )
1001 attributes.hoveredRevision = movedRevision;
1003 this.model.set( attributes );
1006 stop: function( event, ui ) {
1007 $( window ).off('mousemove.wp.revisions');
1008 this.model.updateSliderSettings(); // To snap us back to a tick mark
1009 this.model.set({ scrubbing: false });
1014 // This is the view for the current active diff.
1015 revisions.view.Diff = wp.Backbone.View.extend({
1016 className: 'revisions-diff',
1017 template: wp.template('revisions-diff'),
1019 // Generate the options to be passed to the template.
1020 prepare: function() {
1021 return _.extend({ fields: this.model.fields.toJSON() }, this.options );
1025 // The revisions router
1026 // takes URLs with #hash fragments and routes them
1027 revisions.Router = Backbone.Router.extend({
1028 initialize: function( options ) {
1029 this.model = options.model;
1030 this.routes = _.object([
1031 [ this.baseUrl( '?from=:from&to=:to' ), 'handleRoute' ],
1032 [ this.baseUrl( '?from=:from&to=:to' ), 'handleRoute' ]
1034 // Maintain state and history when navigating
1035 this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
1036 this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl );
1039 baseUrl: function( url ) {
1040 return this.model.get('baseUrl') + url;
1043 updateUrl: function() {
1044 var from = this.model.has('from') ? this.model.get('from').id : 0;
1045 var to = this.model.get('to').id;
1046 if ( this.model.get('compareTwoMode' ) )
1047 this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) );
1049 this.navigate( this.baseUrl( '?revision=' + to ) );
1052 handleRoute: function( a, b ) {
1053 var from, to, compareTwo = _.isUndefined( b );
1055 if ( ! compareTwo ) {
1056 b = this.model.revisions.get( a );
1057 a = this.model.revisions.prev( b );
1063 from: this.model.revisions.get( parseInt( a, 10 ) ),
1064 to: this.model.revisions.get( parseInt( a, 10 ) ),
1065 compareTwoMode: compareTwo
1070 // Initialize the revisions UI.
1071 revisions.init = function() {
1072 revisions.view.frame = new revisions.view.Frame({
1073 model: new revisions.model.FrameState({}, {
1074 revisions: new revisions.model.Revisions( revisions.settings.revisionData )
1079 $( revisions.init );