2014-10-31 20:20:22 -04:00
|
|
|
|
/* global isRtl */
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* @file Revisions interface functions, Backbone classes and
|
|
|
|
|
* the revisions.php document.ready bootstrap.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2013-02-28 10:14:34 -05:00
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
|
|
(function($) {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
var revisions;
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* Expose the module in window.wp.revisions.
|
|
|
|
|
*/
|
2013-07-09 04:15:55 -04:00
|
|
|
|
revisions = wp.revisions = { model: {}, view: {}, controller: {} };
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2014-10-31 20:20:22 -04:00
|
|
|
|
// Link post revisions data served from the back-end.
|
|
|
|
|
revisions.settings = window._wpRevisionsSettings || {};
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-07-09 15:00:11 -04:00
|
|
|
|
// For debugging
|
2013-07-12 22:53:59 -04:00
|
|
|
|
revisions.debug = false;
|
2013-07-09 15:00:11 -04:00
|
|
|
|
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* wp.revisions.log
|
|
|
|
|
*
|
|
|
|
|
* A debugging utility for revisions. Works only when a
|
|
|
|
|
* debug flag is on and the browser supports it.
|
|
|
|
|
*/
|
2013-07-09 15:00:11 -04:00
|
|
|
|
revisions.log = function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( window.console && revisions.debug ) {
|
|
|
|
|
window.console.log.apply( window.console, arguments );
|
|
|
|
|
}
|
2013-07-09 15:21:25 -04:00
|
|
|
|
};
|
2013-07-09 15:00:11 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Handy functions to help with positioning
|
|
|
|
|
$.fn.allOffsets = function() {
|
|
|
|
|
var offset = this.offset() || {top: 0, left: 0}, win = $(window);
|
|
|
|
|
return _.extend( offset, {
|
|
|
|
|
right: win.width() - offset.left - this.outerWidth(),
|
|
|
|
|
bottom: win.height() - offset.top - this.outerHeight()
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$.fn.allPositions = function() {
|
|
|
|
|
var position = this.position() || {top: 0, left: 0}, parent = this.parent();
|
|
|
|
|
return _.extend( position, {
|
|
|
|
|
right: parent.outerWidth() - position.left - this.outerWidth(),
|
|
|
|
|
bottom: parent.outerHeight() - position.top - this.outerHeight()
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-04 03:53:49 -04:00
|
|
|
|
/**
|
|
|
|
|
* ========================================================================
|
2013-06-26 17:06:50 -04:00
|
|
|
|
* MODELS
|
2013-04-04 03:53:49 -04:00
|
|
|
|
* ========================================================================
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Slider = Backbone.Model.extend({
|
|
|
|
|
defaults: {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
value: null,
|
|
|
|
|
values: null,
|
2013-06-26 17:06:50 -04:00
|
|
|
|
min: 0,
|
|
|
|
|
max: 1,
|
2013-07-11 05:14:14 -04:00
|
|
|
|
step: 1,
|
2013-07-12 10:01:39 -04:00
|
|
|
|
range: false,
|
2013-07-11 05:14:14 -04:00
|
|
|
|
compareTwoMode: false
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
|
this.frame = options.frame;
|
|
|
|
|
this.revisions = options.revisions;
|
|
|
|
|
|
|
|
|
|
// Listen for changes to the revisions or mode from outside
|
|
|
|
|
this.listenTo( this.frame, 'update:revisions', this.receiveRevisions );
|
|
|
|
|
this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode );
|
|
|
|
|
|
|
|
|
|
// Listen for internal changes
|
2014-10-31 20:39:24 -04:00
|
|
|
|
this.on( 'change:from', this.handleLocalChanges );
|
|
|
|
|
this.on( 'change:to', this.handleLocalChanges );
|
|
|
|
|
this.on( 'change:compareTwoMode', this.updateSliderSettings );
|
|
|
|
|
this.on( 'update:revisions', this.updateSliderSettings );
|
2013-07-11 05:14:14 -04:00
|
|
|
|
|
|
|
|
|
// Listen for changes to the hovered revision
|
2014-10-31 20:39:24 -04:00
|
|
|
|
this.on( 'change:hoveredRevision', this.hoverRevision );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
|
|
|
|
|
this.set({
|
|
|
|
|
max: this.revisions.length - 1,
|
|
|
|
|
compareTwoMode: this.frame.get('compareTwoMode'),
|
|
|
|
|
from: this.frame.get('from'),
|
|
|
|
|
to: this.frame.get('to')
|
|
|
|
|
});
|
|
|
|
|
this.updateSliderSettings();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getSliderValue: function( a, b ) {
|
|
|
|
|
return isRtl ? this.revisions.length - this.revisions.indexOf( this.get(a) ) - 1 : this.revisions.indexOf( this.get(b) );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateSliderSettings: function() {
|
|
|
|
|
if ( this.get('compareTwoMode') ) {
|
|
|
|
|
this.set({
|
|
|
|
|
values: [
|
|
|
|
|
this.getSliderValue( 'to', 'from' ),
|
|
|
|
|
this.getSliderValue( 'from', 'to' )
|
|
|
|
|
],
|
|
|
|
|
value: null,
|
|
|
|
|
range: true // ensures handles cannot cross
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.set({
|
|
|
|
|
value: this.getSliderValue( 'to', 'to' ),
|
|
|
|
|
values: null,
|
|
|
|
|
range: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
this.trigger( 'update:slider' );
|
2013-07-11 05:14:14 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Called when a revision is hovered
|
|
|
|
|
hoverRevision: function( model, value ) {
|
|
|
|
|
this.trigger( 'hovered:revision', value );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Called when `compareTwoMode` changes
|
|
|
|
|
updateMode: function( model, value ) {
|
|
|
|
|
this.set({ compareTwoMode: value });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Called when `from` or `to` changes in the local model
|
|
|
|
|
handleLocalChanges: function() {
|
|
|
|
|
this.frame.set({
|
|
|
|
|
from: this.get('from'),
|
|
|
|
|
to: this.get('to')
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Receives revisions changes from outside the model
|
|
|
|
|
receiveRevisions: function( from, to ) {
|
|
|
|
|
// Bail if nothing changed
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.get('from') === from && this.get('to') === to ) {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
return;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-11 05:14:14 -04:00
|
|
|
|
|
|
|
|
|
this.set({ from: from, to: to }, { silent: true });
|
|
|
|
|
this.trigger( 'update:revisions', from, to );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}
|
2013-07-11 05:14:14 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-07-05 13:44:47 -04:00
|
|
|
|
revisions.model.Tooltip = Backbone.Model.extend({
|
|
|
|
|
defaults: {
|
|
|
|
|
revision: null,
|
2013-07-19 09:49:15 -04:00
|
|
|
|
offset: {},
|
2013-07-11 05:14:14 -04:00
|
|
|
|
hovering: false, // Whether the mouse is hovering
|
|
|
|
|
scrubbing: false // Whether the mouse is scrubbing
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
this.frame = options.frame;
|
2013-07-11 05:14:14 -04:00
|
|
|
|
this.revisions = options.revisions;
|
|
|
|
|
this.slider = options.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 );
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
updateRevision: function( revision ) {
|
|
|
|
|
this.set({ revision: revision });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setHovering: function( model, value ) {
|
|
|
|
|
this.set({ hovering: value });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setScrubbing: function( model, value ) {
|
|
|
|
|
this.set({ scrubbing: value });
|
2013-07-05 13:44:47 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Revision = Backbone.Model.extend({});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* wp.revisions.model.Revisions
|
|
|
|
|
*
|
|
|
|
|
* A collection of post revisions.
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Revisions = Backbone.Collection.extend({
|
|
|
|
|
model: revisions.model.Revision,
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
initialize: function() {
|
|
|
|
|
_.bindAll( this, 'next', 'prev' );
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 05:57:58 -04:00
|
|
|
|
next: function( revision ) {
|
|
|
|
|
var index = this.indexOf( revision );
|
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( index !== -1 && index !== this.length - 1 ) {
|
2013-07-09 05:57:58 -04:00
|
|
|
|
return this.at( index + 1 );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-09 05:57:58 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
prev: function( revision ) {
|
|
|
|
|
var index = this.indexOf( revision );
|
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( index !== -1 && index !== 0 ) {
|
2013-07-09 05:57:58 -04:00
|
|
|
|
return this.at( index - 1 );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Field = Backbone.Model.extend({});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Fields = Backbone.Collection.extend({
|
|
|
|
|
model: revisions.model.Field
|
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Diff = Backbone.Model.extend({
|
2013-11-07 13:14:10 -05:00
|
|
|
|
initialize: function() {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
var fields = this.get('fields');
|
|
|
|
|
this.unset('fields');
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.fields = new revisions.model.Fields( fields );
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.Diffs = Backbone.Collection.extend({
|
2013-07-09 03:33:15 -04:00
|
|
|
|
initialize: function( models, options ) {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
_.bindAll( this, 'getClosestUnloaded' );
|
|
|
|
|
this.loadAll = _.once( this._loadAll );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.revisions = options.revisions;
|
2014-10-31 20:39:24 -04:00
|
|
|
|
this.postId = options.postId;
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.requests = {};
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
model: revisions.model.Diff,
|
|
|
|
|
|
|
|
|
|
ensure: function( id, context ) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var diff = this.get( id ),
|
|
|
|
|
request = this.requests[ id ],
|
|
|
|
|
deferred = $.Deferred(),
|
|
|
|
|
ids = {},
|
|
|
|
|
from = id.split(':')[0],
|
|
|
|
|
to = id.split(':')[1];
|
2013-07-12 10:01:39 -04:00
|
|
|
|
ids[id] = true;
|
|
|
|
|
|
|
|
|
|
wp.revisions.log( 'ensure', id );
|
|
|
|
|
|
|
|
|
|
this.trigger( 'ensure', ids, from, to, deferred.promise() );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
|
|
|
|
if ( diff ) {
|
|
|
|
|
deferred.resolveWith( context, [ diff ] );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
} else {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.trigger( 'ensure:load', ids, from, to, deferred.promise() );
|
|
|
|
|
_.each( ids, _.bind( function( id ) {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Remove anything that has an ongoing request
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.requests[ id ] ) {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
delete ids[ id ];
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-12 10:01:39 -04:00
|
|
|
|
// Remove anything we already have
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.get( id ) ) {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
delete ids[ id ];
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}, this ) );
|
|
|
|
|
if ( ! request ) {
|
|
|
|
|
// Always include the ID that started this ensure
|
|
|
|
|
ids[ id ] = true;
|
|
|
|
|
request = this.load( _.keys( ids ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.done( _.bind( function() {
|
|
|
|
|
deferred.resolveWith( context, [ this.get( id ) ] );
|
2013-07-18 12:35:19 -04:00
|
|
|
|
}, this ) ).fail( _.bind( function() {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
}) );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
|
|
|
|
return deferred.promise();
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
// Returns an array of proximal diffs
|
|
|
|
|
getClosestUnloaded: function( ids, centerId ) {
|
2013-07-09 15:00:11 -04:00
|
|
|
|
var self = this;
|
2013-07-12 10:01:39 -04:00
|
|
|
|
return _.chain([0].concat( ids )).initial().zip( ids ).sortBy( function( pair ) {
|
|
|
|
|
return Math.abs( centerId - pair[1] );
|
|
|
|
|
}).map( function( pair ) {
|
|
|
|
|
return pair.join(':');
|
|
|
|
|
}).filter( function( diffId ) {
|
|
|
|
|
return _.isUndefined( self.get( diffId ) ) && ! self.requests[ diffId ];
|
|
|
|
|
}).value();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_loadAll: function( allRevisionIds, centerId, num ) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var self = this, deferred = $.Deferred(),
|
|
|
|
|
diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
if ( _.size( diffs ) > 0 ) {
|
|
|
|
|
this.load( diffs ).done( function() {
|
2013-07-18 12:35:19 -04:00
|
|
|
|
self._loadAll( allRevisionIds, centerId, num ).done( function() {
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
}).fail( function() {
|
|
|
|
|
if ( 1 === num ) { // Already tried 1. This just isn't working. Give up.
|
|
|
|
|
deferred.reject();
|
|
|
|
|
} else { // Request fewer diffs this time
|
|
|
|
|
self._loadAll( allRevisionIds, centerId, Math.ceil( num / 2 ) ).done( function() {
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-07-12 10:01:39 -04:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2013-07-18 12:35:19 -04:00
|
|
|
|
deferred.resolve();
|
2013-07-12 10:01:39 -04:00
|
|
|
|
}
|
2013-07-18 12:35:19 -04:00
|
|
|
|
return deferred;
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
load: function( comparisons ) {
|
2013-07-09 15:00:11 -04:00
|
|
|
|
wp.revisions.log( 'load', comparisons );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Our collection should only ever grow, never shrink, so remove: false
|
2013-11-07 13:14:10 -05:00
|
|
|
|
return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
wp.revisions.log( 'load:complete', comparisons );
|
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
sync: function( method, model, options ) {
|
|
|
|
|
if ( 'read' === method ) {
|
2013-07-09 03:33:15 -04:00
|
|
|
|
options = options || {};
|
2013-06-26 17:06:50 -04:00
|
|
|
|
options.context = this;
|
2013-07-09 03:33:15 -04:00
|
|
|
|
options.data = _.extend( options.data || {}, {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
action: 'get-revision-diffs',
|
2014-10-31 20:39:24 -04:00
|
|
|
|
post_id: this.postId
|
2013-06-26 17:06:50 -04:00
|
|
|
|
});
|
2013-04-17 16:12:25 -04:00
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var deferred = wp.ajax.send( options ),
|
|
|
|
|
requests = this.requests;
|
2013-04-17 16:12:25 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Record that we're requesting each diff.
|
|
|
|
|
if ( options.data.compare ) {
|
|
|
|
|
_.each( options.data.compare, function( id ) {
|
|
|
|
|
requests[ id ] = deferred;
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-04-17 16:12:25 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// When the request completes, clear the stored request.
|
|
|
|
|
deferred.always( function() {
|
|
|
|
|
if ( options.data.compare ) {
|
|
|
|
|
_.each( options.data.compare, function( id ) {
|
|
|
|
|
delete requests[ id ];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
return deferred;
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Otherwise, fall back to `Backbone.sync()`.
|
|
|
|
|
} else {
|
|
|
|
|
return Backbone.Model.prototype.sync.apply( this, arguments );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-07 10:32:26 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* wp.revisions.model.FrameState
|
|
|
|
|
*
|
|
|
|
|
* The frame state.
|
|
|
|
|
*
|
|
|
|
|
* @see wp.revisions.view.Frame
|
|
|
|
|
*
|
|
|
|
|
* @param {object} attributes Model attributes - none are required.
|
|
|
|
|
* @param {object} options Options for the model.
|
|
|
|
|
* @param {revisions.model.Revisions} options.revisions A collection of revisions.
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.model.FrameState = Backbone.Model.extend({
|
2013-07-11 18:56:48 -04:00
|
|
|
|
defaults: {
|
2013-07-12 01:11:50 -04:00
|
|
|
|
loading: false,
|
2013-07-18 12:35:19 -04:00
|
|
|
|
error: false,
|
2013-07-11 18:56:48 -04:00
|
|
|
|
compareTwoMode: false
|
|
|
|
|
},
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
initialize: function( attributes, options ) {
|
2014-10-31 20:39:24 -04:00
|
|
|
|
var state = this.get( 'initialDiffState' );
|
2013-07-18 12:35:19 -04:00
|
|
|
|
_.bindAll( this, 'receiveDiff' );
|
2013-07-09 05:57:58 -04:00
|
|
|
|
this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.revisions = options.revisions;
|
2013-07-09 05:57:58 -04:00
|
|
|
|
|
2014-10-31 20:39:24 -04:00
|
|
|
|
this.diffs = new revisions.model.Diffs( [], {
|
|
|
|
|
revisions: this.revisions,
|
|
|
|
|
postId: this.get( 'postId' )
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
// Set the initial diffs collection.
|
|
|
|
|
this.diffs.set( this.get( 'diffData' ) );
|
2013-07-11 18:56:48 -04:00
|
|
|
|
|
2013-07-12 01:11:56 -04:00
|
|
|
|
// Set up internal listeners
|
|
|
|
|
this.listenTo( this, 'change:from', this.changeRevisionHandler );
|
|
|
|
|
this.listenTo( this, 'change:to', this.changeRevisionHandler );
|
2013-07-12 22:49:53 -04:00
|
|
|
|
this.listenTo( this, 'change:compareTwoMode', this.changeMode );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.listenTo( this, 'update:revisions', this.updatedRevisions );
|
2013-07-12 01:11:56 -04:00
|
|
|
|
this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus );
|
|
|
|
|
this.listenTo( this, 'update:diff', this.updateLoadingStatus );
|
|
|
|
|
|
2014-10-31 20:39:24 -04:00
|
|
|
|
// Set the initial revisions, baseUrl, and mode as provided through attributes.
|
|
|
|
|
|
|
|
|
|
this.set( {
|
|
|
|
|
to : this.revisions.get( state.to ),
|
|
|
|
|
from : this.revisions.get( state.from ),
|
|
|
|
|
compareTwoMode : this.revisions.get( state.compareTwoMode )
|
|
|
|
|
} );
|
2013-07-09 05:57:58 -04:00
|
|
|
|
|
2013-07-12 16:51:10 -04:00
|
|
|
|
// Start the router if browser supports History API
|
|
|
|
|
if ( window.history && window.history.pushState ) {
|
|
|
|
|
this.router = new revisions.Router({ model: this });
|
|
|
|
|
Backbone.history.start({ pushState: true });
|
|
|
|
|
}
|
2013-07-09 05:57:58 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 01:11:56 -04:00
|
|
|
|
updateLoadingStatus: function() {
|
2013-07-18 12:35:19 -04:00
|
|
|
|
this.set( 'error', false );
|
2013-07-12 01:11:56 -04:00
|
|
|
|
this.set( 'loading', ! this.diff() );
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 22:49:53 -04:00
|
|
|
|
changeMode: function( model, value ) {
|
2014-07-09 21:00:14 -04:00
|
|
|
|
var toIndex = this.revisions.indexOf( this.get( 'to' ) );
|
|
|
|
|
|
|
|
|
|
// If we were on the first revision before switching to two-handled mode,
|
|
|
|
|
// bump the 'to' position over one
|
|
|
|
|
if ( value && 0 === toIndex ) {
|
|
|
|
|
this.set({
|
|
|
|
|
from: this.revisions.at( toIndex ),
|
|
|
|
|
to: this.revisions.at( toIndex + 1 )
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When switching back to single-handled mode, reset 'from' model to
|
|
|
|
|
// one position before the 'to' model
|
|
|
|
|
if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode
|
2013-07-12 22:49:53 -04:00
|
|
|
|
this.set({
|
2014-07-09 21:00:14 -04:00
|
|
|
|
from: this.revisions.at( toIndex - 1 ),
|
|
|
|
|
to: this.revisions.at( toIndex )
|
2013-07-12 22:49:53 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
updatedRevisions: function( from, to ) {
|
2013-07-09 15:00:11 -04:00
|
|
|
|
if ( this.get( 'compareTwoMode' ) ) {
|
|
|
|
|
// TODO: compare-two loading strategy
|
|
|
|
|
} else {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.diffs.loadAll( this.revisions.pluck('id'), to.id, 40 );
|
2013-07-09 15:00:11 -04:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 05:57:58 -04:00
|
|
|
|
// Fetch the currently loaded diff.
|
|
|
|
|
diff: function() {
|
|
|
|
|
return this.diffs.get( this._diffId );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 03:07:53 -04:00
|
|
|
|
// So long as `from` and `to` are changed at the same time, the diff
|
|
|
|
|
// will only be updated once. This is because Backbone updates all of
|
|
|
|
|
// the changed attributes in `set`, and then fires the `change` events.
|
2013-07-09 05:57:58 -04:00
|
|
|
|
updateDiff: function( options ) {
|
|
|
|
|
var from, to, diffId, diff;
|
2013-07-09 04:50:32 -04:00
|
|
|
|
|
2013-07-09 05:57:58 -04:00
|
|
|
|
options = options || {};
|
2013-07-09 04:50:32 -04:00
|
|
|
|
from = this.get('from');
|
|
|
|
|
to = this.get('to');
|
|
|
|
|
diffId = ( from ? from.id : 0 ) + ':' + to.id;
|
|
|
|
|
|
|
|
|
|
// Check if we're actually changing the diff id.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this._diffId === diffId ) {
|
2013-07-12 01:11:50 -04:00
|
|
|
|
return $.Deferred().reject().promise();
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-09 04:50:32 -04:00
|
|
|
|
|
|
|
|
|
this._diffId = diffId;
|
|
|
|
|
this.trigger( 'update:revisions', from, to );
|
|
|
|
|
|
2013-07-09 05:57:58 -04:00
|
|
|
|
diff = this.diffs.get( diffId );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
|
|
|
|
|
// If we already have the diff, then immediately trigger the update.
|
2013-07-09 05:57:58 -04:00
|
|
|
|
if ( diff ) {
|
2013-07-18 12:35:19 -04:00
|
|
|
|
this.receiveDiff( diff );
|
2013-07-09 23:20:58 -04:00
|
|
|
|
return $.Deferred().resolve().promise();
|
2013-07-09 05:57:58 -04:00
|
|
|
|
// Otherwise, fetch the diff.
|
|
|
|
|
} else {
|
2013-07-09 23:20:58 -04:00
|
|
|
|
if ( options.immediate ) {
|
|
|
|
|
return this._ensureDiff();
|
|
|
|
|
} else {
|
2013-07-09 05:57:58 -04:00
|
|
|
|
this._debouncedEnsureDiff();
|
2013-07-12 01:11:50 -04:00
|
|
|
|
return $.Deferred().reject().promise();
|
2013-07-09 23:20:58 -04:00
|
|
|
|
}
|
2013-07-09 05:57:58 -04:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// A simple wrapper around `updateDiff` to prevent the change event's
|
|
|
|
|
// parameters from being passed through.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
changeRevisionHandler: function() {
|
2013-07-09 05:57:58 -04:00
|
|
|
|
this.updateDiff();
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-18 12:35:19 -04:00
|
|
|
|
receiveDiff: function( diff ) {
|
|
|
|
|
// Did we actually get a diff?
|
|
|
|
|
if ( _.isUndefined( diff ) || _.isUndefined( diff.id ) ) {
|
|
|
|
|
this.set({
|
|
|
|
|
loading: false,
|
|
|
|
|
error: true
|
|
|
|
|
});
|
|
|
|
|
} else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change
|
|
|
|
|
this.trigger( 'update:diff', diff );
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 05:57:58 -04:00
|
|
|
|
_ensureDiff: function() {
|
2013-07-18 12:35:19 -04:00
|
|
|
|
return this.diffs.ensure( this._diffId, this ).always( this.receiveDiff );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
|
|
|
|
|
2013-04-04 03:53:49 -04:00
|
|
|
|
/**
|
|
|
|
|
* ========================================================================
|
|
|
|
|
* VIEWS
|
|
|
|
|
* ========================================================================
|
|
|
|
|
*/
|
|
|
|
|
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* wp.revisions.view.Frame
|
|
|
|
|
*
|
|
|
|
|
* Top level frame that orchestrates the revisions experience.
|
|
|
|
|
*
|
|
|
|
|
* @param {object} options The options hash for the view.
|
|
|
|
|
* @param {revisions.model.FrameState} options.model The frame state model.
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.view.Frame = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions',
|
|
|
|
|
template: wp.template('revisions-frame'),
|
|
|
|
|
|
|
|
|
|
initialize: function() {
|
2013-07-09 04:50:32 -04:00
|
|
|
|
this.listenTo( this.model, 'update:diff', this.renderDiff );
|
2013-07-06 16:22:02 -04:00
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
|
2013-07-12 01:11:56 -04:00
|
|
|
|
this.listenTo( this.model, 'change:loading', this.updateLoadingStatus );
|
2013-07-18 12:35:19 -04:00
|
|
|
|
this.listenTo( this.model, 'change:error', this.updateErrorStatus );
|
2013-07-06 16:22:02 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.views.set( '.revisions-control-frame', new revisions.view.Controls({
|
|
|
|
|
model: this.model
|
|
|
|
|
}) );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
render: function() {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
wp.Backbone.View.prototype.render.apply( this, arguments );
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
$('html').css( 'overflow-y', 'scroll' );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
$('#wpbody-content .wrap').append( this.el );
|
|
|
|
|
this.updateCompareTwoMode();
|
|
|
|
|
this.renderDiff( this.model.diff() );
|
|
|
|
|
this.views.ready();
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
return this;
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 04:50:32 -04:00
|
|
|
|
renderDiff: function( diff ) {
|
|
|
|
|
this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
|
|
|
|
|
model: diff
|
|
|
|
|
}) );
|
2013-07-06 16:22:02 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 01:11:56 -04:00
|
|
|
|
updateLoadingStatus: function() {
|
|
|
|
|
this.$el.toggleClass( 'loading', this.model.get('loading') );
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-18 12:35:19 -04:00
|
|
|
|
updateErrorStatus: function() {
|
|
|
|
|
this.$el.toggleClass( 'diff-error', this.model.get('error') );
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-06 16:22:02 -04:00
|
|
|
|
updateCompareTwoMode: function() {
|
2013-07-09 03:11:43 -04:00
|
|
|
|
this.$el.toggleClass( 'comparing-two-revisions', this.model.get('compareTwoMode') );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2014-10-31 19:55:22 -04:00
|
|
|
|
/**
|
|
|
|
|
* wp.revisions.view.Controls
|
|
|
|
|
*
|
|
|
|
|
* The controls view.
|
|
|
|
|
*
|
|
|
|
|
* Contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.view.Controls = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-controls',
|
|
|
|
|
|
|
|
|
|
initialize: function() {
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
_.bindAll( this, 'setWidth' );
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Add the button view
|
2013-07-03 16:27:19 -04:00
|
|
|
|
this.views.add( new revisions.view.Buttons({
|
2013-06-26 17:06:50 -04:00
|
|
|
|
model: this.model
|
2013-07-09 03:33:15 -04:00
|
|
|
|
}) );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Add the checkbox view
|
|
|
|
|
this.views.add( new revisions.view.Checkbox({
|
|
|
|
|
model: this.model
|
2013-07-09 03:33:15 -04:00
|
|
|
|
}) );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
// Prep the slider model
|
|
|
|
|
var slider = new revisions.model.Slider({
|
|
|
|
|
frame: this.model,
|
|
|
|
|
revisions: this.model.revisions
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}),
|
2013-07-05 13:44:47 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Prep the tooltip model
|
2013-11-07 13:14:10 -05:00
|
|
|
|
tooltip = new revisions.model.Tooltip({
|
2013-07-19 09:49:15 -04:00
|
|
|
|
frame: this.model,
|
|
|
|
|
revisions: this.model.revisions,
|
|
|
|
|
slider: slider
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
// Add the tooltip view
|
|
|
|
|
this.views.add( new revisions.view.Tooltip({
|
2013-07-19 09:49:15 -04:00
|
|
|
|
model: tooltip
|
2013-07-11 05:14:14 -04:00
|
|
|
|
}) );
|
|
|
|
|
|
|
|
|
|
// Add the tickmarks view
|
2013-07-08 17:37:03 -04:00
|
|
|
|
this.views.add( new revisions.view.Tickmarks({
|
2013-07-19 09:49:15 -04:00
|
|
|
|
model: tooltip
|
2013-07-09 03:33:15 -04:00
|
|
|
|
}) );
|
2013-07-08 17:37:03 -04:00
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
// Add the slider view
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.views.add( new revisions.view.Slider({
|
2013-07-11 05:14:14 -04:00
|
|
|
|
model: slider
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}) );
|
|
|
|
|
|
2013-07-28 16:15:28 -04:00
|
|
|
|
// Add the Metabox view
|
|
|
|
|
this.views.add( new revisions.view.Metabox({
|
2013-06-26 17:06:50 -04:00
|
|
|
|
model: this.model
|
|
|
|
|
}) );
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
},
|
2013-07-08 17:37:03 -04:00
|
|
|
|
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
ready: function() {
|
|
|
|
|
this.top = this.$el.offset().top;
|
|
|
|
|
this.window = $(window);
|
|
|
|
|
this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var controls = e.data.controls,
|
|
|
|
|
container = controls.$el.parent(),
|
|
|
|
|
scrolled = controls.window.scrollTop(),
|
|
|
|
|
frame = controls.views.parent;
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
|
|
|
|
|
if ( scrolled >= controls.top ) {
|
|
|
|
|
if ( ! frame.$el.hasClass('pinned') ) {
|
|
|
|
|
controls.setWidth();
|
|
|
|
|
container.css('height', container.height() + 'px' );
|
|
|
|
|
controls.window.on('resize.wp.revisions.pinning click.wp.revisions.pinning', {controls: controls}, function(e) {
|
|
|
|
|
e.data.controls.setWidth();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
frame.$el.addClass('pinned');
|
|
|
|
|
} else if ( frame.$el.hasClass('pinned') ) {
|
|
|
|
|
controls.window.off('.wp.revisions.pinning');
|
|
|
|
|
controls.$el.css('width', 'auto');
|
|
|
|
|
frame.$el.removeClass('pinned');
|
|
|
|
|
container.css('height', 'auto');
|
|
|
|
|
controls.top = controls.$el.offset().top;
|
|
|
|
|
} else {
|
|
|
|
|
controls.top = controls.$el.offset().top;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setWidth: function() {
|
|
|
|
|
this.$el.css('width', this.$el.parent().width() + 'px');
|
2013-07-08 17:37:03 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The tickmarks view
|
|
|
|
|
revisions.view.Tickmarks = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-tickmarks',
|
2013-07-22 17:09:49 -04:00
|
|
|
|
direction: isRtl ? 'right' : 'left',
|
2013-07-08 17:37:03 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
initialize: function() {
|
|
|
|
|
this.listenTo( this.model, 'change:revision', this.reportTickPosition );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
reportTickPosition: function( model, revision ) {
|
2013-07-22 17:09:49 -04:00
|
|
|
|
var offset, thisOffset, parentOffset, tick, index = this.model.revisions.indexOf( revision );
|
|
|
|
|
thisOffset = this.$el.allOffsets();
|
|
|
|
|
parentOffset = this.$el.parent().allOffsets();
|
2013-07-19 09:49:15 -04:00
|
|
|
|
if ( index === this.model.revisions.length - 1 ) {
|
|
|
|
|
// Last one
|
2013-07-22 17:09:49 -04:00
|
|
|
|
offset = {
|
|
|
|
|
rightPlusWidth: thisOffset.left - parentOffset.left + 1,
|
|
|
|
|
leftPlusWidth: thisOffset.right - parentOffset.right + 1
|
|
|
|
|
};
|
2013-07-19 09:49:15 -04:00
|
|
|
|
} else {
|
|
|
|
|
// Normal tick
|
|
|
|
|
tick = this.$('div:nth-of-type(' + (index + 1) + ')');
|
|
|
|
|
offset = tick.allPositions();
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
_.extend( offset, {
|
2013-07-22 17:09:49 -04:00
|
|
|
|
left: offset.left + thisOffset.left - parentOffset.left,
|
|
|
|
|
right: offset.right + thisOffset.right - parentOffset.right
|
|
|
|
|
});
|
|
|
|
|
_.extend( offset, {
|
|
|
|
|
leftPlusWidth: offset.left + tick.outerWidth(),
|
|
|
|
|
rightPlusWidth: offset.right + tick.outerWidth()
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
});
|
2013-07-19 09:49:15 -04:00
|
|
|
|
}
|
|
|
|
|
this.model.set({ offset: offset });
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
ready: function() {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
var tickCount, tickWidth;
|
|
|
|
|
tickCount = this.model.revisions.length - 1;
|
|
|
|
|
tickWidth = 1 / tickCount;
|
2013-07-24 02:08:14 -04:00
|
|
|
|
this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
|
2013-07-08 17:37:03 -04:00
|
|
|
|
|
2013-07-22 17:09:49 -04:00
|
|
|
|
_(tickCount).times( function( index ){
|
2013-07-24 02:08:14 -04:00
|
|
|
|
this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' );
|
|
|
|
|
}, this );
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-07-28 16:15:28 -04:00
|
|
|
|
// The metabox view
|
|
|
|
|
revisions.view.Metabox = wp.Backbone.View.extend({
|
2013-06-26 17:06:50 -04:00
|
|
|
|
className: 'revisions-meta',
|
2013-07-28 16:15:28 -04:00
|
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
|
// Add the 'from' view
|
|
|
|
|
this.views.add( new revisions.view.MetaFrom({
|
|
|
|
|
model: this.model,
|
|
|
|
|
className: 'diff-meta diff-meta-from'
|
|
|
|
|
}) );
|
|
|
|
|
|
|
|
|
|
// Add the 'to' view
|
|
|
|
|
this.views.add( new revisions.view.MetaTo({
|
|
|
|
|
model: this.model
|
|
|
|
|
}) );
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The revision meta view (to be extended)
|
|
|
|
|
revisions.view.Meta = wp.Backbone.View.extend({
|
2013-06-26 17:06:50 -04:00
|
|
|
|
template: wp.template('revisions-meta'),
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
events: {
|
2013-07-10 00:37:50 -04:00
|
|
|
|
'click .restore-revision': 'restoreRevision'
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
initialize: function() {
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
this.listenTo( this.model, 'update:revisions', this.render );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
prepare: function() {
|
2013-07-28 16:15:28 -04:00
|
|
|
|
return _.extend( this.model.toJSON()[this.type] || {}, {
|
|
|
|
|
type: this.type
|
|
|
|
|
});
|
2013-07-12 10:01:39 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
restoreRevision: function() {
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
document.location = this.model.get('to').attributes.restoreUrl;
|
2013-04-04 03:53:49 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-07-28 16:15:28 -04:00
|
|
|
|
// The revision meta 'from' view
|
|
|
|
|
revisions.view.MetaFrom = revisions.view.Meta.extend({
|
|
|
|
|
className: 'diff-meta diff-meta-from',
|
|
|
|
|
type: 'from'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The revision meta 'to' view
|
|
|
|
|
revisions.view.MetaTo = revisions.view.Meta.extend({
|
|
|
|
|
className: 'diff-meta diff-meta-to',
|
|
|
|
|
type: 'to'
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// The checkbox view.
|
|
|
|
|
revisions.view.Checkbox = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-checkbox',
|
2013-07-09 03:33:15 -04:00
|
|
|
|
template: wp.template('revisions-checkbox'),
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
events: {
|
|
|
|
|
'click .compare-two-revisions': 'compareTwoToggle'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initialize: function() {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
ready: function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.model.revisions.length < 3 ) {
|
2013-07-12 10:01:39 -04:00
|
|
|
|
$('.revision-toggle-compare-mode').hide();
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateCompareTwoMode: function() {
|
2013-07-10 01:40:45 -04:00
|
|
|
|
this.$('.compare-two-revisions').prop( 'checked', this.model.get('compareTwoMode') );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Toggle the compare two mode feature when the compare two checkbox is checked.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
compareTwoToggle: function() {
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Activate compare two mode?
|
2013-07-09 03:33:15 -04:00
|
|
|
|
this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The tooltip view.
|
|
|
|
|
// Encapsulates the tooltip.
|
|
|
|
|
revisions.view.Tooltip = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-tooltip',
|
2013-07-28 16:15:28 -04:00
|
|
|
|
template: wp.template('revisions-meta'),
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
initialize: function() {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
this.listenTo( this.model, 'change:offset', this.render );
|
2013-07-11 05:14:14 -04:00
|
|
|
|
this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
|
|
|
|
|
this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
prepare: function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( _.isNull( this.model.get('revision') ) ) {
|
2013-07-29 13:26:03 -04:00
|
|
|
|
return;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else {
|
2013-07-29 13:26:03 -04:00
|
|
|
|
return _.extend( { type: 'tooltip' }, {
|
|
|
|
|
attributes: this.model.get('revision').toJSON()
|
|
|
|
|
});
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-19 09:49:15 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render: function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var otherDirection,
|
|
|
|
|
direction,
|
|
|
|
|
directionVal,
|
|
|
|
|
flipped,
|
|
|
|
|
css = {},
|
|
|
|
|
position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
|
|
|
|
|
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
flipped = ( position / this.model.revisions.length ) > 0.5;
|
|
|
|
|
if ( isRtl ) {
|
|
|
|
|
direction = flipped ? 'left' : 'right';
|
|
|
|
|
directionVal = flipped ? 'leftPlusWidth' : direction;
|
|
|
|
|
} else {
|
|
|
|
|
direction = flipped ? 'right' : 'left';
|
|
|
|
|
directionVal = flipped ? 'rightPlusWidth' : direction;
|
|
|
|
|
}
|
|
|
|
|
otherDirection = 'right' === direction ? 'left': 'right';
|
2013-07-19 09:49:15 -04:00
|
|
|
|
wp.Backbone.View.prototype.render.apply( this, arguments );
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
css[direction] = this.model.get('offset')[directionVal] + 'px';
|
|
|
|
|
css[otherDirection] = '';
|
|
|
|
|
this.$el.toggleClass( 'flipped', flipped ).css( css );
|
2013-07-19 09:49:15 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
visible: function() {
|
|
|
|
|
return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
toggleVisibility: function() {
|
|
|
|
|
if ( this.visible() ) {
|
2013-07-12 16:34:14 -04:00
|
|
|
|
this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else {
|
2013-07-12 16:34:14 -04:00
|
|
|
|
this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-12 16:34:14 -04:00
|
|
|
|
return;
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The buttons view.
|
|
|
|
|
// Encapsulates all of the configuration for the previous/next buttons.
|
|
|
|
|
revisions.view.Buttons = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-buttons',
|
2013-07-09 03:33:15 -04:00
|
|
|
|
template: wp.template('revisions-buttons'),
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-04-04 03:53:49 -04:00
|
|
|
|
events: {
|
Revisions: Pinned controls, layout tweaks, copy tweaks, misc.
* When you scroll down the diff view, the controls will pin to the top.
* The revisions meta view was cleaned up. Copy changes.
* Loading indicator in the center of the screen (so it follows as you scroll).
* Tooltips "flip" when you cross the center line, so that they don't hit the container edge and wrap for later revisions.
* The "Restore" button's inactive state is handled on render, instead of after.
* Make sure we always have a current revision, even if the timestamp doesn't work out on the most recent one.
See #24804. Props markjaquith, nacin, ocean90, aaroncampbell.
git-svn-id: http://core.svn.wordpress.org/trunk@24761 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-07-22 01:05:45 -04:00
|
|
|
|
'click .revisions-next .button': 'nextRevision',
|
|
|
|
|
'click .revisions-previous .button': 'previousRevision'
|
2013-06-26 17:06:50 -04:00
|
|
|
|
},
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
|
|
|
|
initialize: function() {
|
2013-07-11 14:43:18 -04:00
|
|
|
|
this.listenTo( this.model, 'update:revisions', this.disabledButtonCheck );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
ready: function() {
|
2013-07-11 14:43:18 -04:00
|
|
|
|
this.disabledButtonCheck();
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Go to a specific model index
|
2013-06-26 17:06:50 -04:00
|
|
|
|
gotoModel: function( toIndex ) {
|
|
|
|
|
var attributes = {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
to: this.model.revisions.at( toIndex )
|
2013-06-26 17:06:50 -04:00
|
|
|
|
};
|
|
|
|
|
// If we're at the first revision, unset 'from'.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( toIndex ) {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
attributes.from = this.model.revisions.at( toIndex - 1 );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.model.unset('from', { silent: true });
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.model.set( attributes );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Go to the 'next' revision
|
2013-04-04 03:53:49 -04:00
|
|
|
|
nextRevision: function() {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
var toIndex = this.model.revisions.indexOf( this.model.get('to') ) + 1;
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.gotoModel( toIndex );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Go to the 'previous' revision
|
2013-04-04 03:53:49 -04:00
|
|
|
|
previousRevision: function() {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
var toIndex = this.model.revisions.indexOf( this.model.get('to') ) - 1;
|
2013-06-26 17:06:50 -04:00
|
|
|
|
this.gotoModel( toIndex );
|
|
|
|
|
},
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Check to see if the Previous or Next buttons need to be disabled or enabled.
|
2013-06-26 17:06:50 -04:00
|
|
|
|
disabledButtonCheck: function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var maxVal = this.model.revisions.length - 1,
|
|
|
|
|
minVal = 0,
|
|
|
|
|
next = $('.revisions-next .button'),
|
2013-07-09 03:33:15 -04:00
|
|
|
|
previous = $('.revisions-previous .button'),
|
2013-11-07 13:14:10 -05:00
|
|
|
|
val = this.model.revisions.indexOf( this.model.get('to') );
|
2013-03-07 10:32:26 -05:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Disable "Next" button if you're on the last node.
|
2013-07-05 13:44:47 -04:00
|
|
|
|
next.prop( 'disabled', ( maxVal === val ) );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Disable "Previous" button if you're on the first node.
|
2013-07-05 13:44:47 -04:00
|
|
|
|
previous.prop( 'disabled', ( minVal === val ) );
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
});
|
2013-03-21 11:54:11 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// The slider view.
|
|
|
|
|
revisions.view.Slider = wp.Backbone.View.extend({
|
|
|
|
|
className: 'wp-slider',
|
2013-07-19 09:49:15 -04:00
|
|
|
|
direction: isRtl ? 'right' : 'left',
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
2013-07-03 16:27:19 -04:00
|
|
|
|
events: {
|
2013-07-12 16:34:14 -04:00
|
|
|
|
'mousemove' : 'mouseMove'
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
initialize: function() {
|
2013-07-12 16:34:14 -04:00
|
|
|
|
_.bindAll( this, 'start', 'slide', 'stop', 'mouseMove', 'mouseEnter', 'mouseLeave' );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.listenTo( this.model, 'update:slider', this.applySliderSettings );
|
2013-07-11 05:14:14 -04:00
|
|
|
|
},
|
2013-06-26 17:06:50 -04:00
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
ready: function() {
|
2013-07-24 02:08:14 -04:00
|
|
|
|
this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
|
2013-07-11 05:14:14 -04:00
|
|
|
|
this.$el.slider( _.extend( this.model.toJSON(), {
|
2013-06-26 17:06:50 -04:00
|
|
|
|
start: this.start,
|
|
|
|
|
slide: this.slide,
|
|
|
|
|
stop: this.stop
|
2013-07-11 05:14:14 -04:00
|
|
|
|
}) );
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-07-12 16:34:14 -04:00
|
|
|
|
this.$el.hoverIntent({
|
|
|
|
|
over: this.mouseEnter,
|
|
|
|
|
out: this.mouseLeave,
|
|
|
|
|
timeout: 800
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.applySliderSettings();
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
mouseMove: function( e ) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var zoneCount = this.model.revisions.length - 1, // One fewer zone than models
|
|
|
|
|
sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider
|
|
|
|
|
sliderWidth = this.$el.width(), // Width of slider
|
|
|
|
|
tickWidth = sliderWidth / zoneCount, // Calculated width of zone
|
|
|
|
|
actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom;
|
|
|
|
|
currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-07-08 17:37:03 -04:00
|
|
|
|
// Ensure sane value for currentModelIndex.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( currentModelIndex < 0 ) {
|
2013-07-08 17:37:03 -04:00
|
|
|
|
currentModelIndex = 0;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else if ( currentModelIndex >= this.model.revisions.length ) {
|
2013-07-08 17:37:03 -04:00
|
|
|
|
currentModelIndex = this.model.revisions.length - 1;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
// Update the tooltip mode
|
2013-07-12 14:14:23 -04:00
|
|
|
|
this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) });
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
mouseLeave: function() {
|
|
|
|
|
this.model.set({ hovering: false });
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
mouseEnter: function() {
|
|
|
|
|
this.model.set({ hovering: true });
|
2013-07-08 17:37:03 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-12 10:01:39 -04:00
|
|
|
|
applySliderSettings: function() {
|
|
|
|
|
this.$el.slider( _.pick( this.model.toJSON(), 'value', 'values', 'range' ) );
|
|
|
|
|
var handles = this.$('a.ui-slider-handle');
|
2013-07-10 01:40:45 -04:00
|
|
|
|
|
2013-07-09 03:33:15 -04:00
|
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
2013-07-10 01:40:45 -04:00
|
|
|
|
// in RTL mode the 'left handle' is the second in the slider, 'right' is first
|
|
|
|
|
handles.first()
|
2013-07-19 09:49:15 -04:00
|
|
|
|
.toggleClass( 'to-handle', !! isRtl )
|
|
|
|
|
.toggleClass( 'from-handle', ! isRtl );
|
2013-07-10 01:40:45 -04:00
|
|
|
|
handles.last()
|
2013-07-19 09:49:15 -04:00
|
|
|
|
.toggleClass( 'from-handle', !! isRtl )
|
|
|
|
|
.toggleClass( 'to-handle', ! isRtl );
|
2013-07-06 06:48:14 -04:00
|
|
|
|
} else {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
handles.removeClass('from-handle to-handle');
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
},
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
start: function( event, ui ) {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
this.model.set({ scrubbing: true });
|
|
|
|
|
|
2013-07-05 18:54:17 -04:00
|
|
|
|
// Track the mouse position to enable smooth dragging,
|
2013-07-06 16:22:02 -04:00
|
|
|
|
// overrides default jQuery UI step behavior.
|
2013-07-11 05:14:14 -04:00
|
|
|
|
$( window ).on( 'mousemove.wp.revisions', { view: this }, function( e ) {
|
2013-11-14 00:56:09 -05:00
|
|
|
|
var handles,
|
|
|
|
|
view = e.data.view,
|
|
|
|
|
leftDragBoundary = view.$el.offset().left,
|
|
|
|
|
sliderOffset = leftDragBoundary,
|
|
|
|
|
sliderRightEdge = leftDragBoundary + view.$el.width(),
|
|
|
|
|
rightDragBoundary = sliderRightEdge,
|
|
|
|
|
leftDragReset = '0',
|
|
|
|
|
rightDragReset = '100%',
|
|
|
|
|
handle = $( ui.handle );
|
2013-07-06 16:22:02 -04:00
|
|
|
|
|
|
|
|
|
// In two handle mode, ensure handles can't be dragged past each other.
|
|
|
|
|
// Adjust left/right boundaries and reset points.
|
2013-07-12 10:01:39 -04:00
|
|
|
|
if ( view.model.get('compareTwoMode') ) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
handles = handle.parent().find('.ui-slider-handle');
|
2013-07-19 09:49:15 -04:00
|
|
|
|
if ( handle.is( handles.first() ) ) { // We're the left handle
|
|
|
|
|
rightDragBoundary = handles.last().offset().left;
|
|
|
|
|
rightDragReset = rightDragBoundary - sliderOffset;
|
|
|
|
|
} else { // We're the right handle
|
|
|
|
|
leftDragBoundary = handles.first().offset().left + handles.first().width();
|
|
|
|
|
leftDragReset = leftDragBoundary - sliderOffset;
|
2013-07-06 16:22:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-05 18:54:17 -04:00
|
|
|
|
|
|
|
|
|
// Follow mouse movements, as long as handle remains inside slider.
|
2013-07-19 09:49:15 -04:00
|
|
|
|
if ( e.pageX < leftDragBoundary ) {
|
|
|
|
|
handle.css( 'left', leftDragReset ); // Mouse to left of slider.
|
|
|
|
|
} else if ( e.pageX > rightDragBoundary ) {
|
|
|
|
|
handle.css( 'left', rightDragReset ); // Mouse to right of slider.
|
2013-07-05 18:54:17 -04:00
|
|
|
|
} else {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
handle.css( 'left', e.pageX - sliderOffset ); // Mouse in slider.
|
2013-07-05 18:54:17 -04:00
|
|
|
|
}
|
|
|
|
|
} );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-19 09:49:15 -04:00
|
|
|
|
getPosition: function( position ) {
|
|
|
|
|
return isRtl ? this.model.revisions.length - position - 1: position;
|
|
|
|
|
},
|
|
|
|
|
|
2013-07-11 05:14:14 -04:00
|
|
|
|
// Responds to slide events
|
2013-06-26 17:06:50 -04:00
|
|
|
|
slide: function( event, ui ) {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
var attributes, movedRevision;
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Compare two revisions mode
|
2013-07-12 14:14:23 -04:00
|
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
2013-07-03 16:27:19 -04:00
|
|
|
|
// Prevent sliders from occupying same spot
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( ui.values[1] === ui.values[0] ) {
|
2013-07-03 16:27:19 -04:00
|
|
|
|
return false;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
|
|
|
|
if ( isRtl ) {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
ui.values.reverse();
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
attributes = {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
from: this.model.revisions.at( this.getPosition( ui.values[0] ) ),
|
|
|
|
|
to: this.model.revisions.at( this.getPosition( ui.values[1] ) )
|
2013-07-03 16:27:19 -04:00
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
attributes = {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
to: this.model.revisions.at( this.getPosition( ui.value ) )
|
2013-07-03 16:27:19 -04:00
|
|
|
|
};
|
|
|
|
|
// If we're at the first revision, unset 'from'.
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.getPosition( ui.value ) > 0 ) {
|
2013-07-19 09:49:15 -04:00
|
|
|
|
attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
attributes.from = undefined;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
2013-07-19 09:49:15 -04:00
|
|
|
|
movedRevision = this.model.revisions.at( this.getPosition( ui.value ) );
|
2013-07-11 05:14:14 -04:00
|
|
|
|
|
|
|
|
|
// If we are scrubbing, a scrub to a revision is considered a hover
|
2013-11-07 13:14:10 -05:00
|
|
|
|
if ( this.model.get('scrubbing') ) {
|
2013-07-12 13:25:27 -04:00
|
|
|
|
attributes.hoveredRevision = movedRevision;
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-12 13:25:27 -04:00
|
|
|
|
|
|
|
|
|
this.model.set( attributes );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-11-07 13:14:10 -05:00
|
|
|
|
stop: function() {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
$( window ).off('mousemove.wp.revisions');
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.model.updateSliderSettings(); // To snap us back to a tick mark
|
2013-07-11 05:14:14 -04:00
|
|
|
|
this.model.set({ scrubbing: false });
|
2013-04-04 03:53:49 -04:00
|
|
|
|
}
|
2013-02-28 10:14:34 -05:00
|
|
|
|
});
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// The diff view.
|
|
|
|
|
// This is the view for the current active diff.
|
|
|
|
|
revisions.view.Diff = wp.Backbone.View.extend({
|
|
|
|
|
className: 'revisions-diff',
|
2013-11-07 13:14:10 -05:00
|
|
|
|
template: wp.template('revisions-diff'),
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
// Generate the options to be passed to the template.
|
|
|
|
|
prepare: function() {
|
|
|
|
|
return _.extend({ fields: this.model.fields.toJSON() }, this.options );
|
2013-04-04 03:53:49 -04:00
|
|
|
|
}
|
2013-06-26 17:06:50 -04:00
|
|
|
|
});
|
2013-04-04 03:53:49 -04:00
|
|
|
|
|
2014-02-25 15:07:14 -05:00
|
|
|
|
// The revisions router.
|
|
|
|
|
// Maintains the URL routes so browser URL matches state.
|
2013-07-09 04:15:55 -04:00
|
|
|
|
revisions.Router = Backbone.Router.extend({
|
2013-07-04 04:30:36 -04:00
|
|
|
|
initialize: function( options ) {
|
|
|
|
|
this.model = options.model;
|
2014-02-25 15:07:14 -05:00
|
|
|
|
|
2013-07-12 14:14:23 -04:00
|
|
|
|
// Maintain state and history when navigating
|
2013-07-09 05:57:58 -04:00
|
|
|
|
this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
|
2013-07-12 10:01:39 -04:00
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl );
|
2013-07-04 04:30:36 -04:00
|
|
|
|
},
|
2013-07-03 16:27:19 -04:00
|
|
|
|
|
2013-07-11 18:56:48 -04:00
|
|
|
|
baseUrl: function( url ) {
|
|
|
|
|
return this.model.get('baseUrl') + url;
|
2013-07-03 16:27:19 -04:00
|
|
|
|
},
|
|
|
|
|
|
2013-07-09 04:14:38 -04:00
|
|
|
|
updateUrl: function() {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var from = this.model.has('from') ? this.model.get('from').id : 0,
|
|
|
|
|
to = this.model.get('to').id;
|
|
|
|
|
if ( this.model.get('compareTwoMode' ) ) {
|
2014-02-06 18:28:12 -05:00
|
|
|
|
this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ), { replace: true } );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
} else {
|
2014-02-06 18:28:12 -05:00
|
|
|
|
this.navigate( this.baseUrl( '?revision=' + to ), { replace: true } );
|
2013-11-07 13:14:10 -05:00
|
|
|
|
}
|
2013-07-09 04:14:38 -04:00
|
|
|
|
},
|
2013-07-04 04:30:36 -04:00
|
|
|
|
|
2013-07-10 02:08:56 -04:00
|
|
|
|
handleRoute: function( a, b ) {
|
2013-11-07 13:14:10 -05:00
|
|
|
|
var compareTwo = _.isUndefined( b );
|
2013-07-10 02:08:56 -04:00
|
|
|
|
|
2013-07-12 14:14:23 -04:00
|
|
|
|
if ( ! compareTwo ) {
|
2013-07-11 05:14:14 -04:00
|
|
|
|
b = this.model.revisions.get( a );
|
|
|
|
|
a = this.model.revisions.prev( b );
|
|
|
|
|
b = b ? b.id : 0;
|
2013-07-11 18:56:48 -04:00
|
|
|
|
a = a ? a.id : 0;
|
2013-07-10 02:08:56 -04:00
|
|
|
|
}
|
2013-07-03 16:27:19 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-10-31 20:08:21 -04:00
|
|
|
|
/**
|
|
|
|
|
* Initialize the revisions UI for revision.php.
|
|
|
|
|
*/
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.init = function() {
|
2014-10-31 20:39:24 -04:00
|
|
|
|
var state;
|
|
|
|
|
|
2014-10-31 20:08:21 -04:00
|
|
|
|
// Bail if the current page is not revision.php.
|
|
|
|
|
if ( ! window.adminpage || 'revision-php' !== window.adminpage ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 20:39:24 -04:00
|
|
|
|
state = new revisions.model.FrameState({
|
|
|
|
|
initialDiffState: {
|
|
|
|
|
// wp_localize_script doesn't stringifies ints, so cast them.
|
|
|
|
|
to: parseInt( revisions.settings.to, 10 ),
|
|
|
|
|
from: parseInt( revisions.settings.from, 10 ),
|
|
|
|
|
// wp_localize_script does not allow for top-level booleans so do a comparator here.
|
|
|
|
|
compareTwoMode: ( revisions.settings.compareTwoMode === '1' )
|
|
|
|
|
},
|
|
|
|
|
diffData: revisions.settings.diffData,
|
|
|
|
|
baseUrl: revisions.settings.baseUrl,
|
|
|
|
|
postId: parseInt( revisions.settings.postId, 10 )
|
|
|
|
|
}, {
|
|
|
|
|
revisions: new revisions.model.Revisions( revisions.settings.revisionData )
|
|
|
|
|
});
|
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
revisions.view.frame = new revisions.view.Frame({
|
2014-10-31 20:39:24 -04:00
|
|
|
|
model: state
|
2013-06-26 17:06:50 -04:00
|
|
|
|
}).render();
|
|
|
|
|
};
|
2013-02-28 10:14:34 -05:00
|
|
|
|
|
2013-06-26 17:06:50 -04:00
|
|
|
|
$( revisions.init );
|
2013-02-28 10:14:34 -05:00
|
|
|
|
}(jQuery));
|