PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/js/revisions.js

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