revisions.js 33 KB

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