PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/revisions.js

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