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-03 22:27:19 +02:00
|
|
|
revisions = wp.revisions = { model: {}, view: {}, controller: {}, router: {} };
|
2013-04-04 09:53:49 +02:00
|
|
|
|
2013-05-02 12:12:19 +02:00
|
|
|
// Link settings.
|
2013-06-26 23:06:50 +02:00
|
|
|
revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings;
|
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,
|
|
|
|
step: 1
|
|
|
|
}
|
|
|
|
});
|
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,
|
|
|
|
position: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-06-26 23:06:50 +02:00
|
|
|
comparator: function( revision ) {
|
|
|
|
return revision.id;
|
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 ) {
|
|
|
|
comparisons = _.object( comparisons, comparisons );
|
|
|
|
_.each( comparisons, _.bind( function( id ) {
|
|
|
|
// Exists
|
|
|
|
if ( this.get( id ) )
|
|
|
|
delete comparisons[ id ];
|
|
|
|
}, this ) );
|
|
|
|
comparisons = _.toArray( comparisons );
|
|
|
|
return this.load( comparisons );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
load: function( comparisons ) {
|
|
|
|
// 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 ) {
|
|
|
|
num = num || 1;
|
|
|
|
var ids = this.getProximalDiffIds();
|
|
|
|
ids = _.last( ids, num );
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
if ( ids.length ) {
|
|
|
|
return this.loadNew( ids );
|
|
|
|
}
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
loadLastUnloaded: function( num ) {
|
|
|
|
num = num || 1;
|
|
|
|
var ids = this.getUnloadedProximalDiffIds();
|
|
|
|
ids = _.last( ids, num );
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
if ( ids.length ) {
|
|
|
|
return this.loadNew( ids );
|
|
|
|
}
|
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-06-26 23:06:50 +02:00
|
|
|
var deferred = wp.xhr.send( options );
|
|
|
|
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({
|
|
|
|
initialize: function( attributes, options ) {
|
|
|
|
this.revisions = options.revisions;
|
2013-07-09 09:07:53 +02:00
|
|
|
this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions });
|
|
|
|
this.listenTo( this, 'change:from', this.updateDiff );
|
|
|
|
this.listenTo( this, 'change:to', this.updateDiff );
|
2013-07-04 10:30:36 +02:00
|
|
|
this.revisionsRouter = new revisions.router.Router({ model: this });
|
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.
|
|
|
|
updateDiff: function() {
|
|
|
|
var from = this.get('from');
|
2013-07-04 10:30:36 +02:00
|
|
|
this.set( 'diffId', (from ? from.id : '0' ) + ':' + this.get('to').id );
|
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() {
|
|
|
|
this.model = new revisions.model.FrameState({}, {
|
|
|
|
revisions: this.collection
|
2013-04-04 09:53:49 +02:00
|
|
|
});
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.listenTo( this.model, 'change:diffId', this.updateDiff );
|
2013-07-06 22:22:02 +02:00
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
|
|
|
|
|
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-06-26 23:06:50 +02:00
|
|
|
if ( this.model.revisions.length ) {
|
|
|
|
var last = this.model.revisions.last(2);
|
|
|
|
var attributes = { to: last.pop() };
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
if ( last.length )
|
|
|
|
attributes.from = last.pop();
|
|
|
|
|
|
|
|
this.model.set( attributes );
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Load the rest: first 10, then the rest by 50
|
|
|
|
this.model.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
|
|
|
|
this.model.diffs.loadAllBy( 50 );
|
|
|
|
}, this ) );
|
2013-04-04 09:53:49 +02:00
|
|
|
}
|
2013-07-03 22:27:19 +02:00
|
|
|
Backbone.history.start();
|
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() {
|
|
|
|
wp.Backbone.View.prototype.render.apply( this, arguments );
|
2013-03-21 16:54:11 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
$('#wpbody-content .wrap').append( this.el );
|
|
|
|
this.views.ready();
|
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-06-26 23:06:50 +02:00
|
|
|
updateDiff: function() {
|
|
|
|
this.model.diffs.ensure( this.model.get('diffId'), this ).done( function( diff ) {
|
|
|
|
if ( this.model.get('diffId') !== diff.id )
|
|
|
|
return;
|
2013-07-09 09:33:15 +02:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
|
|
|
|
model: diff
|
2013-07-09 09:33:15 +02:00
|
|
|
}) );
|
|
|
|
|
|
|
|
this.model.trigger('renderDiff');
|
2013-06-26 23:06:50 +02:00
|
|
|
});
|
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
|
|
|
|
|
|
|
// Add the tooltip view
|
2013-07-05 19:44:47 +02:00
|
|
|
var tooltip = new revisions.view.Tooltip({
|
|
|
|
model: new revisions.model.Tooltip()
|
|
|
|
});
|
|
|
|
this.views.add( tooltip );
|
|
|
|
|
2013-07-08 23:37:03 +02:00
|
|
|
// Add the Tickmarks view
|
|
|
|
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-05 19:44:47 +02:00
|
|
|
// Add the Slider view with a reference to the tooltip view
|
2013-06-26 23:06:50 +02:00
|
|
|
this.views.add( new revisions.view.Slider({
|
2013-07-05 19:44:47 +02:00
|
|
|
model: this.model,
|
|
|
|
tooltip: tooltip
|
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',
|
|
|
|
template: wp.template('revisions-tickmarks'),
|
|
|
|
|
|
|
|
numberOfTickmarksSet: function() {
|
|
|
|
var tickCount = this.model.revisions.length - 1, // One tickmark per model
|
2013-07-09 09:33:15 +02:00
|
|
|
sliderWidth = $('.wp-slider').parent().width() * 0.7, // Width of slider is 70% of container (reset on resize)
|
2013-07-08 23:37:03 +02:00
|
|
|
tickWidth = Math.floor( sliderWidth / tickCount ), // Divide width by # of tickmarks, round down
|
|
|
|
newSiderWidth = ( ( tickWidth + 1 ) * tickCount ) + 1, // Calculate the actual width
|
|
|
|
tickNumber;
|
|
|
|
|
2013-07-09 09:33:15 +02:00
|
|
|
$('.wp-slider').css( 'width', newSiderWidth ); // Reset the slider width to match the calculated tick size
|
2013-07-08 23:37:03 +02:00
|
|
|
this.$el.css( 'width', newSiderWidth ); // Match the tickmark div width
|
|
|
|
|
|
|
|
for ( tickNumber = 0; tickNumber <= tickCount; tickNumber++ ){
|
2013-07-09 09:33:15 +02:00
|
|
|
this.$el.append('<div style="left:' + ( tickWidth * tickNumber ) + 'px;"></div>');
|
2013-07-08 23:37:03 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
|
|
|
var self = this;
|
|
|
|
self.numberOfTickmarksSet();
|
|
|
|
$( window ).on( 'resize', _.debounce( function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
self.$el.html('');
|
2013-07-08 23:37:03 +02:00
|
|
|
self.numberOfTickmarksSet();
|
2013-07-09 09:33:15 +02:00
|
|
|
}, 50 ) );
|
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: {
|
|
|
|
'click #restore-revision': 'restoreRevision'
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.listenTo( this.model, 'change:diffId', this.updateMeta );
|
|
|
|
},
|
|
|
|
|
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-06-26 23:06:50 +02:00
|
|
|
updateMeta: function() {
|
|
|
|
this.$el.html( this.template( this.model.toJSON() ) );
|
2013-07-06 22:22:02 +02:00
|
|
|
|
2013-07-09 09:33:15 +02:00
|
|
|
$('#restore-revision').prop( 'disabled', this.model.get('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-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
|
|
|
$('.compare-two-revisions').prop( 'checked', true );
|
2013-07-03 22:27:19 +02:00
|
|
|
// in RTL mode the 'left handle' is the second in the slider, 'right' is first
|
2013-07-09 09:33:15 +02:00
|
|
|
$('.wp-slider a.ui-slider-handle').first().addClass( isRtl ? 'right-handle' : 'left-handle' );
|
|
|
|
$('.wp-slider a.ui-slider-handle').last().addClass( isRtl ? 'left-handle' : 'right-handle' );
|
2013-07-03 22:27:19 +02:00
|
|
|
} else {
|
2013-07-09 09:33:15 +02:00
|
|
|
$('.compare-two-revisions').prop( 'checked', false );
|
|
|
|
$('.wp-slider a.ui-slider-handle').removeClass('left-handle').removeClass('right-handle');
|
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 09:33:15 +02:00
|
|
|
this.model.revisionsRouter.navigateRoute( this.model.get('to').id, this.model.get('from').id );
|
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-06-26 23:06:50 +02:00
|
|
|
initialize: function() {
|
2013-07-05 19:44:47 +02:00
|
|
|
this.listenTo( this.model, 'change', this.render );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
2013-07-05 19:44:47 +02:00
|
|
|
// Hide tooltip on start.
|
2013-07-09 09:33:15 +02:00
|
|
|
this.$el.addClass('hidden');
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
show: function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
this.$el.removeClass('hidden');
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
hide: function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
this.$el.addClass('hidden');
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
render: function() {
|
|
|
|
// Check if a revision exists.
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( null === 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-09 09:33:15 +02:00
|
|
|
var offset = $('.revisions-buttons').offset().left;
|
|
|
|
this.$el.css( 'left', this.model.get('position') - 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() );
|
|
|
|
},
|
|
|
|
|
|
|
|
ready: function() {
|
|
|
|
this.listenTo( this.model, 'change:diffId', this.disabledButtonCheck );
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
this.model.revisionsRouter.navigateRoute( attributes.to.id, attributes.from ? attributes.from.id : 0 );
|
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.
|
|
|
|
// Encapsulates all of the configuration for the jQuery UI slider into a view.
|
|
|
|
revisions.view.Slider = wp.Backbone.View.extend({
|
|
|
|
className: 'wp-slider',
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
events: {
|
|
|
|
'mousemove' : 'mousemove',
|
2013-07-08 23:37:03 +02:00
|
|
|
'mouseleave' : 'mouseleave',
|
|
|
|
'mouseenter' : 'mouseenter'
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
initialize: function( options ) {
|
2013-06-26 23:06:50 +02:00
|
|
|
_.bindAll( this, 'start', 'slide', 'stop' );
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
this.tooltip = options.tooltip;
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Create the slider model from the provided collection data.
|
|
|
|
var latestRevisionIndex = this.model.revisions.length - 1;
|
|
|
|
|
|
|
|
// Find the initially selected revision
|
|
|
|
var initiallySelectedRevisionIndex =
|
2013-07-03 22:27:19 +02:00
|
|
|
this.model.revisions.indexOf(
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.revisions.findWhere({ id: Number( revisions.settings.selectedRevision ) }) );
|
2013-06-26 23:06:50 +02:00
|
|
|
|
|
|
|
this.settings = new revisions.model.Slider({
|
|
|
|
max: latestRevisionIndex,
|
|
|
|
value: initiallySelectedRevisionIndex,
|
|
|
|
start: this.start,
|
|
|
|
slide: this.slide,
|
|
|
|
stop: this.stop
|
|
|
|
});
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
ready: function() {
|
2013-07-05 19:44:47 +02:00
|
|
|
// Refresh the currently selected revision position in case router has set it.
|
|
|
|
this.settings.attributes.value = this.model.revisions.indexOf(
|
2013-07-09 09:33:15 +02:00
|
|
|
this.model.revisions.findWhere({ id: Number( revisions.settings.selectedRevision ) }) );
|
2013-07-05 19:44:47 +02:00
|
|
|
|
2013-07-06 12:48:14 +02:00
|
|
|
// And update the slider in case the route has set it.
|
|
|
|
this.updateSliderSettings();
|
2013-07-05 19:44:47 +02:00
|
|
|
this.slide( '', this.settings.attributes );
|
2013-06-26 23:06:50 +02:00
|
|
|
this.$el.slider( this.settings.toJSON() );
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
// Listen for changes in Compare Two Mode setting
|
|
|
|
this.listenTo( this.model, 'change:compareTwoMode', this.updateSliderSettings );
|
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
this.settings.on( 'change', function() {
|
2013-07-03 22:27:19 +02:00
|
|
|
this.updateSliderSettings();
|
2013-06-26 23:06:50 +02:00
|
|
|
}, this );
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
// Listen for changes in the diffId
|
|
|
|
this.listenTo( this.model, 'change:diffId', this.diffIdChanged );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
mousemove: function( e ) {
|
2013-07-08 23:37:03 +02:00
|
|
|
var tickCount = this.model.revisions.length - 1, // One tickmark per model
|
|
|
|
sliderLeft = Math.ceil( this.$el.offset().left ), // Left edge of slider
|
|
|
|
sliderWidth = this.$el.width(), // Width of slider
|
|
|
|
tickWidth = Math.floor( sliderWidth / tickCount ), // Calculated width of tickmark
|
|
|
|
actualX = e.clientX - sliderLeft, // Offset of mouse position in slider
|
|
|
|
currentModelIndex = Math.floor( ( actualX + tickWidth / 2 ) / tickWidth ), // Calculate the model index
|
|
|
|
tooltipPosition = sliderLeft + 2 + currentModelIndex * tickWidth; // Stick tooltip to tickmark
|
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-08 23:37:03 +02:00
|
|
|
this.tooltip.model.set( 'revision', this.model.revisions.at( currentModelIndex ) );
|
|
|
|
this.tooltip.model.set( 'position', tooltipPosition );
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
mouseleave: function( e ) {
|
2013-07-05 19:44:47 +02:00
|
|
|
this.tooltip.hide();
|
2013-07-03 22:27:19 +02:00
|
|
|
},
|
|
|
|
|
2013-07-08 23:37:03 +02:00
|
|
|
mouseenter: function( e ) {
|
|
|
|
this.tooltip.show();
|
|
|
|
},
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
updateSliderSettings: function() {
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
2013-07-06 12:48:14 +02:00
|
|
|
var leftValue, rightValue;
|
|
|
|
|
|
|
|
// In single handle mode, the 1st stored revision is 'blank' and the 'from' model is not set
|
|
|
|
// In this case we move the to index over one
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( 'undefined' == typeof this.model.get('from') ) {
|
2013-07-06 12:48:14 +02:00
|
|
|
if ( isRtl ) {
|
2013-07-09 09:33:15 +02:00
|
|
|
leftValue = this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 2;
|
2013-07-06 12:48:14 +02:00
|
|
|
rightValue = leftValue + 1;
|
|
|
|
} else {
|
2013-07-09 09:33:15 +02:00
|
|
|
leftValue = this.model.revisions.indexOf( this.model.get('to') );
|
2013-07-06 12:48:14 +02:00
|
|
|
rightValue = leftValue + 1;
|
|
|
|
}
|
2013-07-03 22:27:19 +02:00
|
|
|
} else {
|
2013-07-09 09:33:15 +02:00
|
|
|
leftValue = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 :
|
|
|
|
this.model.revisions.indexOf( this.model.get('from') ),
|
|
|
|
rightValue = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('from') ) - 1 :
|
|
|
|
this.model.revisions.indexOf( this.model.get('to') );
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
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
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.$el.slider( { // Set handle to current to model
|
|
|
|
// Reverse order for RTL.
|
2013-07-09 09:33:15 +02:00
|
|
|
value: isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get('to') ) - 1 :
|
|
|
|
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-03 22:27:19 +02:00
|
|
|
}
|
|
|
|
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ){
|
2013-07-03 22:27:19 +02:00
|
|
|
// in RTL mode the 'left handle' is the second in the slider, 'right' is first
|
|
|
|
$( 'a.ui-slider-handle', this.$el )
|
|
|
|
.first()
|
|
|
|
.addClass( isRtl ? 'right-handle' : 'left-handle' )
|
|
|
|
.removeClass( isRtl ? 'left-handle' : 'right-handle' );
|
|
|
|
$( 'a.ui-slider-handle', this.$el )
|
|
|
|
.last()
|
|
|
|
.addClass( isRtl ? 'left-handle' : 'right-handle' )
|
|
|
|
.removeClass( isRtl ? 'right-handle' : 'left-handle' );
|
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
},
|
2013-02-28 16:14:34 +01:00
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
diffIdChanged: function() {
|
|
|
|
// Reset the view settings when diffId is changed
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
|
|
|
this.settings.set({ 'values': [
|
|
|
|
this.model.revisions.indexOf( this.model.get('from') ),
|
|
|
|
this.model.revisions.indexOf( this.model.get('to') )
|
|
|
|
] });
|
2013-07-03 22:27:19 +02:00
|
|
|
} else {
|
2013-07-09 09:33:15 +02:00
|
|
|
this.settings.set({ 'value': this.model.revisions.indexOf( this.model.get('to') ) });
|
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-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.
|
|
|
|
$( window ).on( 'mousemove', { view: this }, function( e ) {
|
|
|
|
var view = e.data.view,
|
|
|
|
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-09 09:33:15 +02:00
|
|
|
if ( view.model.get('compareTwoMode') ) {
|
|
|
|
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-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-09 09:33:15 +02:00
|
|
|
if ( 'undefined' !== typeof ui.values && this.model.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-09 09:33:15 +02:00
|
|
|
this.model.unset( 'from', { silent: true });
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-06-26 23:06:50 +02:00
|
|
|
this.model.set( attributes );
|
2013-04-04 09:53:49 +02:00
|
|
|
},
|
|
|
|
|
2013-06-26 23:06:50 +02:00
|
|
|
stop: function( event, ui ) {
|
2013-07-09 09:33:15 +02:00
|
|
|
$( window ).off('mousemove');
|
2013-07-06 00:54:17 +02:00
|
|
|
|
2013-07-05 19:44:47 +02:00
|
|
|
// Reset settings props handle back to the step position.
|
2013-07-09 09:33:15 +02:00
|
|
|
this.settings.trigger('change');
|
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
|
|
|
|
revisions.router.Router = Backbone.Router.extend({
|
2013-07-04 10:30:36 +02:00
|
|
|
initialize: function( options ) {
|
|
|
|
this.model = options.model;
|
|
|
|
|
|
|
|
// Maintain state history when dragging
|
|
|
|
this.listenTo( this.model, 'renderDiff', this.updateURL );
|
|
|
|
},
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
routes: {
|
|
|
|
'revision/from/:from/to/:to/handles/:handles': 'gotoRevisionId'
|
|
|
|
},
|
|
|
|
|
|
|
|
navigateRoute: function( to, from ) {
|
|
|
|
var navigateTo = '/revision/from/' + from + '/to/' + to + '/handles/';
|
2013-07-09 09:33:15 +02:00
|
|
|
if ( this.model.get('compareTwoMode') ) {
|
2013-07-05 19:44:47 +02:00
|
|
|
navigateTo += '2';
|
2013-07-03 22:27:19 +02:00
|
|
|
} else {
|
2013-07-05 19:44:47 +02:00
|
|
|
navigateTo += '1';
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
|
|
|
this.navigate( navigateTo );
|
|
|
|
},
|
|
|
|
|
2013-07-04 10:30:36 +02:00
|
|
|
updateURL: _.debounce( function() {
|
|
|
|
var from = this.model.get('from');
|
|
|
|
this.navigateRoute( this.model.get('to').id, from ? from.id : 0 );
|
|
|
|
}, 250 ),
|
|
|
|
|
2013-07-03 22:27:19 +02:00
|
|
|
gotoRevisionId: function( from, to, handles ) {
|
2013-07-05 19:44:47 +02:00
|
|
|
this.model.set( { compareTwoMode: ( '2' === handles ) } );
|
2013-07-03 22:27:19 +02:00
|
|
|
|
|
|
|
if ( 'undefined' !== typeof this.model ) {
|
2013-07-09 09:33:15 +02:00
|
|
|
var selectedToRevision = this.model.revisions.findWhere({ 'id': Number( to ) }),
|
|
|
|
selectedFromRevision = this.model.revisions.findWhere({ 'id': Number( from ) });
|
|
|
|
|
|
|
|
this.model.set({
|
|
|
|
to: selectedToRevision,
|
|
|
|
from: selectedFromRevision
|
|
|
|
});
|
2013-07-03 22:27:19 +02:00
|
|
|
}
|
2013-07-05 19:44:47 +02:00
|
|
|
revisions.settings.selectedRevision = 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({
|
|
|
|
collection: new revisions.model.Revisions( revisions.settings.revisionData )
|
|
|
|
}).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));
|