Make WordPress Core

Ticket #25864: 25864.diff

File 25864.diff, 13.9 KB (added by adamsilverstein, 10 years ago)

fix jshint for revisions.js

  • src/wp-admin/js/revisions.js

     
     1/*global _, Backbone, _wpRevisionsSettings */
    12window.wp = window.wp || {};
    23
    34(function($) {
     
    1213        revisions.debug = false;
    1314
    1415        revisions.log = function() {
    15                 if ( window.console && revisions.debug )
    16                         console.log.apply( console, arguments );
     16                if ( window.console && revisions.debug ) {
     17                        window.console.log.apply( window.console, arguments );
     18                }
    1719        };
    1820
    1921        // Handy functions to help with positioning
     
    3436        };
    3537
    3638        // wp_localize_script transforms top-level numbers into strings. Undo that.
    37         if ( revisions.settings.to )
     39        if ( revisions.settings.to ) {
    3840                revisions.settings.to = parseInt( revisions.settings.to, 10 );
    39         if ( revisions.settings.from )
     41        }
     42        if ( revisions.settings.from ) {
    4043                revisions.settings.from = parseInt( revisions.settings.from, 10 );
     44        }
    4145
    4246        // wp_localize_script does not allow for top-level booleans. Fix that.
    43         if ( revisions.settings.compareTwoMode )
     47        if ( revisions.settings.compareTwoMode ) {
    4448                revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1';
     49        }
    4550
    4651        /**
    4752         * ========================================================================
     
    130135                // Receives revisions changes from outside the model
    131136                receiveRevisions: function( from, to ) {
    132137                        // Bail if nothing changed
    133                         if ( this.get('from') === from && this.get('to') === to )
     138                        if ( this.get('from') === from && this.get('to') === to ) {
    134139                                return;
     140                        }
    135141
    136142                        this.set({ from: from, to: to }, { silent: true });
    137143                        this.trigger( 'update:revisions', from, to );
     
    183189                next: function( revision ) {
    184190                        var index = this.indexOf( revision );
    185191
    186                         if ( index !== -1 && index !== this.length - 1 )
     192                        if ( index !== -1 && index !== this.length - 1 ) {
    187193                                return this.at( index + 1 );
     194                        }
    188195                },
    189196
    190197                prev: function( revision ) {
    191198                        var index = this.indexOf( revision );
    192199
    193                         if ( index !== -1 && index !== 0 )
     200                        if ( index !== -1 && index !== 0 ) {
    194201                                return this.at( index - 1 );
     202                        }
    195203                }
    196204        });
    197205
     
    202210        });
    203211
    204212        revisions.model.Diff = Backbone.Model.extend({
    205                 initialize: function( attributes, options ) {
     213                initialize: function() {
    206214                        var fields = this.get('fields');
    207215                        this.unset('fields');
    208216
     
    221229                model: revisions.model.Diff,
    222230
    223231                ensure: function( id, context ) {
    224                         var diff     = this.get( id );
    225                         var request  = this.requests[ id ];
    226                         var deferred = $.Deferred();
    227                         var ids      = {};
    228                         var from     = id.split(':')[0];
    229                         var to       = id.split(':')[1];
     232                        var diff     = this.get( id ),
     233                                request  = this.requests[ id ],
     234                                deferred = $.Deferred(),
     235                                ids      = {},
     236                                from     = id.split(':')[0],
     237                                to       = id.split(':')[1];
    230238                        ids[id] = true;
    231239
    232240                        wp.revisions.log( 'ensure', id );
     
    239247                                this.trigger( 'ensure:load', ids, from, to, deferred.promise() );
    240248                                _.each( ids, _.bind( function( id ) {
    241249                                        // Remove anything that has an ongoing request
    242                                         if ( this.requests[ id ] )
     250                                        if ( this.requests[ id ] ) {
    243251                                                delete ids[ id ];
     252                                        }
    244253                                        // Remove anything we already have
    245                                         if ( this.get( id ) )
     254                                        if ( this.get( id ) ) {
    246255                                                delete ids[ id ];
     256                                        }
    247257                                }, this ) );
    248258                                if ( ! request ) {
    249259                                        // Always include the ID that started this ensure
     
    274284                },
    275285
    276286                _loadAll: function( allRevisionIds, centerId, num ) {
    277                         var self = this, deferred = $.Deferred();
    278                         diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
     287                        var self = this, deferred = $.Deferred(),
     288                                diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );
    279289                        if ( _.size( diffs ) > 0 ) {
    280290                                this.load( diffs ).done( function() {
    281291                                        self._loadAll( allRevisionIds, centerId, num ).done( function() {
     
    299309                load: function( comparisons ) {
    300310                        wp.revisions.log( 'load', comparisons );
    301311                        // Our collection should only ever grow, never shrink, so remove: false
    302                         return this.fetch({ data: { compare: comparisons }, remove: false }).done( function(){
     312                        return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() {
    303313                                wp.revisions.log( 'load:complete', comparisons );
    304314                        });
    305315                },
     
    313323                                        post_id: revisions.settings.postId
    314324                                });
    315325
    316                                 var deferred = wp.ajax.send( options );
    317                                 var requests = this.requests;
     326                                var deferred = wp.ajax.send( options ),
     327                                        requests = this.requests;
    318328
    319329                                // Record that we're requesting each diff.
    320330                                if ( options.data.compare ) {
     
    423433                        diffId = ( from ? from.id : 0 ) + ':' + to.id;
    424434
    425435                        // Check if we're actually changing the diff id.
    426                         if ( this._diffId === diffId )
     436                        if ( this._diffId === diffId ) {
    427437                                return $.Deferred().reject().promise();
     438                        }
    428439
    429440                        this._diffId = diffId;
    430441                        this.trigger( 'update:revisions', from, to );
     
    448459
    449460                // A simple wrapper around `updateDiff` to prevent the change event's
    450461                // parameters from being passed through.
    451                 changeRevisionHandler: function( model, value, options ) {
     462                changeRevisionHandler: function() {
    452463                        this.updateDiff();
    453464                },
    454465
     
    545556                        var slider = new revisions.model.Slider({
    546557                                frame: this.model,
    547558                                revisions: this.model.revisions
    548                         });
     559                        }),
    549560
    550561                        // Prep the tooltip model
    551                         var tooltip = new revisions.model.Tooltip({
     562                        tooltip = new revisions.model.Tooltip({
    552563                                frame: this.model,
    553564                                revisions: this.model.revisions,
    554565                                slider: slider
     
    579590                        this.top = this.$el.offset().top;
    580591                        this.window = $(window);
    581592                        this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) {
    582                                 var controls = e.data.controls;
    583                                 var container = controls.$el.parent();
    584                                 var scrolled = controls.window.scrollTop();
    585                                 var frame = controls.views.parent;
     593                                var controls  = e.data.controls,
     594                                        container = controls.$el.parent(),
     595                                        scrolled  = controls.window.scrollTop(),
     596                                        frame    = controls.views.parent;
    586597
    587598                                if ( scrolled >= controls.top ) {
    588599                                        if ( ! frame.$el.hasClass('pinned') ) {
     
    724735                },
    725736
    726737                ready: function() {
    727                         if ( this.model.revisions.length < 3 )
     738                        if ( this.model.revisions.length < 3 ) {
    728739                                $('.revision-toggle-compare-mode').hide();
     740                        }
    729741                },
    730742
    731743                updateCompareTwoMode: function() {
     
    733745                },
    734746
    735747                // Toggle the compare two mode feature when the compare two checkbox is checked.
    736                 compareTwoToggle: function( event ) {
     748                compareTwoToggle: function() {
    737749                        // Activate compare two mode?
    738750                        this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') });
    739751                }
     
    745757                className: 'revisions-tooltip',
    746758                template: wp.template('revisions-meta'),
    747759
    748                 initialize: function( options ) {
     760                initialize: function() {
    749761                        this.listenTo( this.model, 'change:offset', this.render );
    750762                        this.listenTo( this.model, 'change:hovering', this.toggleVisibility );
    751763                        this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility );
    752764                },
    753765
    754766                prepare: function() {
    755                         if ( _.isNull( this.model.get('revision') ) )
     767                        if ( _.isNull( this.model.get('revision') ) ) {
    756768                                return;
    757                         else
     769                        } else {
    758770                                return _.extend( { type: 'tooltip' }, {
    759771                                        attributes: this.model.get('revision').toJSON()
    760772                                });
     773                        }
    761774                },
    762775
    763776                render: function() {
    764                         var direction, directionVal, flipped, css = {}, position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
     777                        var otherDirection,
     778                                direction,
     779                                directionVal,
     780                                flipped,
     781                                css      = {},
     782                                position = this.model.revisions.indexOf( this.model.get('revision') ) + 1;
     783
    765784                        flipped = ( position / this.model.revisions.length ) > 0.5;
    766785                        if ( isRtl ) {
    767786                                direction = flipped ? 'left' : 'right';
     
    781800                        return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' );
    782801                },
    783802
    784                 toggleVisibility: function( options ) {
    785                         if ( this.visible() )
     803                toggleVisibility: function() {
     804                        if ( this.visible() ) {
    786805                                this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 );
    787                         else
     806                        } else {
    788807                                this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } );
     808                        }
    789809                        return;
    790810                }
    791811        });
     
    815835                                to: this.model.revisions.at( toIndex )
    816836                        };
    817837                        // If we're at the first revision, unset 'from'.
    818                         if ( toIndex )
     838                        if ( toIndex ) {
    819839                                attributes.from = this.model.revisions.at( toIndex - 1 );
    820                         else
     840                        } else {
    821841                                this.model.unset('from', { silent: true });
     842                        }
    822843
    823844                        this.model.set( attributes );
    824845                },
     
    837858
    838859                // Check to see if the Previous or Next buttons need to be disabled or enabled.
    839860                disabledButtonCheck: function() {
    840                         var maxVal = this.model.revisions.length - 1,
    841                                 minVal = 0,
    842                                 next = $('.revisions-next .button'),
     861                        var maxVal   = this.model.revisions.length - 1,
     862                                minVal   = 0,
     863                                next     = $('.revisions-next .button'),
    843864                                previous = $('.revisions-previous .button'),
    844                                 val = this.model.revisions.indexOf( this.model.get('to') );
     865                                val      = this.model.revisions.indexOf( this.model.get('to') );
    845866
    846867                        // Disable "Next" button if you're on the last node.
    847868                        next.prop( 'disabled', ( maxVal === val ) );
     
    884905                },
    885906
    886907                mouseMove: function( e ) {
    887                         var zoneCount = this.model.revisions.length - 1, // One fewer zone than models
    888                                 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider
    889                                 sliderWidth = this.$el.width(), // Width of slider
    890                                 tickWidth = sliderWidth / zoneCount, // Calculated width of zone
    891                                 actualX = isRtl? $(window).width() - e.pageX : e.pageX; // Flipped for RTL - sliderFrom;
    892                         actualX = actualX - sliderFrom; // Offset of mouse position in slider
    893                         var currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 )  ) / tickWidth ); // Calculate the model index
     908                        var zoneCount         = this.model.revisions.length - 1, // One fewer zone than models
     909                                sliderFrom        = this.$el.allOffsets()[this.direction], // "From" edge of slider
     910                                sliderWidth       = this.$el.width(), // Width of slider
     911                                tickWidth         = sliderWidth / zoneCount, // Calculated width of zone
     912                                actualX           = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom;
     913                                currentModelIndex = Math.floor( ( actualX  + ( tickWidth / 2 )  ) / tickWidth ); // Calculate the model index
    894914
    895915                        // Ensure sane value for currentModelIndex.
    896                         if ( currentModelIndex < 0 )
     916                        if ( currentModelIndex < 0 ) {
    897917                                currentModelIndex = 0;
    898                         else if ( currentModelIndex >= this.model.revisions.length )
     918                        } else if ( currentModelIndex >= this.model.revisions.length ) {
    899919                                currentModelIndex = this.model.revisions.length - 1;
     920                        }
    900921
    901922                        // Update the tooltip mode
    902923                        this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) });
     
    940961                                    rightDragBoundary = sliderRightEdge,
    941962                                    leftDragReset     = '0',
    942963                                    rightDragReset    = '100%',
     964                                    handles,
    943965                                    handle            = $( ui.handle );
    944966
    945967                                // In two handle mode, ensure handles can't be dragged past each other.
    946968                                // Adjust left/right boundaries and reset points.
    947969                                if ( view.model.get('compareTwoMode') ) {
    948                                         var handles = handle.parent().find('.ui-slider-handle');
     970                                        handles = handle.parent().find('.ui-slider-handle');
    949971                                        if ( handle.is( handles.first() ) ) { // We're the left handle
    950972                                                rightDragBoundary = handles.last().offset().left;
    951973                                                rightDragReset    = rightDragBoundary - sliderOffset;
     
    976998                        // Compare two revisions mode
    977999                        if ( this.model.get('compareTwoMode') ) {
    9781000                                // Prevent sliders from occupying same spot
    979                                 if ( ui.values[1] === ui.values[0] )
     1001                                if ( ui.values[1] === ui.values[0] ) {
    9801002                                        return false;
    981                                 if ( isRtl )
     1003                                }
     1004                                if ( isRtl ) {
    9821005                                        ui.values.reverse();
     1006                                }
    9831007                                attributes = {
    9841008                                        from: this.model.revisions.at( this.getPosition( ui.values[0] ) ),
    9851009                                        to: this.model.revisions.at( this.getPosition( ui.values[1] ) )
     
    9891013                                        to: this.model.revisions.at( this.getPosition( ui.value ) )
    9901014                                };
    9911015                                // If we're at the first revision, unset 'from'.
    992                                 if ( this.getPosition( ui.value ) > 0 )
     1016                                if ( this.getPosition( ui.value ) > 0 ) {
    9931017                                        attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 );
    994                                 else
     1018                                } else {
    9951019                                        attributes.from = undefined;
     1020                                }
    9961021                        }
    9971022                        movedRevision = this.model.revisions.at( this.getPosition( ui.value ) );
    9981023
    9991024                        // If we are scrubbing, a scrub to a revision is considered a hover
    1000                         if ( this.model.get('scrubbing') )
     1025                        if ( this.model.get('scrubbing') ) {
    10011026                                attributes.hoveredRevision = movedRevision;
     1027                        }
    10021028
    10031029                        this.model.set( attributes );
    10041030                },
    10051031
    1006                 stop: function( event, ui ) {
     1032                stop: function() {
    10071033                        $( window ).off('mousemove.wp.revisions');
    10081034                        this.model.updateSliderSettings(); // To snap us back to a tick mark
    10091035                        this.model.set({ scrubbing: false });
     
    10141040        // This is the view for the current active diff.
    10151041        revisions.view.Diff = wp.Backbone.View.extend({
    10161042                className: 'revisions-diff',
    1017                 template: wp.template('revisions-diff'),
     1043                template:  wp.template('revisions-diff'),
    10181044
    10191045                // Generate the options to be passed to the template.
    10201046                prepare: function() {
     
    10411067                },
    10421068
    10431069                updateUrl: function() {
    1044                         var from = this.model.has('from') ? this.model.get('from').id : 0;
    1045                         var to = this.model.get('to').id;
    1046                         if ( this.model.get('compareTwoMode' ) )
     1070                        var from = this.model.has('from') ? this.model.get('from').id : 0,
     1071                                to  = this.model.get('to').id;
     1072                        if ( this.model.get('compareTwoMode' ) ) {
    10471073                                this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) );
    1048                         else
     1074                        } else {
    10491075                                this.navigate( this.baseUrl( '?revision=' + to ) );
     1076                        }
    10501077                },
    10511078
    10521079                handleRoute: function( a, b ) {
    1053                         var from, to, compareTwo = _.isUndefined( b );
     1080                        var compareTwo = _.isUndefined( b );
    10541081
    10551082                        if ( ! compareTwo ) {
    10561083                                b = this.model.revisions.get( a );