revisions.js 33 KB

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