Make WordPress Core

Ticket #24425: 24425.draft.20.diff

File 24425.draft.20.diff, 17.2 KB (added by adamsilverstein, 12 years ago)

after linting and cleanup

  • wp-admin/js/revisions.js

     
    33(function($) {
    44        var revisions;
    55
    6         revisions = wp.revisions = { model: {}, view: {}, controller: {} };
     6        revisions = wp.revisions = { model: {}, view: {}, controller: {}, router: {} };
    77
    88        // Link settings.
    99        revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings;
     
    3030
    3131                comparator: function( revision ) {
    3232                        return revision.id;
    33                 },
     33                }
    3434        });
    3535
    3636        revisions.model.Field = Backbone.Model.extend({});
     
    101101                        return this.fetch({ data: { compare: comparisons }, remove: false });
    102102                },
    103103
    104 /**/
    105104                loadLast: function( num ) {
    106105                        num     = num || 1;
    107106                        var ids = this.getProximalDiffIds();
     
    194193                initialize: function( attributes, options ) {
    195194                        this.revisions = options.revisions;
    196195                        this.diffs     = new revisions.model.Diffs( [], {revisions: this.revisions} );
     196                        this.listenTo( this, 'change:from', this.updateDiffFrom );
     197                        this.listenTo( this, 'change:to', this.updateDiffTo );
     198                        this.revisionsRouter = new revisions.router.Router();
     199                        this.revisionsRouter.model = this;
    197200
    198                         this.listenTo( this, 'change:from change:to', this.updateDiffId );
    199201                },
    200202
    201                 updateDiffId: function() {
     203                updateDiffTo: function() {
    202204                        var from = this.get( 'from' );
    203205                        var to   = this.get( 'to' );
    204                         this.set( 'diffId', (from ? from.id : '0') + ':' + to.id );
     206                        this.set( 'diffId', (from ? from.id : '0' ) + ':' + to.id );
     207                },
     208
     209                updateDiffFrom: function() {
     210                        if ( this.get( 'compareTwoMode' ) ) {
     211                                this.updateDiffTo();
     212                        }
    205213                }
     214
    206215        });
    207216
    208217
     
    243252                                        this.model.diffs.loadAllBy( 50 );
    244253                                }, this ) );
    245254                        }
     255                        Backbone.history.start();
    246256                },
    247257
    248258                render: function() {
     
    266276        });
    267277
    268278        // The control view.
    269         // This contains the revision slider, previous/next buttons, and the compare checkbox.
     279        // This contains the revision slider, previous/next buttons, the meta info and the compare checkbox.
    270280        revisions.view.Controls = wp.Backbone.View.extend({
    271281                tagName: 'div',
    272282                className: 'revisions-controls',
    273283
    274284                initialize: function() {
    275285                        // Add the button view
    276                         this.views.add( new revisions.view.Buttons({ 
     286                        this.views.add( new revisions.view.Buttons({
    277287                                model: this.model
    278288                        }));
    279289
     
    314324                        if( this.model.get( 'to' ).attributes.current ) {
    315325                                $( '#restore-revision' ).prop( 'disabled', true);
    316326                        } else {
    317                                 $( '#restore-revision' ).prop( 'disabled', false)
     327                                $( '#restore-revision' ).prop( 'disabled', false);
    318328                        }
    319329                }
    320330        });
    321331
    322 
    323332        // The buttons view.
    324         // Encapsulates all of the configuration for the previous/next buttons, and the compare checkbox.
     333        // Encapsulates all of the configuration for the previous / next buttons, and the compare check-box.
    325334        revisions.view.Buttons = wp.Backbone.View.extend({
    326335                tagName: 'div',
    327336                className: 'revisions-buttons',
    328337                template: wp.template('revisions-controls'),
    329338
    330339                initialize: function() {
    331                         this.$el.html( this.template() )
     340                        this.$el.html( this.template() );
     341                        this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode );
     342
    332343                },
    333344
    334345                events: {
    335346                        'click #next': 'nextRevision',
    336                         'click #previous': 'previousRevision'
     347                        'click #previous': 'previousRevision',
     348                        'click .compare-two-revisions': 'compareTwoToggle'
    337349                },
    338                
     350
     351                updateCompareTwoMode: function(){
     352                        if ( this.model.get( 'compareTwoMode' ) ){
     353                                $( '#revision-diff-container' ).addClass( 'comparing-two-revisions' );
     354                                // in RTL mode the 'left handle' is the second in the slider, 'right' is first
     355                                $( '.wp-slider a.ui-slider-handle' ).first().addClass( isRtl ? 'right-handle' : 'left-handle' );
     356                                $( '.wp-slider a.ui-slider-handle' ).last().addClass( isRtl ? 'left-handle' : 'right-handle' );
     357                        } else {
     358                                $( '#revision-diff-container' ).removeClass( 'comparing-two-revisions' );
     359                                $( '.wp-slider a.ui-slider-handle' ).removeClass( 'left-handle' ).removeClass( 'right-handle' );
     360                        }
     361
     362                },
     363
     364                // Toggle the compare two mode feature when the compare two check-box is checked.
     365                compareTwoToggle: function( event ){
     366                        // Activate compare two mode?
     367                        if ( $( '.compare-two-revisions' ).is( ':checked' ) ) {
     368                                this.model.set( { compareTwoMode: true } );
     369                        } else {
     370                                this.model.set( { compareTwoMode: false } );
     371                        }
     372                        this.model.revisionsRouter.navigateRoute( this.model.get( 'to').id, this.model.get( 'from' ).id );
     373
     374                },
     375
     376                // Go to a specific modelindex, taking into account Rtl mode.
    339377                gotoModel: function( toIndex ) {
    340378                        var attributes = {
    341                                 to: this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl
     379                                to: this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl.
    342380                        };
    343381                        // If we're at the first revision, unset 'from'.
    344382                        if ( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl
     
    347385                                this.model.unset('from', { silent: true });
    348386
    349387                        this.model.set( attributes );
     388
     389                        this.model.revisionsRouter.navigateRoute( attributes.to.id, attributes.from ? attributes.from.id : 0 );
     390
    350391                },
    351392
     393                // Go to the 'next' revision, direction takes into account Rtl mode.
    352394                nextRevision: function() {
    353                         var toIndex = this.model.revisions.indexOf( this.model.get( 'to' ) );
     395                        var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1 : this.model.revisions.indexOf( this.model.get( 'to' ) );
    354396                        toIndex     = isRtl ? toIndex - 1 : toIndex + 1;
    355397                        this.gotoModel( toIndex );
    356398                },
    357                
     399
     400                // Go to the 'previous' revision, direction takes into account Rtl mode.
    358401                previousRevision: function() {
    359                         var toIndex = this.model.revisions.indexOf( this.model.get('to') );
     402                        var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1 : this.model.revisions.indexOf( this.model.get( 'to' ) );
    360403                        toIndex     = isRtl ? toIndex + 1 : toIndex - 1;
    361404                        this.gotoModel( toIndex );
    362405                },
    363406
    364407                ready: function() {
    365408                        this.listenTo( this.model, 'change:diffId', this.disabledButtonCheck );
     409
     410                        // Hide compare two mode toggle when fewer than three revisions.
     411                        if ( this.model.revisions.length < 3 ) {
     412                                $( '.revision-toggle-compare-mode' ).hide();
     413                        }
    366414                },
    367415
    368                 // Check to see if the Previous or Next buttons need to be disabled or enabled
     416                // Check to see if the Previous or Next buttons need to be disabled or enabled.
    369417                disabledButtonCheck: function() {
    370                         var maxVal   = isRtl ? 0 : this.model.revisions.length - 1,
    371                                 minVal   = isRtl ? this.model.revisions.length - 1 : 0,
    372                                 next     = $( '.revisions-next .button' ),
     418                        var maxVal = this.model.revisions.length - 1,
     419                                minVal = 0,
     420                                next = $( '.revisions-next .button' ),
    373421                                previous = $( '.revisions-previous .button' ),
    374                                 val      = this.model.revisions.indexOf( this.model.get( 'to' ) );
     422                                val = this.model.revisions.indexOf( this.model.get( 'to' ) );
    375423
    376                         // Disable "Next" button if you're on the last node
     424                        // Disable "Next" button if you're on the last node.
    377425                        if ( maxVal === val )
    378426                                next.prop( 'disabled', true );
    379427                        else
    380428                                next.prop( 'disabled', false );
    381429
    382                         // Disable "Previous" button if you're on the first node
     430                        // Disable "Previous" button if you're on the first node.
    383431                        if ( minVal === val )
    384432                                previous.prop( 'disabled', true );
    385433                        else
    386434                                previous.prop( 'disabled', false );
    387                 },
     435                }
    388436
    389437
    390438        });
     
    404452
    405453                        // Find the initially selected revision
    406454                        var initiallySelectedRevisionIndex =
    407                                 this.model.revisions.indexOf( 
     455                                this.model.revisions.indexOf(
    408456                                        this.model.revisions.findWhere(  { id: Number( revisions.settings.selectedRevision ) } ) );
    409457
    410458                        this.settings = new revisions.model.Slider({
     
    418466
    419467                ready: function() {
    420468                        this.$el.slider( this.settings.toJSON() );
     469
     470                        // Listen for changes in Compare Two Mode setting
     471                        this.listenTo( this.model, 'change:compareTwoMode', this.updateSliderSettings );
     472
    421473                        this.settings.on( 'change', function( model, options ) {
    422                                 // Apply changes to slider settings here.
    423                                 this.$el.slider( { value: this.model.revisions.indexOf( this.model.get( 'to' ) ) } ); // Set handle to current to model
     474                                this.updateSliderSettings();
    424475                        }, this );
    425                         // Reset to the initially selected revision
    426                         this.slide( '', this.settings.attributes );
    427476
    428477                        // Listen for changes in the diffId
    429478                        this.listenTo( this.model, 'change:diffId', this.diffIdChanged );
    430479
     480                        // Reset to the initially selected revision
     481                        this.slide( '', this.settings.attributes );
     482
     483                        if ( this.model.get( 'compareTwoMode' ) )
     484                                $( '.compare-two-revisions' ).trigger( 'click' );
    431485                },
    432486
     487                updateSliderSettings: function() {
     488                        if ( isRtl ) {
     489                                this.$el.slider( { // Order reversed in Rtl mode
     490                                        value: this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1
     491                                } );
     492                        } else {
     493                                if ( this.model.get( 'compareTwoMode' ) ) {
     494                                        this.$el.slider( { // Set handles to current from/to models
     495                                                values: [
     496                                                        this.model.revisions.indexOf( this.model.get( 'from' ) ),
     497                                                        this.model.revisions.indexOf( this.model.get( 'to' ) )
     498                                                                ],
     499                                                value: null,
     500                                                range: true // Range mode ensures handles can't cross
     501                                        } );
     502                                } else {
     503                                        this.$el.slider( { // Set handle to current to model
     504                                                value: this.model.revisions.indexOf( this.model.get( 'to' ) ),
     505                                                values: null, // Clear existing two handled values
     506                                                range: false
     507                                        } );
     508                                }
     509                        }
     510                        if ( this.model.get( 'compareTwoMode' ) ){
     511                                $( '.revisions' ).addClass( 'comparing-two-revisions' );
     512                                // in RTL mode the 'left handle' is the second in the slider, 'right' is first
     513
     514                                $( '.wp-slider a.ui-slider-handle' )
     515                                        .first()
     516                                        .addClass( isRtl ? 'right-handle' : 'left-handle' )
     517                                        .removeClass( isRtl ? 'left-handle' : 'right-handle' );
     518                                $( '.wp-slider a.ui-slider-handle' )
     519                                        .last()
     520                                        .addClass( isRtl ? 'left-handle' : 'right-handle' )
     521                                        .removeClass( isRtl ? 'right-handle' : 'left-handle' );
     522                        } else {
     523                                $( '.revisions' ).removeClass( 'comparing-two-revisions' );
     524                        }
     525                },
     526
    433527                diffIdChanged: function() {
    434528                        // Reset the view settings when diffId is changed
    435                         this.settings.set( { 'value': this.model.revisions.indexOf( this.model.get( 'to' ) ) } );
     529                        if ( this.model.get( 'compareTwoMode' ) ) {
     530                                this.settings.set( { 'values': [
     531                                                this.model.revisions.indexOf( this.model.get( 'from' ) ),
     532                                                this.model.revisions.indexOf( this.model.get( 'to' ) )
     533                                        ] } );
     534                        } else {
     535                                this.settings.set( { 'value': this.model.revisions.indexOf( this.model.get( 'to' ) ) } );
     536                        }
    436537                },
    437538
    438539                start: function( event, ui ) {
    439                         // Track the mouse position to enable smooth dragging, overrides default jquery ui step behaviour
    440                         $( window ).mousemove( function( e ) {
    441                                 var sliderLeft  = $( '.wp-slider' ).offset().left,
    442                                         sliderRight = sliderLeft + $( '.wp-slider' ).width();
     540                        if ( true !== this.model.get( 'compareTwoMode' )) {
     541                                // Track the mouse position to enable smooth dragging, overrides default jquery ui step behaviour .
     542                                $( window ).mousemove( function( e ) {
     543                                        var sliderLeft = $( '.wp-slider' ).offset().left,
     544                                                sliderRight = sliderLeft + $( '.wp-slider' ).width();
    443545
    444                                 // Follow mouse movements, as long as handle remains inside slider
    445                                 if ( e.clientX < sliderLeft ) {
    446                                         $( ui.handle ).css( 'left', 0 ); // Mouse to left of slider
    447                                 } else if ( e.clientX > sliderRight ) {
    448                                         $( ui.handle ).css( 'left', sliderRight - sliderLeft); // Mouse to right of slider
    449                                 } else {
    450                                         $( ui.handle ).css( 'left', e.clientX - sliderLeft ); // Mouse in slider
    451                                 }
    452                         } ); // End mousemove
     546                                        // Follow mouse movements, as long as handle remains inside slider.
     547                                        if ( e.clientX < sliderLeft ) {
     548                                                $( ui.handle ).css( 'left', 0 ); // Mouse to left of slider.
     549                                        } else if ( e.clientX > sliderRight ) {
     550                                                $( ui.handle ).css( 'left', sliderRight - sliderLeft); // Mouse to right of slider.
     551                                        } else {
     552                                                $( ui.handle ).css( 'left', e.clientX - sliderLeft ); // Mouse in slider.
     553                                        }
     554                                } ); // End mousemove.
     555                        }
    453556                },
    454557
     558                getSliderPosition: function( ui ){
     559                        return isRtl ? this.model.revisions.length - ui.value - 1 : ui.value;
     560                },
     561
    455562                slide: function( event, ui ) {
    456                         var attributes = {
    457                                 to: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.value - 1 : ui.value ) // Reverse directions for Rtl
    458                         };
     563                        var attributes;
     564                        // Compare two revisions mode
     565                        if ( 'undefined' !== typeof ui.values && this.model.get( 'compareTwoMode' ) ) {
     566                                // Prevent sliders from occupying same spot
     567                                if ( ui.values[1] === ui.values[0] )
     568                                        return false;
     569                                attributes = {
     570                                        to: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[1] - 1 : ui.values[1] ), // Reverse directions for Rtl.
     571                                        from: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[0] - 1 : ui.values[0] ) // Reverse directions for Rtl.
     572                                };
    459573
    460                         // If we're at the first revision, unset 'from'.
    461                         if ( isRtl ? this.model.revisions.length - ui.value - 1 : ui.value ) // Reverse directions for Rtl
    462                                 attributes.from = this.model.revisions.at( isRtl ? this.model.revisions.length - ui.value - 2 : ui.value - 1 );
    463                         else
    464                                 this.model.unset('from', { silent: true });
     574                        } else {
     575                                // Compare single revision mode
     576                                var sliderPosition = this.getSliderPosition( ui );
     577                                attributes = {
     578                                        to: this.model.revisions.at( sliderPosition )
     579                                };
    465580
     581                                // If we're at the first revision, unset 'from'.
     582                                if ( sliderPosition ) // Reverse directions for Rtl.
     583                                        attributes.from = this.model.revisions.at( sliderPosition - 1  );
     584                                else
     585                                        this.model.unset('from', { silent: true });
     586
     587                        }
    466588                        this.model.set( attributes );
     589
     590                        // Maintain state history when dragging
     591                        this.model.revisionsRouter.navigateRoute( attributes.to.id, ( attributes.from ? attributes.from.id : 0 ) );
     592
    467593                },
    468594
    469595                stop: function( event, ui ) {
    470                         $( window ).unbind( 'mousemove' ); // Stop tracking the mouse
    471                         // Reset settings pops handle back to the step position
    472                         this.settings.trigger( 'change' );
     596                        if ( true !== this.model.get( 'compareTwoMode' )) {
     597                                $( window ).unbind( 'mousemove' ); // Stop tracking the mouse.
     598                                // Reset settings pops handle back to the step position.
     599                                this.settings.trigger( 'change' );
     600                        }
    473601                }
    474602        });
    475603
     
    486614                }
    487615        });
    488616
     617        // The revisions router
     618        // takes URLs with #hash fragments and routes them
     619        revisions.router.Router = Backbone.Router.extend({
     620                model: null,
     621
     622                routes: {
     623                        'revision/from/:from/to/:to/handles/:handles': 'gotoRevisionId'
     624                },
     625
     626                navigateRoute: function( to, from ) {
     627                        var navigateTo = '/revision/from/' + from + '/to/' + to + '/handles/';
     628                        if ( this.model.get( 'compareTwoMode' ) ){
     629                                navigateTo = navigateTo + '2';
     630                        } else {
     631                                navigateTo = navigateTo + '1';
     632                        }
     633                        this.navigate( navigateTo );
     634                },
     635
     636                gotoRevisionId: function( to, from, handles ) {
     637                        if ( '2' === handles ) {
     638                                this.model.set( { compareTwoMode: true } );
     639                                $( '.compare-two-revisions' ).attr( 'checked', true );
     640                        } else {
     641                                this.model.set( { compareTwoMode: false } );
     642                                $( '.compare-two-revisions' ).attr( 'checked', false );
     643                        }
     644                        if ('undefined' !== typeof this.model ) {
     645                                var selectedToRevision =
     646                                        this.model.revisions.findWhere( { 'id': Number( to ) } ),
     647                                        selectedFromRevision =
     648                                        this.model.revisions.findWhere( { 'id': Number( from ) } );
     649
     650                                this.model.set( {
     651                                        to:   selectedToRevision,
     652                                        from: selectedFromRevision } );
     653                        }
     654                }
     655
     656        });
     657
    489658        // Initialize the revisions UI.
    490659        revisions.init = function() {
    491660                revisions.view.frame = new revisions.view.Frame({
    492661                        collection: new revisions.model.Revisions( revisions.settings.revisionData )
    493662                }).render();
     663                // Only allow compare two mode if three or more revisions
    494664        };
    495665
    496666        $( revisions.init );
  • wp-admin/css/wp-admin-rtl.css

     
    975975        left: 6px;
    976976}
    977977
    978 #toggle-revision-compare-mode {
     978.revision-toggle-compare-mode {
    979979        right: auto;
    980980        left: 0;
    981981        padding: 9px 0 0 9px;
    982982}
    983983
    984 #diff-next-revision {
     984.revisions-next {
    985985        float: left;
    986986}
    987987
    988 #diff-previous-revision {
     988.revisions-previous {
    989989        float: right;
    990990}
    991991
  • wp-admin/css/wp-admin.css

     
    35023502        margin-bottom: 10px;
    35033503}
    35043504
     3505.comparing-two-revisions .revisions-controls {
     3506        height: 90px;
     3507}
     3508
    35053509.revisions-meta {
    35063510        margin-top: 15px;
    35073511}
     
    35103514        top: 0;
    35113515        right: 0;
    35123516}
     3517.comparing-two-revisions .revisions-previous,
     3518.comparing-two-revisions .revisions-next {
     3519        display: none;
     3520}
    35133521
    35143522.revisions-previous {
    35153523        float: left;
     
    36553663        margin-top: 20px;
    36563664}
    36573665
    3658 .comparing-two-revisions #diff-previous-revision,
    3659 .comparing-two-revisions #diff-next-revision,
     3666#diff-previous-revision,
     3667#diff-next-revision,
    36603668#diff-header-from {
    36613669        display: none;
    36623670}