Applying code conventions:
authorKrinkle <krinkle@users.mediawiki.org>
Thu, 28 Jul 2011 05:53:34 +0000 (05:53 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Thu, 28 Jul 2011 05:53:34 +0000 (05:53 +0000)
* size() -> length
* strict comparison to undefined instead of typeof + string comparison
* merge var statements
* strict comparison to 0 and ''
* dot notation
* trailing whitespace

resources/jquery/jquery.messageBox.js
resources/jquery/jquery.suggestions.js
resources/mediawiki/mediawiki.util.js

index a69fca5..c59e29c 100644 (file)
@@ -11,7 +11,7 @@
  * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
  * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
  */
-( function( $, mw ) {
+( function( $ ) {
 // @return jQuery object of the message box
 $.messageBoxNew = function( options ) {
        options = $.extend( {
@@ -19,16 +19,16 @@ $.messageBoxNew = function( options ) {
                'parent': 'body', // jQuery/CSS selector
                'insert': 'prepend' // 'prepend' or 'append'
        }, options );
-       var $curBox = $( '#'+ options.id );
+       var $curBox = $( '#' + options.id );
        // Only create a new box if it doesn't exist already
-       if ( $curBox.size() > 0 ) {
+       if ( $curBox.length > 0 ) {
                if ( $curBox.hasClass( 'js-messagebox' ) ) {
                        return $curBox;
                } else {
                        return $curBox.addClass( 'js-messagebox' );
                }
        } else {
-               var $newBox = $( '<div/>', {
+               var $newBox = $( '<div>', {
                        'id': options.id,
                        'class': 'js-messagebox',
                        'css': {
@@ -63,8 +63,8 @@ $.messageBox = function( options ) {
        var groupID = options.target + '-' + options.group;
        var $group = $( '#' + groupID );
        // Create group container if not existant
-       if ( $group.size() < 1 ) {
-               $group = $( '<div/>', {
+       if ( $group.length < 1 ) {
+               $group = $( '<div>', {
                        'id': groupID,
                        'class': 'js-messagebox-group'
                });
@@ -79,12 +79,12 @@ $.messageBox = function( options ) {
                $group.hide();
        } else {
                // Actual message addition
-               $group.prepend( $( '<p/>' ).append( options.message ) ).show();
+               $group.prepend( $( '<p>' ).append( options.message ) ).show();
                $target.slideDown();
        }
        // If the last visible group was just hidden, slide the entire box up
        // Othere wise slideDown (if already visible nothing will happen)
-       if ( $target.find( '> *:visible' ).size() === 0 ) {
+       if ( $target.find( '> *:visible' ).length === 0 ) {
                // to avoid a sudden dissapearance of the last group followed by
                // a slide up of only the outline, show it for a second
                $group.show();
@@ -95,4 +95,4 @@ $.messageBox = function( options ) {
        }
        return $group;
 };
-} )( jQuery, mediaWiki );
\ No newline at end of file
+} )( jQuery );
index 90cf53b..55c3001 100644 (file)
@@ -4,36 +4,36 @@
  * Usage:
  *
  * Set options:
- *             $('#textbox').suggestions( { option1: value1, option2: value2 } );
- *             $('#textbox').suggestions( option, value );
+ *             $( '#textbox' ).suggestions( { option1: value1, option2: value2 } );
+ *             $( '#textbox' ).suggestions( option, value );
  * Get option:
- *             value = $('#textbox').suggestions( option );
+ *             value = $( '#textbox' ).suggestions( option );
  * Initialize:
- *             $('#textbox').suggestions();
+ *             $( '#textbox' ).suggestions();
  *
  * Options:
  *
  * fetch(query): Callback that should fetch suggestions and set the suggestions property. Executed in the context of the
- *             textbox
- *             Type: Function
+ *             textbox
+ *             Type: Function
  * cancel: Callback function to call when any pending asynchronous suggestions fetches should be canceled.
- *             Executed in the context of the textbox
+ *             Executed in the context of the textbox
  *             Type: Function
  * special: Set of callbacks for rendering and selecting
  *             Type: Object of Functions 'render' and 'select'
  * result: Set of callbacks for rendering and selecting
  *             Type: Object of Functions 'render' and 'select'
  * $region: jQuery selection of element to place the suggestions below and match width of
- *             Type: jQuery Object, Default: $(this)
+ *             Type: jQuery Object, Default: $(this)
  * suggestions: Suggestions to display
- *             Type: Array of strings
+ *             Type: Array of strings
  * maxRows: Maximum number of suggestions to display at one time
- *             Type: Number, Range: 1 - 100, Default: 7
+ *             Type: Number, Range: 1 - 100, Default: 7
  * delay: Number of ms to wait for the user to stop typing
- *             Type: Number, Range: 0 - 1200, Default: 120
+ *             Type: Number, Range: 0 - 1200, Default: 120
  * submitOnClick: Whether to submit the form containing the textbox when a suggestion is clicked
  *             Type: Boolean, Default: false
- * maxExpandFactor: Maximum suggestions box width relative to the textbox width.  If set to e.g. 2, the suggestions box
+ * maxExpandFactor: Maximum suggestions box width relative to the textbox width. If set to e.g. 2, the suggestions box
  *             will never be grown beyond 2 times the width of the textbox.
  *             Type: Number, Range: 1 - infinity, Default: 3
  * positionFromLeft: Whether to position the suggestion box with the left attribute or the right
@@ -52,7 +52,7 @@ $.suggestions = {
                if ( context.data.timerID != null ) {
                        clearTimeout( context.data.timerID );
                }
-               if ( typeof context.config.cancel == 'function' ) {
+               if ( $.isFunction( context.config.cancel ) ) {
                        context.config.cancel.call( context.data.$textbox );
                }
        },
@@ -66,7 +66,7 @@ $.suggestions = {
        },
        /**
         * Ask the user-specified callback for new suggestions. Any previous delayed call to this function still pending
-        * will be canceled.  If the value in the textbox is empty or hasn't changed since the last time suggestions were fetched, this
+        * will be canceled. If the value in the textbox is empty or hasn't changed since the last time suggestions were fetched, this
         * function does nothing.
         * @param {Boolean} delayed Whether or not to delay this by the currently configured amount of time
         */
@@ -74,12 +74,12 @@ $.suggestions = {
                // Only fetch if the value in the textbox changed and is not empty
                // if the textbox is empty then clear the result div, but leave other settings intouched
                function maybeFetch() {
-                       if ( context.data.$textbox.val().length == 0 ) {
+                       if ( context.data.$textbox.val().length === 0 ) {
                                context.data.$container.hide();
                                context.data.prevText = '';
                        } else if ( context.data.$textbox.val() !== context.data.prevText ) {
-                               if ( typeof context.config.fetch == 'function' ) {
-                                       context.data.prevText = context.data.$textbox.val();
+                               if ( typeof context.config.fetch === 'function' ) {
+                                       context.data.prevText = context.data.$textbox.val();
                                        context.config.fetch.call( context.data.$textbox, context.data.$textbox.val() );
                                }
                        }
@@ -99,7 +99,7 @@ $.suggestions = {
        },
        special: function( context ) {
                // Allow custom rendering - but otherwise don't do any rendering
-               if ( typeof context.config.special.render == 'function' ) {
+               if ( typeof context.config.special.render === 'function' ) {
                        // Wait for the browser to update the value
                        setTimeout( function() {
                                // Render special
@@ -126,8 +126,8 @@ $.suggestions = {
                        case 'suggestions':
                                context.config[property] = value;
                                // Update suggestions
-                               if ( typeof context.data !== 'undefined'  ) {
-                                       if ( context.data.$textbox.val().length == 0 ) {
+                               if ( context.data !== undefined ) {
+                                       if ( context.data.$textbox.val().length === 0 ) {
                                                // Hide the div when no suggestion exist
                                                context.data.$container.hide();
                                        } else {
@@ -135,17 +135,17 @@ $.suggestions = {
                                                context.data.$container.show();
                                                // Update the size and position of the list
                                                var newCSS = {
-                                                       'top': context.config.$region.offset().top + context.config.$region.outerHeight(),
-                                                       'bottom': 'auto',
-                                                       'width': context.config.$region.outerWidth(),
-                                                       'height': 'auto'
+                                                       top: context.config.$region.offset().top + context.config.$region.outerHeight(),
+                                                       bottom: 'auto',
+                                                       width: context.config.$region.outerWidth(),
+                                                       height: 'auto'
                                                };
                                                if ( context.config.positionFromLeft ) {
-                                                       newCSS['left'] = context.config.$region.offset().left;
-                                                       newCSS['right'] = 'auto';
+                                                       newCSS.left = context.config.$region.offset().left;
+                                                       newCSS.right = 'auto';
                                                } else {
-                                                       newCSS['left'] = 'auto';
-                                                       newCSS['right'] = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
+                                                       newCSS.left = 'auto';
+                                                       newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
                                                }
                                                context.data.$container.css( newCSS );
                                                var $results = context.data.$container.children( '.suggestions-results' );
@@ -155,7 +155,7 @@ $.suggestions = {
                                                var matchedText = null;
                                                for ( var i = 0; i < context.config.suggestions.length; i++ ) {
                                                        var text = context.config.suggestions[i];
-                                                       var $result = $( '<div />' )
+                                                       var $result = $( '<div>' )
                                                                .addClass( 'suggestions-result' )
                                                                .attr( 'rel', i )
                                                                .data( 'text', context.config.suggestions[i] )
@@ -167,18 +167,18 @@ $.suggestions = {
                                                                } )
                                                                .appendTo( $results );
                                                        // Allow custom rendering
-                                                       if ( typeof context.config.result.render == 'function' ) {
+                                                       if ( typeof context.config.result.render === 'function' ) {
                                                                context.config.result.render.call( $result, context.config.suggestions[i] );
                                                        } else {
                                                                // Add <span> with text
                                                                if( context.config.highlightInput ) {
                                                                        matchedText = context.data.prevText;
                                                                }
-                                                               $result.append( $( '<span />' )
+                                                               $result.append( $( '<span>' )
                                                                                .css( 'whiteSpace', 'nowrap' )
                                                                                .text( text )
                                                                        );
-                                                               
+
                                                                // Widen results box if needed
                                                                // New width is only calculated here, applied later
                                                                var $span = $result.children( 'span' );
@@ -223,25 +223,25 @@ $.suggestions = {
        highlight: function( context, result, updateTextbox ) {
                var selected = context.data.$container.find( '.suggestions-result-current' );
                if ( !result.get || selected.get( 0 ) != result.get( 0 ) ) {
-                       if ( result == 'prev' ) {
+                       if ( result === 'prev' ) {
                                if( selected.is( '.suggestions-special' ) ) {
-                                       result = context.data.$container.find( '.suggestions-result:last' )
+                                       result = context.data.$container.find( '.suggestions-result:last' );
                                } else {
                                        result = selected.prev();
-                                       if ( selected.length == 0 ) {
+                                       if ( selected.length === 0 ) {
                                                // we are at the beginning, so lets jump to the last item
-                                               if ( context.data.$container.find( '.suggestions-special' ).html() != "" ) {
+                                               if ( context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
                                                        result = context.data.$container.find( '.suggestions-special' );
                                                } else {
                                                        result = context.data.$container.find( '.suggestions-results div:last' );
                                                }
                                        }
                                }
-                       } else if ( result == 'next' ) {
-                               if ( selected.length == 0 ) {
+                       } else if ( result === 'next' ) {
+                               if ( selected.length === 0 ) {
                                        // No item selected, go to the first one
                                        result = context.data.$container.find( '.suggestions-results div:first' );
-                                       if ( result.length == 0 && context.data.$container.find( '.suggestions-special' ).html() != "" ) {
+                                       if ( result.length === 0 && context.data.$container.find( '.suggestions-special' ).html() !== '' ) {
                                                // No suggestion exists, go to the special one directly
                                                result = context.data.$container.find( '.suggestions-special' );
                                        }
@@ -250,8 +250,8 @@ $.suggestions = {
                                        if ( selected.is( '.suggestions-special' ) ) {
                                                result = $( [] );
                                        } else if (
-                                               result.length == 0 &&
-                                               context.data.$container.find( '.suggestions-special' ).html() != ""
+                                               result.length === 0 &&
+                                               context.data.$container.find( '.suggestions-special' ).html() !== ''
                                        ) {
                                                // We were at the last item, jump to the specials!
                                                result = context.data.$container.find( '.suggestions-special' );
@@ -262,7 +262,7 @@ $.suggestions = {
                        result.addClass( 'suggestions-result-current' );
                }
                if ( updateTextbox ) {
-                       if ( result.length == 0 || result.is( '.suggestions-special' ) ) {
+                       if ( result.length === 0 || result.is( '.suggestions-special' ) ) {
                                $.suggestions.restore( context );
                        } else {
                                context.data.$textbox.val( result.data( 'text' ) );
@@ -278,8 +278,8 @@ $.suggestions = {
         * @param key Integer Code of key pressed
         */
        keypress: function( e, context, key ) {
-               var wasVisible = context.data.$container.is( ':visible' );
-               var preventDefault = false;
+               var     wasVisible = context.data.$container.is( ':visible' ),
+                       preventDefault = false;
                switch ( key ) {
                        // Arrow down
                        case 40:
@@ -312,17 +312,17 @@ $.suggestions = {
                                context.data.$container.hide();
                                preventDefault = wasVisible;
                                selected = context.data.$container.find( '.suggestions-result-current' );
-                               if ( selected.size() == 0 || context.data.selectedWithMouse ) {
-                                       // if nothing is selected OR if something was selected with the mouse, 
+                               if ( selected.length === 0 || context.data.selectedWithMouse ) {
+                                       // if nothing is selected OR if something was selected with the mouse,
                                        // cancel any current requests and submit the form
                                        $.suggestions.cancel( context );
                                        context.config.$region.closest( 'form' ).submit();
                                } else if ( selected.is( '.suggestions-special' ) ) {
-                                       if ( typeof context.config.special.select == 'function' ) {
+                                       if ( typeof context.config.special.select === 'function' ) {
                                                context.config.special.select.call( selected, context.data.$textbox );
                                        }
                                } else {
-                                       if ( typeof context.config.result.select == 'function' ) {
+                                       if ( typeof context.config.result.select === 'function' ) {
                                                $.suggestions.highlight( context, selected, true );
                                                context.config.result.select.call( selected, context.data.$textbox );
                                        } else {
@@ -341,17 +341,17 @@ $.suggestions = {
        }
 };
 $.fn.suggestions = function() {
-       
+
        // Multi-context fields
        var returnValue = null;
        var args = arguments;
-       
+
        $(this).each( function() {
 
                /* Construction / Loading */
-               
+
                var context = $(this).data( 'suggestions-context' );
-               if ( typeof context == 'undefined' || context == null ) {
+               if ( context === undefined || context === null ) {
                        context = {
                                config: {
                                        'fetch' : function() {},
@@ -369,17 +369,17 @@ $.fn.suggestions = function() {
                                }
                        };
                }
-               
+
                /* API */
-               
+
                // Handle various calling styles
                if ( args.length > 0 ) {
-                       if ( typeof args[0] == 'object' ) {
+                       if ( typeof args[0] === 'object' ) {
                                // Apply set of properties
                                for ( var key in args[0] ) {
                                        $.suggestions.configure( context, key, args[0][key] );
                                }
-                       } else if ( typeof args[0] == 'string' ) {
+                       } else if ( typeof args[0] === 'string' ) {
                                if ( args.length > 1 ) {
                                        // Set property values
                                        $.suggestions.configure( context, args[0], args[1] );
@@ -389,10 +389,10 @@ $.fn.suggestions = function() {
                                }
                        }
                }
-               
+
                /* Initialization */
-               
-               if ( typeof context.data == 'undefined' ) {
+
+               if ( context.data === undefined ) {
                        context.data = {
                                // ID of running timer
                                'timerID': null,
@@ -407,23 +407,23 @@ $.fn.suggestions = function() {
                        };
                        // Setup the css for positioning the results box
                        var newCSS = {
-                               'top': Math.round( context.data.$textbox.offset().top + context.data.$textbox.outerHeight() ),
-                               'width': context.data.$textbox.outerWidth(),
-                               'display': 'none'
+                               top: Math.round( context.data.$textbox.offset().top + context.data.$textbox.outerHeight() ),
+                               width: context.data.$textbox.outerWidth(),
+                               display: 'none'
                        };
                        if ( context.config.positionFromLeft ) {
-                               newCSS['left'] = context.config.$region.offset().left;
-                               newCSS['right'] = 'auto';
+                               newCSS.left = context.config.$region.offset().left;
+                               newCSS.right = 'auto';
                        } else {
-                               newCSS['left'] = 'auto';
-                               newCSS['right'] = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
+                               newCSS.left = 'auto';
+                               newCSS.right = $( 'body' ).width() - ( context.config.$region.offset().left + context.config.$region.outerWidth() );
                        }
-                       
-                       context.data.$container = $( '<div />' )
+
+                       context.data.$container = $( '<div>' )
                                .css( newCSS )
                                .addClass( 'suggestions' )
                                .append(
-                                       $( '<div />' ).addClass( 'suggestions-results' )
+                                       $( '<div>' ).addClass( 'suggestions-results' )
                                                // Can't use click() because the container div is hidden when the textbox loses focus. Instead,
                                                // listen for a mousedown followed by a mouseup on the same div
                                                .mousedown( function( e ) {
@@ -438,14 +438,14 @@ $.fn.suggestions = function() {
                                                        }
                                                        $.suggestions.highlight( context, $result, true );
                                                        context.data.$container.hide();
-                                                       if ( typeof context.config.result.select == 'function' ) {
+                                                       if ( typeof context.config.result.select === 'function' ) {
                                                                context.config.result.select.call( $result, context.data.$textbox );
                                                        }
                                                        context.data.$textbox.focus();
                                                } )
                                )
                                .append(
-                                       $( '<div />' ).addClass( 'suggestions-special' )
+                                       $( '<div>' ).addClass( 'suggestions-special' )
                                                // Can't use click() because the container div is hidden when the textbox loses focus. Instead,
                                                // listen for a mousedown followed by a mouseup on the same div
                                                .mousedown( function( e ) {
@@ -459,7 +459,7 @@ $.fn.suggestions = function() {
                                                                return;
                                                        }
                                                        context.data.$container.hide();
-                                                       if ( typeof context.config.special.select == 'function' ) {
+                                                       if ( typeof context.config.special.select === 'function' ) {
                                                                context.config.special.select.call( $special, context.data.$textbox );
                                                        }
                                                        context.data.$textbox.focus();
@@ -477,9 +477,9 @@ $.fn.suggestions = function() {
                                .attr( 'autocomplete', 'off')
                                .keydown( function( e ) {
                                        // Store key pressed to handle later
-                                       context.data.keypressed = ( e.keyCode == undefined ) ? e.which : e.keyCode;
+                                       context.data.keypressed = ( e.keyCode === undefined ) ? e.which : e.keyCode;
                                        context.data.keypressedCount = 0;
-                                       
+
                                        switch ( context.data.keypressed ) {
                                                // This preventDefault logic is duplicated from
                                                // $.suggestions.keypress(), which sucks
@@ -503,7 +503,7 @@ $.fn.suggestions = function() {
                                .keyup( function( e ) {
                                        // Some browsers won't throw keypress() for arrow keys. If we got a keydown and a keyup without a
                                        // keypress in between, solve it
-                                       if ( context.data.keypressedCount == 0 ) {
+                                       if ( context.data.keypressedCount === 0 ) {
                                                $.suggestions.keypress( e, context, context.data.keypressed );
                                        }
                                } )
index 402fe3a..a3986d7 100644 (file)
                                                $tocTitle = $( '#toctitle' ),
                                                $tocToggleLink = $( '#togglelink' );
                                        // Only add it if there is a TOC and there is no toggle added already
-                                       if ( $tocContainer.size() && $tocTitle.size() && !$tocToggleLink.size() ) {
+                                       if ( $tocContainer.length && $tocTitle.length && !$tocToggleLink.length ) {
                                                var     hideTocCookie = $.cookie( 'mw_hidetoc' );
-                                                       $tocToggleLink = $( '<a href="#" class="internal" id="togglelink">' )
+                                                       $tocToggleLink = $( '<a href="#" class="internal" id="togglelink"></a>' )
                                                                .text( mw.msg( 'hidetoc' ) )
                                                                .click( function(e){
                                                                        e.preventDefault();
                                                                        util.toggleToc( $(this) );
                                                        } );
-                                               $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle">' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ) );
+                                               $tocTitle.append( $tocToggleLink.wrap( '<span class="toctoggle"></span>' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ) );
 
                                                if ( hideTocCookie == '1' ) {
                                                        // Cookie says user want toc hidden
 
                        // This function shouldn't be called if there's no TOC,
                        // but just in case...
-                       if ( $tocList.size() ) {
+                       if ( $tocList.length ) {
                                if ( $tocList.is( ':hidden' ) ) {
                                        $tocList.slideDown( 'fast', callback );
                                        $toggleLink.text( mw.msg( 'hidetoc' ) );
                'updateTooltipAccessKeys' : function( nodeList ) {
                        var $nodes;
                        if ( !nodeList ) {
-                       
+
                                // Rather than scanning all links, just the elements that
                                // contain the relevant links
                                this.updateTooltipAccessKeys(