[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-admin/js/ -> revisions.js (source)

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


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1