X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/e08b42e8ad054ec67522d7ac1aaae5dc68cb3d01..HEAD:/wp-admin/js/revisions.js diff --git a/wp-admin/js/revisions.js b/wp-admin/js/revisions.js index 26b22cf2..5d3fe906 100644 --- a/wp-admin/js/revisions.js +++ b/wp-admin/js/revisions.js @@ -1,17 +1,31 @@ -/* global _wpRevisionsSettings, isRtl */ +/* global isRtl */ +/** + * @file Revisions interface functions, Backbone classes and + * the revisions.php document.ready bootstrap. + * + */ + window.wp = window.wp || {}; (function($) { var revisions; - + /** + * Expose the module in window.wp.revisions. + */ revisions = wp.revisions = { model: {}, view: {}, controller: {} }; - // Link settings. - revisions.settings = _.isUndefined( _wpRevisionsSettings ) ? {} : _wpRevisionsSettings; + // Link post revisions data served from the back end. + revisions.settings = window._wpRevisionsSettings || {}; // For debugging revisions.debug = false; + /** + * wp.revisions.log + * + * A debugging utility for revisions. Works only when a + * debug flag is on and the browser supports it. + */ revisions.log = function() { if ( window.console && revisions.debug ) { window.console.log.apply( window.console, arguments ); @@ -35,19 +49,6 @@ window.wp = window.wp || {}; }); }; - // wp_localize_script transforms top-level numbers into strings. Undo that. - if ( revisions.settings.to ) { - revisions.settings.to = parseInt( revisions.settings.to, 10 ); - } - if ( revisions.settings.from ) { - revisions.settings.from = parseInt( revisions.settings.from, 10 ); - } - - // wp_localize_script does not allow for top-level booleans. Fix that. - if ( revisions.settings.compareTwoMode ) { - revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1'; - } - /** * ======================================================================== * MODELS @@ -73,13 +74,13 @@ window.wp = window.wp || {}; this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode ); // Listen for internal changes - this.listenTo( this, 'change:from', this.handleLocalChanges ); - this.listenTo( this, 'change:to', this.handleLocalChanges ); - this.listenTo( this, 'change:compareTwoMode', this.updateSliderSettings ); - this.listenTo( this, 'update:revisions', this.updateSliderSettings ); + this.on( 'change:from', this.handleLocalChanges ); + this.on( 'change:to', this.handleLocalChanges ); + this.on( 'change:compareTwoMode', this.updateSliderSettings ); + this.on( 'update:revisions', this.updateSliderSettings ); // Listen for changes to the hovered revision - this.listenTo( this, 'change:hoveredRevision', this.hoverRevision ); + this.on( 'change:hoveredRevision', this.hoverRevision ); this.set({ max: this.revisions.length - 1, @@ -179,6 +180,11 @@ window.wp = window.wp || {}; revisions.model.Revision = Backbone.Model.extend({}); + /** + * wp.revisions.model.Revisions + * + * A collection of post revisions. + */ revisions.model.Revisions = Backbone.Collection.extend({ model: revisions.model.Revision, @@ -223,6 +229,7 @@ window.wp = window.wp || {}; _.bindAll( this, 'getClosestUnloaded' ); this.loadAll = _.once( this._loadAll ); this.revisions = options.revisions; + this.postId = options.postId; this.requests = {}; }, @@ -320,7 +327,7 @@ window.wp = window.wp || {}; options.context = this; options.data = _.extend( options.data || {}, { action: 'get-revision-diffs', - post_id: revisions.settings.postId + post_id: this.postId }); var deferred = wp.ajax.send( options ), @@ -352,6 +359,17 @@ window.wp = window.wp || {}; }); + /** + * wp.revisions.model.FrameState + * + * The frame state. + * + * @see wp.revisions.view.Frame + * + * @param {object} attributes Model attributes - none are required. + * @param {object} options Options for the model. + * @param {revisions.model.Revisions} options.revisions A collection of revisions. + */ revisions.model.FrameState = Backbone.Model.extend({ defaults: { loading: false, @@ -360,16 +378,19 @@ window.wp = window.wp || {}; }, initialize: function( attributes, options ) { - var properties = {}; - + var state = this.get( 'initialDiffState' ); _.bindAll( this, 'receiveDiff' ); this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 ); this.revisions = options.revisions; - this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions }); - // Set the initial diffs collection provided through the settings - this.diffs.set( revisions.settings.diffData ); + this.diffs = new revisions.model.Diffs( [], { + revisions: this.revisions, + postId: this.get( 'postId' ) + } ); + + // Set the initial diffs collection. + this.diffs.set( this.get( 'diffData' ) ); // Set up internal listeners this.listenTo( this, 'change:from', this.changeRevisionHandler ); @@ -379,12 +400,13 @@ window.wp = window.wp || {}; this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus ); this.listenTo( this, 'update:diff', this.updateLoadingStatus ); - // Set the initial revisions, baseUrl, and mode as provided through settings - properties.to = this.revisions.get( revisions.settings.to ); - properties.from = this.revisions.get( revisions.settings.from ); - properties.compareTwoMode = revisions.settings.compareTwoMode; - properties.baseUrl = revisions.settings.baseUrl; - this.set( properties ); + // Set the initial revisions, baseUrl, and mode as provided through attributes. + + this.set( { + to : this.revisions.get( state.to ), + from : this.revisions.get( state.from ), + compareTwoMode : state.compareTwoMode + } ); // Start the router if browser supports History API if ( window.history && window.history.pushState ) { @@ -399,11 +421,23 @@ window.wp = window.wp || {}; }, changeMode: function( model, value ) { - // If we were on the first revision before switching, we have to bump them over one - if ( value && 0 === this.revisions.indexOf( this.get('to') ) ) { + var toIndex = this.revisions.indexOf( this.get( 'to' ) ); + + // If we were on the first revision before switching to two-handled mode, + // bump the 'to' position over one + if ( value && 0 === toIndex ) { this.set({ - from: this.revisions.at(0), - to: this.revisions.at(1) + from: this.revisions.at( toIndex ), + to: this.revisions.at( toIndex + 1 ) + }); + } + + // When switching back to single-handled mode, reset 'from' model to + // one position before the 'to' model + if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode + this.set({ + from: this.revisions.at( toIndex - 1 ), + to: this.revisions.at( toIndex ) }); } }, @@ -487,7 +521,14 @@ window.wp = window.wp || {}; * ======================================================================== */ - // The frame view. This contains the entire page. + /** + * wp.revisions.view.Frame + * + * Top level frame that orchestrates the revisions experience. + * + * @param {object} options The options hash for the view. + * @param {revisions.model.FrameState} options.model The frame state model. + */ revisions.view.Frame = wp.Backbone.View.extend({ className: 'revisions', template: wp.template('revisions-frame'), @@ -534,8 +575,13 @@ window.wp = window.wp || {}; } }); - // The control view. - // This contains the revision slider, previous/next buttons, the meta info and the compare checkbox. + /** + * wp.revisions.view.Controls + * + * The controls view. + * + * Contains the revision slider, previous/next buttons, the meta info and the compare checkbox. + */ revisions.view.Controls = wp.Backbone.View.extend({ className: 'revisions-controls', @@ -1048,15 +1094,12 @@ window.wp = window.wp || {}; } }); - // The revisions router - // takes URLs with #hash fragments and routes them + // The revisions router. + // Maintains the URL routes so browser URL matches state. revisions.Router = Backbone.Router.extend({ initialize: function( options ) { this.model = options.model; - this.routes = _.object([ - [ this.baseUrl( '?from=:from&to=:to' ), 'handleRoute' ], - [ this.baseUrl( '?from=:from&to=:to' ), 'handleRoute' ] - ]); + // Maintain state and history when navigating this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) ); this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl ); @@ -1070,9 +1113,9 @@ window.wp = window.wp || {}; var from = this.model.has('from') ? this.model.get('from').id : 0, to = this.model.get('to').id; if ( this.model.get('compareTwoMode' ) ) { - this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) ); + this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ), { replace: true } ); } else { - this.navigate( this.baseUrl( '?revision=' + to ) ); + this.navigate( this.baseUrl( '?revision=' + to ), { replace: true } ); } }, @@ -1085,21 +1128,37 @@ window.wp = window.wp || {}; b = b ? b.id : 0; a = a ? a.id : 0; } - - this.model.set({ - from: this.model.revisions.get( parseInt( a, 10 ) ), - to: this.model.revisions.get( parseInt( a, 10 ) ), - compareTwoMode: compareTwo - }); } }); - // Initialize the revisions UI. + /** + * Initialize the revisions UI for revision.php. + */ revisions.init = function() { + var state; + + // Bail if the current page is not revision.php. + if ( ! window.adminpage || 'revision-php' !== window.adminpage ) { + return; + } + + state = new revisions.model.FrameState({ + initialDiffState: { + // wp_localize_script doesn't stringifies ints, so cast them. + to: parseInt( revisions.settings.to, 10 ), + from: parseInt( revisions.settings.from, 10 ), + // wp_localize_script does not allow for top-level booleans so do a comparator here. + compareTwoMode: ( revisions.settings.compareTwoMode === '1' ) + }, + diffData: revisions.settings.diffData, + baseUrl: revisions.settings.baseUrl, + postId: parseInt( revisions.settings.postId, 10 ) + }, { + revisions: new revisions.model.Revisions( revisions.settings.revisionData ) + }); + revisions.view.frame = new revisions.view.Frame({ - model: new revisions.model.FrameState({}, { - revisions: new revisions.model.Revisions( revisions.settings.revisionData ) - }) + model: state }).render(); };