2013-02-28 16:14:34 +01:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
(function($) {
|
2013-06-26 23:06:50 +02:00
|
|
|
var revisions;
|
2013-03-07 16:32:26 +01:00
|
|
|
|
2013-07-09 10:15:55 +02:00
|
|
|
revisions = wp.revisions = { model: {}, view: {}, controller: {} };
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-05-02 12:12:19 +02:00
|
|
|
// Link settings.
|
2013-07-10 08:08:56 +02:00
|
|
|
revisions.settings = _.isUndefined( _wpRevisionsSettings ) ? {} : _wpRevisionsSettings;
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-07-09 21:00:11 +02:00
|
|
|
// For debugging
|
|
|
|
revisions.debug = true;
|
|
|
|
|
|
|
|
revisions.log = function() {
|
|
|
|
if ( revisions.debug )
|
|
|
|
console.log.apply( console, arguments );
|
2013-07-09 21:21:25 +02:00
|
|
|
};
|
2013-07-09 21:00:11 +02:00
|
|
|
|
2013-07-09 11:57:58 +02:00
|
|
|
// wp_localize_script transforms top-level numbers into strings. Undo that.
|
2013-07-12 00:56:48 +02:00
|
|
|
if ( revisions.settings.to )
|
|
|
|
revisions.settings.to = parseInt( revisions.settings.to, 10 );
|
|
|
|
if ( revisions.settings.from )
|
|
|
|
revisions.settings.from = parseInt( revisions.settings.from, 10 );
|
2013-07-09 11:57:58 +02:00
|
|
|
|
2013-07-12 00:56:48 +02:00
|
|
|
// wp_localize_script does not allow for top-level booleans. Fix that.
|
|
|
|
if ( revisions.settings.compareTwoMode )
|
|
|
|
revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1';
|
2013-04-04 09:53:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ========================================================================
|
2013-06-26 23:06:50 +02:00
|
|
|
* MODELS
|
2013-04-04 09:53:49 +02:00
|
|
|
* ========================================================================
|
|
|
|
*/
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Slider = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
value: 0,
|
|
|
|
min: 0,
|
|
|
|
max: 1,
|
2013-07-11 11:14:14 +02:00
|
|
|
step: 1,
|
|
|
|
compareTwoMode: false
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
this.frame = options.frame;
|
|
|
|
this.revisions = options.revisions;
|
|
|
|
this.set({
|
|
|
|
max: this.revisions.length - 1,
|
2013-07-12 00:56:48 +02:00
|
|
|
value: this.revisions.indexOf( this.revisions.get( revisions.settings.to ) ),
|
2013-07-11 11:14:14 +02:00
|
|
|
compareTwoMode: this.frame.get('compareTwoMode')
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
this.listenTo( this, 'change:from', this.handleLocalChanges );
|
|
|
|
this.listenTo( this, 'change:to', this.handleLocalChanges );
|
|
|
|
|
|
|
|
// Listen for changes to the hovered revision
|
|
|
|
this.listenTo( this, 'change:hoveredRevision', this.hoverRevision );
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if ( this.get('from') === from && this.get('to') === to )
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.set({ from: from, to: to }, { silent: true });
|
|
|
|
this.trigger( 'update:revisions', from, to );
|
2013-06-26 23:06:50 +02:00
|
|
|
}
|
2013-07-11 11:14:14 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
revisions.model.Tooltip = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
revision: null,
|
2013-07-11 11:14:14 +02:00
|
|
|
hovering: false, // Whether the mouse is hovering
|
|
|
|
scrubbing: false // Whether the mouse is scrubbing
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
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 );
|
|
|
|
},
|
|
|
|
|
|
|
|
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 19:44:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Revision = Backbone.Model.extend({});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Revisions = Backbone.Collection.extend({
|
|
|
|
model: revisions.model.Revision,
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll( this, 'next', 'prev' );
|
|
|
|
},
|
|
|
|
|
2013-07-09 11:57:58 +02:00
|
|
|
next: function( revision ) {
|
|
|
|
var index = this.indexOf( revision );
|
|
|
|
|
|
|
|
if ( index !== -1 && index !== this.length - 1 )
|
|
|
|
return this.at( index + 1 );
|
|
|
|
},
|
|
|
|
|
|
|
|
prev: function( revision ) {
|
|
|
|
var index = this.indexOf( revision );
|
|
|
|
|
|
|
|
if ( index !== -1 && index !== 0 )
|
|
|
|
return this.at( index - 1 );
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Field = Backbone.Model.extend({});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Fields = Backbone.Collection.extend({
|
|
|
|
model: revisions.model.Field
|
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Diff = Backbone.Model.extend({
|
2013-07-09 09:33:15 +02:00
|
|
|
initialize: function( attributes, options ) {
|
2013-06-26 23:06:50 +02:00
|
|
|
var fields = this.get('fields');
|
|
|
|
this.unset('fields');
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.fields = new revisions.model.Fields( fields );
|
|
|
|
}
|
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.model.Diffs = Backbone.Collection.extend({
|
2013-07-09 09:33:15 +02:00
|
|
|
initialize: function( models, options ) {
|
2013-06-26 23:06:50 +02:00
|
|
|
this.revisions = options.revisions;
|
|
|
|
this.requests = {};
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
model: revisions.model.Diff,
|
|
|
|
|
|
|
|
ensure: function( id, context ) {
|
|
|
|
var diff = this.get( id );
|
|
|
|
var request = this.requests[ id ];
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
var ids = {};
|
|
|
|
|
|
|
|
if ( diff ) {
|
|
|
|
deferred.resolveWith( context, [ diff ] );
|
2013-04-04 09:53:49 +02:00
|
|
|
} else {
|
2013-06-26 23:06:50 +02:00
|
|
|
this.trigger( 'ensure:load', ids );
|
|
|
|
_.each( ids, _.bind( function(id) {
|
|
|
|
// Remove anything that has an ongoing request
|
|
|
|
if ( this.requests[ id ] )
|
|
|
|
delete ids[ id ];
|
|
|
|
}, 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 ) ] );
|
|
|
|
}, this ) );
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
|
|
|
|
return deferred.promise();
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
loadNew: function( comparisons ) {
|
2013-07-09 21:00:11 +02:00
|
|
|
var self = this;
|
|
|
|
_.each( comparisons, function( id, index ) {
|
|
|
|
// Already exists in collection. Don't request it again.
|
|
|
|
if ( self.get( id ) )
|
|
|
|
delete comparisons[ index ];
|
|
|
|
});
|
|
|
|
wp.revisions.log( 'loadNew', comparisons );
|
|
|
|
|
|
|
|
if ( comparisons.length )
|
|
|
|
return this.load( comparisons );
|
|
|
|
else
|
|
|
|
return $.Deferred().resolve().promise();
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
load: function( comparisons ) {
|
2013-07-09 21:00:11 +02:00
|
|
|
wp.revisions.log( 'load', comparisons );
|
2013-06-26 23:06:50 +02:00
|
|
|
// Our collection should only ever grow, never shrink, so remove: false
|
|
|
|
return this.fetch({ data: { compare: comparisons }, remove: false });
|
|
|
|
},
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
loadLast: function( num ) {
|
2013-07-09 21:00:11 +02:00
|
|
|
var ids;
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-09 21:00:11 +02:00
|
|
|
num = num || 1;
|
|
|
|
ids = _.last( this.getProximalDiffIds(), num );
|
|
|
|
|
|
|
|
if ( ids.length )
|
2013-06-26 23:06:50 +02:00
|
|
|
return this.loadNew( ids );
|
2013-07-09 21:00:11 +02:00
|
|
|
else
|
|
|
|
return $.Deferred().resolve().promise();
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
loadLastUnloaded: function( num ) {
|
2013-07-09 21:00:11 +02:00
|
|
|
var ids;
|
|
|
|
|
|
|
|
num = num || 1;
|
|
|
|
ids = _.last( this.getUnloadedProximalDiffIds(), num );
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-09 21:00:11 +02:00
|
|
|
if ( ids.length )
|
2013-06-26 23:06:50 +02:00
|
|
|
return this.loadNew( ids );
|
2013-07-09 21:00:11 +02:00
|
|
|
else
|
|
|
|
return $.Deferred().resolve().promise();
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
getProximalDiffIds: function() {
|
|
|
|
var previous = 0, ids = [];
|
2013-07-09 09:33:15 +02:00
|
|
|
this.revisions.each( _.bind( function( revision ) {
|
2013-06-26 23:06:50 +02:00
|
|
|
ids.push( previous + ':' + revision.id );
|
|
|
|
previous = revision.id;
|
|
|
|
}, this ) );
|
|
|
|
return ids;
|
|
|
|
},
|
|
|
|
|
|
|
|
getUnloadedProximalDiffIds: function() {
|
|
|
|
var comparisons = this.getProximalDiffIds();
|
|
|
|
comparisons = _.object( comparisons, comparisons );
|
|
|
|
_.each( comparisons, _.bind( function( id ) {
|
|
|
|
// Exists
|
|
|
|
if ( this.get( id ) )
|
|
|
|
delete comparisons[ id ];
|
|
|
|
}, this ) );
|
|
|
|
return _.toArray( comparisons );
|
|
|
|
},
|
|
|
|
|
|
|
|
loadAllBy: function( chunkSize ) {
|
|
|
|
chunkSize = chunkSize || 20;
|
|
|
|
var unloaded = this.getUnloadedProximalDiffIds();
|
|
|
|
if ( unloaded.length ) {
|
|
|
|
return this.loadLastUnloaded( chunkSize ).always( _.bind( function() {
|
|
|
|
this.loadAllBy( chunkSize );
|
|
|
|
}, this ) );
|
|
|
|
}
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
sync: function( method, model, options ) {
|
|
|
|
if ( 'read' === method ) {
|
2013-07-09 09:33:15 +02:00
|
|
|
options = options || {};
|
2013-06-26 23:06:50 +02:00
|
|
|
options.context = this;
|
2013-07-09 09:33:15 +02:00
|
|
|
options.data = _.extend( options.data || {}, {
|
2013-06-26 23:06:50 +02:00
|
|
|
action: 'get-revision-diffs',
|
|
|
|
post_id: revisions.settings.postId
|
|
|
|
});
|
2013-04-17 22:12:25 +02:00
|
|
|
|
2013-07-11 02:20:36 +02:00
|
|
|
var deferred = wp.ajax.send( options );
|
2013-06-26 23:06:50 +02:00
|
|
|
var requests = this.requests;
|
2013-04-17 22:12:25 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Record that we're requesting each diff.
|
|
|
|
if ( options.data.compare ) {
|
|
|
|
_.each( options.data.compare, function( id ) {
|
|
|
|
requests[ id ] = deferred;
|
|
|
|
});
|
|
|
|
}
|
2013-04-17 22:12:25 +02:00
|
|
|
|
2013-06-26 23:06:50 +02: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 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
return deferred;
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Otherwise, fall back to `Backbone.sync()`.
|
|
|
|
} else {
|
|
|
|
return Backbone.Model.prototype.sync.apply( this, arguments );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-03-07 16:32:26 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
|
|
|
|
revisions.model.FrameState = Backbone.Model.extend({
|
2013-07-12 00:56:48 +02:00
|
|
|
defaults: {
|
2013-07-12 07:11:50 +02:00
|
|
|
loading: false,
|
2013-07-12 00:56:48 +02:00
|
|
|
compareTwoMode: false
|
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
initialize: function( attributes, options ) {
|
2013-07-09 11:57:58 +02:00
|
|
|
var properties = {};
|
|
|
|
|
|
|
|
this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.revisions = options.revisions;
|
2013-07-09 09:07:53 +02:00
|
|
|
this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions });
|
2013-07-09 11:57:58 +02:00
|
|
|
|
2013-07-12 00:56:48 +02:00
|
|
|
// Set the initial diffs collection provided through the settings
|
|
|
|
this.diffs.set( revisions.settings.diffData );
|
|
|
|
|
2013-07-12 07:11:56 +02:00
|
|
|
// Set up internal listeners
|
|
|
|
this.listenTo( this, 'change:from', this.changeRevisionHandler );
|
|
|
|
this.listenTo( this, 'change:to', this.changeRevisionHandler );
|
|
|
|
this.listenTo( this, 'update:revisions', this.loadSurrounding );
|
|
|
|
this.listenTo( this, 'change:compareTwoMode', this.changedMode );
|
|
|
|
this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus );
|
|
|
|
this.listenTo( this, 'update:diff', this.updateLoadingStatus );
|
|
|
|
|
2013-07-12 00:56:48 +02:00
|
|
|
// Set the initial revisions, baseUrl, and mode as provided through settings
|
|
|
|
properties.to = this.revisions.get( revisions.settings.to );
|
2013-07-12 07:11:56 +02:00
|
|
|
properties.from = this.revisions.get( revisions.settings.from );
|
2013-07-12 00:56:48 +02:00
|
|
|
properties.compareTwoMode = revisions.settings.compareTwoMode;
|
|
|
|
properties.baseUrl = revisions.settings.baseUrl;
|
2013-07-12 07:11:56 +02:00
|
|
|
this.set( properties, { silent: true } );
|
2013-07-09 11:57:58 +02:00
|
|
|
|
2013-07-12 07:11:56 +02:00
|
|
|
// Start the router
|
2013-07-09 10:20:12 +02:00
|
|
|
this.router = new revisions.Router({ model: this });
|
2013-07-12 00:56:48 +02:00
|
|
|
Backbone.history.start({ pushState: true });
|
2013-07-09 11:57:58 +02:00
|
|
|
},
|
|
|
|
|
2013-07-09 21:00:11 +02:00
|
|
|
changedMode: function() {
|
|
|
|
// This isn't passed from/to so we grab them from the model
|
|
|
|
this.loadSurrounding( this.get( 'from' ), this.get( 'to' ) );
|
|
|
|
},
|
|
|
|
|
2013-07-12 07:11:56 +02:00
|
|
|
updateLoadingStatus: function() {
|
|
|
|
this.set( 'loading', ! this.diff() );
|
|
|
|
},
|
|
|
|
|
2013-07-09 21:00:11 +02:00
|
|
|
loadSurrounding: function( from, to ) {
|
|
|
|
// Different strategies for single and compare-two models
|
|
|
|
if ( this.get( 'compareTwoMode' ) ) {
|
|
|
|
// TODO: compare-two loading strategy
|
|
|
|
} else {
|
|
|
|
// TODO: clean this up to hook in to the ensure process
|
|
|
|
if ( this.revisions.length ) {
|
|
|
|
// Load the rest: first 10, then the rest by 50
|
|
|
|
this.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
|
|
|
|
this.diffs.loadAllBy( 50 );
|
|
|
|
}, this ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-07-09 11:57:58 +02:00
|
|
|
// Fetch the currently loaded diff.
|
|
|
|
diff: function() {
|
|
|
|
return this.diffs.get( this._diffId );
|
2013-06-26 23:06:50 +02:00
|
|
|
},
|
|
|
|
|
2013-07-09 09:07:53 +02: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 11:57:58 +02:00
|
|
|
updateDiff: function( options ) {
|
|
|
|
var from, to, diffId, diff;
|
2013-07-09 10:50:32 +02:00
|
|
|
|
2013-07-09 11:57:58 +02:00
|
|
|
options = options || {};
|
2013-07-09 10:50:32 +02: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.
|
|
|
|
if ( this._diffId === diffId )
|
2013-07-12 07:11:50 +02:00
|
|
|
return $.Deferred().reject().promise();
|
2013-07-09 10:50:32 +02:00
|
|
|
|
|
|
|
this._diffId = diffId;
|
|
|
|
this.trigger( 'update:revisions', from, to );
|
|
|
|
|
2013-07-09 11:57:58 +02:00
|
|
|
// If we already have the diff, then immediately trigger the update.
|
|
|
|
diff = this.diffs.get( diffId );
|
|
|
|
if ( diff ) {
|
2013-07-09 10:50:32 +02:00
|
|
|
this.trigger( 'update:diff', diff );
|
2013-07-10 05:20:58 +02:00
|
|
|
return $.Deferred().resolve().promise();
|
2013-07-09 11:57:58 +02:00
|
|
|
// Otherwise, fetch the diff.
|
|
|
|
} else {
|
2013-07-10 05:20:58 +02:00
|
|
|
if ( options.immediate ) {
|
|
|
|
return this._ensureDiff();
|
|
|
|
} else {
|
2013-07-09 11:57:58 +02:00
|
|
|
this._debouncedEnsureDiff();
|
2013-07-12 07:11:50 +02:00
|
|
|
return $.Deferred().reject().promise();
|
2013-07-10 05:20:58 +02:00
|
|
|
}
|
2013-07-09 11:57:58 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// A simple wrapper around `updateDiff` to prevent the change event's
|
|
|
|
// parameters from being passed through.
|
|
|
|
changeRevisionHandler: function( model, value, options ) {
|
|
|
|
this.updateDiff();
|
|
|
|
},
|
|
|
|
|
|
|
|
_ensureDiff: function() {
|
2013-07-10 05:20:58 +02:00
|
|
|
return this.diffs.ensure( this._diffId, this ).done( function( diff ) {
|
2013-07-09 11:57:58 +02:00
|
|
|
// Make sure the current diff didn't change while the request was in flight.
|
|
|
|
if ( this._diffId === diff.id )
|
|
|
|
this.trigger( 'update:diff', diff );
|
2013-07-09 10:50:32 +02:00
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
|
|
|
});
|
2013-03-21 16:54:11 +01:00
|
|
|
|
|
|
|
|
2013-04-04 09:53:49 +02:00
|
|
|
/**
|
|
|
|
* ========================================================================
|
|
|
|
* VIEWS
|
|
|
|
* ========================================================================
|
|
|
|
*/
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// The frame view. This contains the entire page.
|
|
|
|
revisions.view.Frame = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions',
|
|
|
|
template: wp.template('revisions-frame'),
|
|
|
|
|
|
|
|
initialize: function() {
|
2013-07-09 10:50:32 +02:00
|
|
|
this.listenTo( this.model, 'update:diff', this.renderDiff );
|
2013-07-06 22:22:02 +02:00
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
|
2013-07-12 07:11:56 +02:00
|
|
|
this.listenTo( this.model, 'change:loading', this.updateLoadingStatus );
|
2013-07-06 22:22:02 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.views.set( '.revisions-control-frame', new revisions.view.Controls({
|
|
|
|
model: this.model
|
|
|
|
}) );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
render: function() {
|
2013-07-12 07:11:56 +02:00
|
|
|
console.log( 'diff', this.model.diff() );
|
2013-07-10 05:20:58 +02:00
|
|
|
this.model.updateDiff({ immediate: true }).done( _.bind( function() {
|
|
|
|
wp.Backbone.View.prototype.render.apply( this, arguments );
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-10 05:20:58 +02:00
|
|
|
$('#wpbody-content .wrap').append( this.el );
|
2013-07-10 06:15:34 +02:00
|
|
|
this.updateCompareTwoMode();
|
2013-07-10 05:20:58 +02:00
|
|
|
this.views.ready();
|
|
|
|
}, this ) );
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
return this;
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-07-09 10:50:32 +02:00
|
|
|
renderDiff: function( diff ) {
|
|
|
|
this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
|
|
|
|
model: diff
|
|
|
|
}) );
|
2013-07-06 22:22:02 +02:00
|
|
|
},
|
|
|
|
|
2013-07-12 07:11:56 +02:00
|
|
|
updateLoadingStatus: function() {
|
|
|
|
this.$el.toggleClass( 'loading', this.model.get('loading') );
|
|
|
|
},
|
|
|
|
|
2013-07-06 22:22:02 +02:00
|
|
|
updateCompareTwoMode: function() {
|
2013-07-09 09:11:43 +02:00
|
|
|
this.$el.toggleClass( 'comparing-two-revisions', this.model.get('compareTwoMode') );
|
2013-06-26 23:06:50 +02:00
|
|
|
}
|
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// The control view.
|
2013-07-03 22:27:19 +02:00
|
|
|
// This contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.view.Controls = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-controls',
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
// Add the button view
|
2013-07-03 22:27:19 +02:00
|
|
|
this.views.add( new revisions.view.Buttons({
|
2013-06-26 23:06:50 +02:00
|
|
|
model: this.model
|
2013-07-09 09:33:15 +02:00
|
|
|
}) );
|
2013-06-26 23:06:50 +02:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// Add the checkbox view
|
|
|
|
this.views.add( new revisions.view.Checkbox({
|
|
|
|
model: this.model
|
2013-07-09 09:33:15 +02:00
|
|
|
}) );
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
// Prep the slider model
|
|
|
|
var slider = new revisions.model.Slider({
|
|
|
|
frame: this.model,
|
|
|
|
revisions: this.model.revisions
|
2013-07-05 19:44:47 +02:00
|
|
|
});
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
// Add the tooltip view
|
|
|
|
this.views.add( new revisions.view.Tooltip({
|
|
|
|
model: new revisions.model.Tooltip({
|
|
|
|
revisions: this.model.revisions,
|
|
|
|
slider: slider
|
|
|
|
})
|
|
|
|
}) );
|
|
|
|
|
|
|
|
// Add the tickmarks view
|
2013-07-08 23:37:03 +02:00
|
|
|
this.views.add( new revisions.view.Tickmarks({
|
|
|
|
model: this.model
|
2013-07-09 09:33:15 +02:00
|
|
|
}) );
|
2013-07-08 23:37:03 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
// Add the slider view
|
2013-06-26 23:06:50 +02:00
|
|
|
this.views.add( new revisions.view.Slider({
|
2013-07-11 11:14:14 +02:00
|
|
|
model: slider
|
2013-06-26 23:06:50 +02:00
|
|
|
}) );
|
|
|
|
|
|
|
|
// Add the Meta view
|
|
|
|
this.views.add( new revisions.view.Meta({
|
|
|
|
model: this.model
|
|
|
|
}) );
|
2013-07-08 23:37:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// The tickmarks view
|
|
|
|
// This contains the slider tickmarks.
|
|
|
|
revisions.view.Tickmarks = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-tickmarks',
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
render: function() {
|
|
|
|
var tickCount, tickWidth;
|
2013-07-08 23:37:03 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
tickCount = this.model.revisions.length - 1;
|
|
|
|
tickWidth = 1 / tickCount;
|
2013-07-08 23:37:03 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
this.$el.html('');
|
|
|
|
_(tickCount).times( function(){ this.$el.append( '<div></div>' ); }, this );
|
|
|
|
|
|
|
|
this.$('div').css( 'width', ( 100 * tickWidth ) + '%' );
|
2013-07-08 23:37:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
2013-07-11 11:14:14 +02:00
|
|
|
this.render();
|
2013-06-26 23:06:50 +02:00
|
|
|
}
|
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// The meta view.
|
|
|
|
// This contains the revision meta, and the restore button.
|
|
|
|
revisions.view.Meta = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-meta',
|
|
|
|
template: wp.template('revisions-meta'),
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
events: {
|
2013-07-10 06:37:50 +02:00
|
|
|
'click .restore-revision': 'restoreRevision'
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
initialize: function() {
|
2013-07-09 10:50:32 +02:00
|
|
|
this.listenTo( this.model, 'update:revisions', this.updateMeta );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
restoreRevision: function() {
|
|
|
|
var restoreUrl = this.model.get('to').attributes.restoreUrl.replace(/&/g, '&');
|
|
|
|
document.location = restoreUrl;
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-07-09 10:50:32 +02:00
|
|
|
updateMeta: function( from, to ) {
|
2013-06-26 23:06:50 +02:00
|
|
|
this.$el.html( this.template( this.model.toJSON() ) );
|
2013-07-10 06:37:50 +02:00
|
|
|
this.$('.restore-revision').prop( 'disabled', to.attributes.current );
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
|
|
|
});
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// The checkbox view.
|
|
|
|
// Encapsulates all of the configuration for the compare checkbox.
|
|
|
|
revisions.view.Checkbox = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-checkbox',
|
2013-07-09 09:33:15 +02:00
|
|
|
template: wp.template('revisions-checkbox'),
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
events: {
|
|
|
|
'click .compare-two-revisions': 'compareTwoToggle'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.$el.html( this.template() );
|
|
|
|
},
|
|
|
|
|
|
|
|
updateCompareTwoMode: function() {
|
2013-07-10 07:40:45 +02:00
|
|
|
this.$('.compare-two-revisions').prop( 'checked', this.model.get('compareTwoMode') );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Toggle the compare two mode feature when the compare two checkbox is checked.
|
|
|
|
compareTwoToggle: function( event ) {
|
|
|
|
// Activate compare two mode?
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
// Update route
|
2013-07-09 10:20:12 +02:00
|
|
|
this.model.router.updateUrl();
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
|
|
|
// Hide compare two mode toggle when fewer than three revisions.
|
|
|
|
if ( this.model.revisions.length < 3 )
|
2013-07-09 09:33:15 +02:00
|
|
|
$('.revision-toggle-compare-mode').hide();
|
2013-07-06 12:48:14 +02:00
|
|
|
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
|
|
|
|
|
|
|
|
// Update the mode in case route has set it
|
|
|
|
this.updateCompareTwoMode();
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// The tooltip view.
|
|
|
|
// Encapsulates the tooltip.
|
|
|
|
revisions.view.Tooltip = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-tooltip',
|
2013-07-09 09:33:15 +02:00
|
|
|
template: wp.template('revisions-tooltip'),
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
initialize: function( options ) {
|
|
|
|
this.listenTo( this.model, 'change:revision', this.render );
|
|
|
|
this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
|
|
|
|
this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
2013-07-11 11:14:14 +02:00
|
|
|
this.toggleVisibility({ immediate: true });
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
visible: function() {
|
|
|
|
return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
toggleVisibility: function( options ) {
|
|
|
|
options = options || {};
|
2013-07-11 20:43:12 +02:00
|
|
|
var visible = this.visible();
|
2013-07-11 11:14:14 +02:00
|
|
|
if ( visible ) { // Immediate show
|
2013-07-11 20:43:12 +02:00
|
|
|
this.$el.fadeIn( 200 );
|
2013-07-11 11:14:14 +02:00
|
|
|
} else if ( options.immediate ) { // Immediate fade out
|
2013-07-11 20:43:12 +02:00
|
|
|
this.$el.fadeOut( 200 );
|
2013-07-11 11:14:14 +02:00
|
|
|
} else { // Wait a bit, make sure we're really done, then fade it out
|
|
|
|
_.delay( function( view ) {
|
|
|
|
if ( ! view.visible() )
|
|
|
|
view.toggleVisibility({ immediate: true });
|
|
|
|
}, 500, this );
|
|
|
|
}
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
render: function() {
|
2013-07-11 11:14:14 +02:00
|
|
|
var offset;
|
2013-07-05 19:44:47 +02:00
|
|
|
// Check if a revision exists.
|
2013-07-11 11:14:14 +02:00
|
|
|
if ( _.isNull( this.model.get('revision') ) )
|
2013-07-05 19:44:47 +02:00
|
|
|
return;
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-06 12:48:14 +02:00
|
|
|
// Insert revision data.
|
2013-07-09 09:33:15 +02:00
|
|
|
this.$el.html( this.template( this.model.get('revision').toJSON() ) );
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-06 12:48:14 +02:00
|
|
|
// Set the position.
|
2013-07-11 11:14:14 +02:00
|
|
|
offset = this.model.revisions.indexOf( this.model.get('revision') ) / ( this.model.revisions.length - 1 );
|
|
|
|
// 15% to get us to the start of the slider
|
|
|
|
// 0.7 to convert the slider-relative percentage to a page-relative percentage
|
|
|
|
// 100 to convert to a percentage
|
|
|
|
offset = 15 + (0.7 * offset * 100 ); // Now in a percentage
|
|
|
|
this.$el.css( 'left', offset + '%' );
|
2013-07-03 22:27:19 +02: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 09:33:15 +02:00
|
|
|
template: wp.template('revisions-buttons'),
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-04-04 09:53:49 +02:00
|
|
|
events: {
|
2013-06-26 23:06:50 +02:00
|
|
|
'click #next': 'nextRevision',
|
|
|
|
'click #previous': 'previousRevision'
|
|
|
|
},
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.$el.html( this.template() );
|
2013-07-11 20:43:18 +02:00
|
|
|
this.listenTo( this.model, 'update:revisions', this.disabledButtonCheck );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
2013-07-11 20:43:18 +02:00
|
|
|
this.disabledButtonCheck();
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Go to a specific modelindex, taking into account RTL mode.
|
2013-06-26 23:06:50 +02:00
|
|
|
gotoModel: function( toIndex ) {
|
|
|
|
var attributes = {
|
2013-07-03 22:27:19 +02:00
|
|
|
to: this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for RTL.
|
2013-06-26 23:06:50 +02:00
|
|
|
};
|
|
|
|
// If we're at the first revision, unset 'from'.
|
2013-07-03 22:27:19 +02:00
|
|
|
if ( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for RTL
|
2013-06-26 23:06:50 +02:00
|
|
|
attributes.from = this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 2 : toIndex - 1 );
|
|
|
|
else
|
|
|
|
this.model.unset('from', { silent: true });
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.model.set( attributes );
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
// Update route
|
2013-07-09 10:20:12 +02:00
|
|
|
this.model.router.updateUrl();
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// Go to the 'next' revision, direction takes into account RTL mode.
|
2013-04-04 09:53:49 +02:00
|
|
|
nextRevision: function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 : this.model.revisions.indexOf( this.model.get('to') );
|
2013-06-26 23:06:50 +02:00
|
|
|
toIndex = isRtl ? toIndex - 1 : toIndex + 1;
|
|
|
|
this.gotoModel( toIndex );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
// Go to the 'previous' revision, direction takes into account RTL mode.
|
2013-04-04 09:53:49 +02:00
|
|
|
previousRevision: function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 : this.model.revisions.indexOf( this.model.get('to') );
|
2013-06-26 23:06:50 +02:00
|
|
|
toIndex = isRtl ? toIndex + 1 : toIndex - 1;
|
|
|
|
this.gotoModel( toIndex );
|
|
|
|
},
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// Check to see if the Previous or Next buttons need to be disabled or enabled.
|
2013-06-26 23:06:50 +02:00
|
|
|
disabledButtonCheck: function() {
|
2013-07-03 22:27:19 +02:00
|
|
|
var maxVal = this.model.revisions.length - 1,
|
|
|
|
minVal = 0,
|
2013-07-09 09:33:15 +02:00
|
|
|
next = $('.revisions-next .button'),
|
|
|
|
previous = $('.revisions-previous .button'),
|
|
|
|
val = this.model.revisions.indexOf( this.model.get('to') );
|
2013-03-07 16:32:26 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// Disable "Next" button if you're on the last node.
|
2013-07-05 19:44:47 +02:00
|
|
|
next.prop( 'disabled', ( maxVal === val ) );
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// Disable "Previous" button if you're on the first node.
|
2013-07-05 19:44:47 +02:00
|
|
|
previous.prop( 'disabled', ( minVal === val ) );
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
});
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// The slider view.
|
|
|
|
revisions.view.Slider = wp.Backbone.View.extend({
|
|
|
|
className: 'wp-slider',
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
events: {
|
2013-07-11 11:14:14 +02:00
|
|
|
'mousemove' : 'mouseMove',
|
|
|
|
'mouseleave' : 'mouseLeave',
|
|
|
|
'mouseenter' : 'mouseEnter'
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
initialize: function() {
|
|
|
|
_.bindAll( this, 'start', 'slide', 'stop', 'mouseMove' );
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateSliderSettings );
|
|
|
|
this.listenTo( this.model, 'update:revisions', this.updateSliderSettings );
|
|
|
|
},
|
2013-06-26 23:06:50 +02:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
ready: function() {
|
|
|
|
this.$el.slider( _.extend( this.model.toJSON(), {
|
2013-06-26 23:06:50 +02:00
|
|
|
start: this.start,
|
|
|
|
slide: this.slide,
|
|
|
|
stop: this.stop
|
2013-07-11 11:14:14 +02:00
|
|
|
}) );
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
this.listenTo( this, 'slide:stop', this.updateSliderSettings );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
mouseMove: function( e ) {
|
|
|
|
var zoneCount = this.model.revisions.length - 1, // One fewer zone than models
|
|
|
|
sliderLeft = this.$el.offset().left, // Left edge of slider
|
2013-07-08 23:37:03 +02:00
|
|
|
sliderWidth = this.$el.width(), // Width of slider
|
2013-07-11 11:14:14 +02:00
|
|
|
tickWidth = sliderWidth / zoneCount, // Calculated width of zone
|
2013-07-08 23:37:03 +02:00
|
|
|
actualX = e.clientX - sliderLeft, // Offset of mouse position in slider
|
2013-07-11 11:14:14 +02:00
|
|
|
currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-06 22:22:02 +02:00
|
|
|
// Reverse direction in RTL mode.
|
2013-07-05 19:44:47 +02:00
|
|
|
if ( isRtl )
|
2013-07-08 23:37:03 +02:00
|
|
|
currentModelIndex = this.model.revisions.length - currentModelIndex - 1;
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-08 23:37:03 +02:00
|
|
|
// Ensure sane value for currentModelIndex.
|
|
|
|
if ( currentModelIndex < 0 )
|
|
|
|
currentModelIndex = 0;
|
|
|
|
else if ( currentModelIndex >= this.model.revisions.length )
|
|
|
|
currentModelIndex = this.model.revisions.length - 1;
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
// Update the tooltip model
|
2013-07-11 11:14:14 +02:00
|
|
|
this.model.set({
|
|
|
|
'hoveredRevision': this.model.revisions.at( currentModelIndex )
|
|
|
|
});
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
mouseLeave: function() {
|
|
|
|
this.model.set({ hovering: false });
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
mouseEnter: function() {
|
|
|
|
this.model.set({ hovering: true });
|
2013-07-08 23:37:03 +02:00
|
|
|
},
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
updateSliderSettings: function() {
|
2013-07-11 11:14:14 +02:00
|
|
|
var handles, leftValue, rightValue;
|
2013-07-10 07:40:45 +02:00
|
|
|
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
2013-07-11 11:14:14 +02:00
|
|
|
leftValue = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 :
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.revisions.indexOf( this.model.get('from') ),
|
2013-07-11 11:14:14 +02:00
|
|
|
rightValue = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('from') ) - 1 :
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.revisions.indexOf( this.model.get('to') );
|
2013-07-06 12:48:14 +02:00
|
|
|
|
|
|
|
// Set handles to current from / to models.
|
|
|
|
// Reverse order for RTL
|
|
|
|
this.$el.slider( {
|
|
|
|
values: [
|
|
|
|
leftValue,
|
|
|
|
rightValue
|
|
|
|
],
|
|
|
|
value: null,
|
|
|
|
range: true // Range mode ensures handles can't cross
|
|
|
|
} );
|
2013-07-10 07:40:45 +02:00
|
|
|
|
|
|
|
handles = this.$('a.ui-slider-handle');
|
|
|
|
// in RTL mode the 'left handle' is the second in the slider, 'right' is first
|
|
|
|
handles.first()
|
|
|
|
.toggleClass( 'right-handle', !! isRtl )
|
|
|
|
.toggleClass( 'left-handle', ! isRtl );
|
|
|
|
handles.last()
|
|
|
|
.toggleClass( 'left-handle', !! isRtl )
|
|
|
|
.toggleClass( 'right-handle', ! isRtl );
|
|
|
|
|
2013-07-06 12:48:14 +02:00
|
|
|
} else {
|
|
|
|
this.$el.slider( { // Set handle to current to model
|
|
|
|
// Reverse order for RTL.
|
2013-07-10 07:40:45 +02:00
|
|
|
value: isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 :
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.revisions.indexOf( this.model.get('to') ),
|
2013-07-06 12:48:14 +02:00
|
|
|
values: null, // Clear existing two handled values
|
|
|
|
range: false
|
|
|
|
} );
|
2013-07-10 07:40:45 +02:00
|
|
|
this.$('a.ui-slider-handle').removeClass('left-handle right-handle');
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
getSliderPosition: function( ui ){
|
|
|
|
return isRtl ? this.model.revisions.length - ui.value - 1 : ui.value;
|
2013-06-26 23:06:50 +02:00
|
|
|
},
|
2013-05-08 23:22:01 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
start: function( event, ui ) {
|
2013-07-11 11:14:14 +02:00
|
|
|
this.model.set({ scrubbing: true });
|
|
|
|
|
2013-07-06 00:54:17 +02:00
|
|
|
// Track the mouse position to enable smooth dragging,
|
2013-07-06 22:22:02 +02:00
|
|
|
// overrides default jQuery UI step behavior.
|
2013-07-11 11:14:14 +02:00
|
|
|
$( window ).on( 'mousemove.wp.revisions', { view: this }, function( e ) {
|
|
|
|
var view = e.data.view,
|
2013-07-06 22:22:02 +02:00
|
|
|
leftDragBoundary = view.$el.offset().left, // Initial left boundary
|
|
|
|
sliderOffset = leftDragBoundary,
|
|
|
|
sliderRightEdge = leftDragBoundary + view.$el.width(),
|
|
|
|
rightDragBoundary = sliderRightEdge, // Initial right boundary
|
|
|
|
leftDragReset = 0, // Initial left drag reset
|
|
|
|
rightDragReset = sliderRightEdge - sliderOffset; // Initial right drag reset
|
|
|
|
|
|
|
|
// In two handle mode, ensure handles can't be dragged past each other.
|
|
|
|
// Adjust left/right boundaries and reset points.
|
2013-07-11 11:14:14 +02:00
|
|
|
if ( view.model.frame.get('compareTwoMode') ) {
|
2013-07-09 09:33:15 +02:00
|
|
|
var rightHandle = $( ui.handle ).parent().find('.right-handle'),
|
|
|
|
leftHandle = $( ui.handle ).parent().find('.left-handle');
|
2013-07-06 22:22:02 +02:00
|
|
|
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( $( ui.handle ).hasClass('left-handle') ) {
|
2013-07-06 22:22:02 +02:00
|
|
|
// Dragging the left handle, boundary is right handle.
|
|
|
|
// RTL mode calculations reverse directions.
|
|
|
|
if ( isRtl ) {
|
|
|
|
leftDragBoundary = rightHandle.offset().left + rightHandle.width();
|
|
|
|
leftDragReset = leftDragBoundary - sliderOffset;
|
|
|
|
} else {
|
|
|
|
rightDragBoundary = rightHandle.offset().left;
|
|
|
|
rightDragReset = rightDragBoundary - sliderOffset;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Dragging the right handle, boundary is the left handle.
|
|
|
|
// RTL mode calculations reverse directions.
|
|
|
|
if ( isRtl ) {
|
|
|
|
rightDragBoundary = leftHandle.offset().left;
|
|
|
|
rightDragReset = rightDragBoundary - sliderOffset;
|
|
|
|
} else {
|
|
|
|
leftDragBoundary = leftHandle.offset().left + leftHandle.width() ;
|
|
|
|
leftDragReset = leftDragBoundary - sliderOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-06 00:54:17 +02:00
|
|
|
|
|
|
|
// Follow mouse movements, as long as handle remains inside slider.
|
2013-07-06 22:22:02 +02:00
|
|
|
if ( e.clientX < leftDragBoundary ) {
|
|
|
|
$( ui.handle ).css( 'left', leftDragReset ); // Mouse to left of slider.
|
|
|
|
} else if ( e.clientX > rightDragBoundary ) {
|
|
|
|
$( ui.handle ).css( 'left', rightDragReset ); // Mouse to right of slider.
|
2013-07-06 00:54:17 +02:00
|
|
|
} else {
|
2013-07-06 22:22:02 +02:00
|
|
|
$( ui.handle ).css( 'left', e.clientX - sliderOffset ); // Mouse in slider.
|
2013-07-06 00:54:17 +02:00
|
|
|
}
|
|
|
|
} );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-07-11 11:14:14 +02:00
|
|
|
// Responds to slide events
|
2013-06-26 23:06:50 +02:00
|
|
|
slide: function( event, ui ) {
|
2013-07-03 22:27:19 +02:00
|
|
|
var attributes;
|
|
|
|
// Compare two revisions mode
|
2013-07-11 11:14:14 +02:00
|
|
|
if ( ! _.isUndefined( ui.values ) && this.model.frame.get('compareTwoMode') ) {
|
2013-07-03 22:27:19 +02:00
|
|
|
// Prevent sliders from occupying same spot
|
|
|
|
if ( ui.values[1] === ui.values[0] )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
attributes = {
|
2013-07-06 12:48:14 +02:00
|
|
|
to: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[0] - 1 : ui.values[1] ), // Reverse directions for RTL.
|
|
|
|
from: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[1] - 1 : ui.values[0] ) // Reverse directions for RTL.
|
2013-07-03 22:27:19 +02:00
|
|
|
};
|
2013-07-06 12:48:14 +02:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
} else {
|
|
|
|
// Compare single revision mode
|
|
|
|
var sliderPosition = this.getSliderPosition( ui );
|
|
|
|
attributes = {
|
|
|
|
to: this.model.revisions.at( sliderPosition )
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we're at the first revision, unset 'from'.
|
|
|
|
if ( sliderPosition ) // Reverse directions for RTL.
|
|
|
|
attributes.from = this.model.revisions.at( sliderPosition - 1 );
|
|
|
|
else
|
2013-07-11 11:14:14 +02:00
|
|
|
attributes.from = undefined;
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
this.model.set( attributes );
|
2013-07-11 11:14:14 +02:00
|
|
|
|
|
|
|
// If we are scrubbing, a scrub to a revision is considered a hover
|
|
|
|
if ( this.model.get( 'scrubbing' ) ) {
|
|
|
|
this.model.set({
|
|
|
|
'hoveredRevision': attributes.to
|
|
|
|
});
|
|
|
|
}
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
stop: function( event, ui ) {
|
2013-07-11 11:14:14 +02:00
|
|
|
$( window ).off('mousemove.wp.revisions');
|
|
|
|
this.updateSliderSettings();
|
|
|
|
this.model.set({ scrubbing: false });
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
2013-02-28 16:14:34 +01:00
|
|
|
});
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// The diff view.
|
|
|
|
// This is the view for the current active diff.
|
|
|
|
revisions.view.Diff = wp.Backbone.View.extend({
|
|
|
|
className: 'revisions-diff',
|
|
|
|
template: wp.template('revisions-diff'),
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Generate the options to be passed to the template.
|
|
|
|
prepare: function() {
|
|
|
|
return _.extend({ fields: this.model.fields.toJSON() }, this.options );
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
// The revisions router
|
|
|
|
// takes URLs with #hash fragments and routes them
|
2013-07-09 10:15:55 +02:00
|
|
|
revisions.Router = Backbone.Router.extend({
|
2013-07-04 10:30:36 +02:00
|
|
|
initialize: function( options ) {
|
|
|
|
this.model = options.model;
|
2013-07-12 00:56:48 +02:00
|
|
|
this.routes = this.getRoutes();
|
2013-07-04 10:30:36 +02:00
|
|
|
|
|
|
|
// Maintain state history when dragging
|
2013-07-09 11:57:58 +02:00
|
|
|
this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
|
2013-07-04 10:30:36 +02:00
|
|
|
},
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-12 00:56:48 +02:00
|
|
|
getRoutes: function() {
|
|
|
|
var routes = {};
|
|
|
|
routes[this.baseUrl( '?from=:from&to=:to' )] = 'handleRoute';
|
|
|
|
routes[this.baseUrl( '?revision=:to' )] = 'handleRoute';
|
|
|
|
return routes;
|
|
|
|
},
|
|
|
|
|
|
|
|
baseUrl: function( url ) {
|
|
|
|
return this.model.get('baseUrl') + url;
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-09 10:14:38 +02:00
|
|
|
updateUrl: function() {
|
|
|
|
var from = this.model.has('from') ? this.model.get('from').id : 0;
|
|
|
|
var to = this.model.get('to').id;
|
2013-07-10 08:08:56 +02:00
|
|
|
if ( this.model.get('compareTwoMode' ) )
|
2013-07-12 00:56:48 +02:00
|
|
|
this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) );
|
2013-07-10 08:08:56 +02:00
|
|
|
else
|
2013-07-12 00:56:48 +02:00
|
|
|
this.navigate( this.baseUrl( '?revision=' + to ) );
|
2013-07-09 10:14:38 +02:00
|
|
|
},
|
2013-07-04 10:30:36 +02:00
|
|
|
|
2013-07-10 08:08:56 +02:00
|
|
|
handleRoute: function( a, b ) {
|
|
|
|
var from, to, compareTwo;
|
|
|
|
|
2013-07-10 21:17:01 +02:00
|
|
|
// If `b` is undefined, this is an 'at/:to' route, for a single revision
|
2013-07-10 08:08:56 +02:00
|
|
|
if ( _.isUndefined( b ) ) {
|
2013-07-11 11:14:14 +02:00
|
|
|
b = this.model.revisions.get( a );
|
|
|
|
a = this.model.revisions.prev( b );
|
|
|
|
b = b ? b.id : 0;
|
2013-07-12 00:56:48 +02:00
|
|
|
a = a ? a.id : 0;
|
2013-07-10 08:08:56 +02:00
|
|
|
compareTwo = false;
|
2013-07-10 21:17:01 +02:00
|
|
|
} else {
|
|
|
|
compareTwo = true;
|
2013-07-10 08:08:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
from = parseInt( a, 10 );
|
|
|
|
to = parseInt( b, 10 );
|
2013-07-09 09:55:50 +02:00
|
|
|
|
2013-07-10 08:08:56 +02:00
|
|
|
this.model.set({ compareTwoMode: compareTwo });
|
2013-07-03 22:27:19 +02:00
|
|
|
|
2013-07-10 08:08:56 +02:00
|
|
|
if ( ! _.isUndefined( this.model ) ) {
|
2013-07-11 11:14:14 +02:00
|
|
|
var selectedToRevision = this.model.revisions.get( to ),
|
|
|
|
selectedFromRevision = this.model.revisions.get( from );
|
2013-07-09 09:33:15 +02:00
|
|
|
|
|
|
|
this.model.set({
|
|
|
|
to: selectedToRevision,
|
|
|
|
from: selectedFromRevision
|
|
|
|
});
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-07-12 00:56:48 +02:00
|
|
|
revisions.settings.to = to;
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Initialize the revisions UI.
|
|
|
|
revisions.init = function() {
|
|
|
|
revisions.view.frame = new revisions.view.Frame({
|
2013-07-12 07:11:56 +02:00
|
|
|
model: new revisions.model.FrameState({}, {
|
|
|
|
revisions: new revisions.model.Revisions( revisions.settings.revisionData )
|
|
|
|
})
|
2013-06-26 23:06:50 +02:00
|
|
|
}).render();
|
|
|
|
};
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
$( revisions.init );
|
2013-02-28 16:14:34 +01:00
|
|
|
}(jQuery));
|