http://www.mediawiki.org/wiki/Special:Code/MediaWiki/83605

Revision: 83605
Author:   krinkle
Date:     2011-03-09 19:32:25 +0000 (Wed, 09 Mar 2011)
Log Message:
-----------
JavaScript clean-up (Code conventions, 1.17 migration, cross-browser fixes and 
JSHint validation)
: Vector SimpleSearch
* Using the mw-alias global
* Optimizing selector (Sizzle starts at the right, about 3x faster now )
*--> see also http://jsperf.com/jquery-selector-perf-select-right-to-left )
* Using mw.config instead of legacy globals
* Before checking "foo in bar" making sure it's an array as it'll throw an 
exception
* typeof is not reliable for functions cross-browser, using $.isFunction
* $.fn.is() is fairly slow as it parses the string and then calls filter which 
will then do it's thing and eventually call hasClass(). Calling it directly now
* Except for elements that can't have children (<input>) or can't be close 
(<img />), the html string should be valid and have a closing tag.("<input 
/>"-> "<input>", "<div />" -> "<div></div>"). See also 
http://api.jquery.com/jQuery/
* Using jQuery 1.4's new ability to set element properties in the initial 
function call by passing an object as second argument instead of 
chain().of().calls() afterwards (see docs for what properties can and can't be 
set this way)
* Using strict === comparison to compare to zero (faster and safer)

Modified Paths:
--------------
    trunk/extensions/Vector/modules/ext.vector.simpleSearch.js

Modified: trunk/extensions/Vector/modules/ext.vector.simpleSearch.js
===================================================================
--- trunk/extensions/Vector/modules/ext.vector.simpleSearch.js  2011-03-09 
19:15:37 UTC (rev 83604)
+++ trunk/extensions/Vector/modules/ext.vector.simpleSearch.js  2011-03-09 
19:32:25 UTC (rev 83605)
@@ -1,7 +1,7 @@
 /* JavaScript for SimpleSearch extension */
 
 $( document ).ready( function() {
-       
+
        // Compatibility map
        var map = {
                'browsers': {
@@ -27,33 +27,33 @@
        if ( !$.client.test( map ) ) {
                return true;
        }
-       
+
        // Disable MWSuggest if loaded
        if ( window.os_MWSuggestDisable ) {
                window.os_MWSuggestDisable();
        }
-       
+
        // Placeholder text for SimpleSearch box
-       $( 'div#simpleSearch > input#searchInput' )
-               .attr( 'placeholder', mediaWiki.msg( 
'vector-simplesearch-search' ) )
+       $( '#simpleSearch > input#searchInput' )
+               .attr( 'placeholder', mw.msg( 'vector-simplesearch-search' ) )
                .placeholder();
-       
+
        // General suggestions functionality for all search boxes
        $( '#searchInput, #searchInput2, #powerSearchText, #searchText' )
                .suggestions( {
                        fetch: function( query ) {
                                var $this = $(this);
                                var request = $.ajax( {
-                                       url: wgScriptPath + '/api.php',
+                                       url: mw.config.get( 'wgScriptPath' ) + 
'/api.php',
                                        data: {
-                                               'action': 'opensearch',
-                                               'search': query,
-                                               'namespace': 0,
-                                               'suggest': ''
+                                               action: 'opensearch',
+                                               search: query,
+                                               namespace: 0,
+                                               suggest: ''
                                        },
                                        dataType: 'json',
                                        success: function( data ) {
-                                               if ( data && 1 in data ) {
+                                               if ( $.isArray( data ) && 1 in 
data ) {
                                                        $this.suggestions( 
'suggestions', data[1] );
                                                }
                                        }
@@ -64,7 +64,7 @@
                                var request = $(this).data( 'request' );
                                // If the delay setting has caused the fetch to 
have not even happend yet, the request object will
                                // have never been set
-                               if ( request && typeof request.abort == 
'function' ) {
+                               if ( request && $.isFunction( request.abort ) ) 
{
                                        request.abort();
                                        $(this).removeData( 'request' );
                                }
@@ -75,7 +75,7 @@
                                }
                        },
                        delay: 120,
-                       positionFromLeft: $( 'body' ).is( '.rtl' ),
+                       positionFromLeft: $( 'body' ).hasClass( 'rtl' ),
                        highlightInput: true
                } )
                .bind( 'paste cut drop', function( e ) {
@@ -92,15 +92,17 @@
                },
                special: {
                        render: function( query ) {
-                               if ( $(this).children().size() == 0  ) {
+                               if ( $(this).children().size() === 0 ) {
                                        $(this).show();
-                                       $label = $( '<div />' )
-                                               .addClass( 'special-label' )
-                                               .text( mediaWiki.msg( 
'vector-simplesearch-containing' ) )
+                                       var $label = $( '<div></div>', {
+                                                       'class': 
'special-label',
+                                                       text: mw.msg( 
'vector-simplesearch-containing' )
+                                               })
                                                .appendTo( $(this) );
-                                       $query = $( '<div />' )
-                                               .addClass( 'special-query' )
-                                               .text( query )
+                                       var $query = $( '<div></div>', {
+                                                       'class': 
'special-query',
+                                                       text: query
+                                               })
                                                .appendTo( $(this) );
                                        $query.autoEllipsis();
                                } else {
@@ -112,11 +114,15 @@
                        },
                        select: function( $input ) {
                                $input.closest( 'form' ).append(
-                                       $( '<input />' ).attr( { 'type': 
'hidden', 'name': 'fulltext', 'value': 1 } )
+                                       $( '<input>', {
+                                               type: 'hidden',
+                                               name: 'fulltext',
+                                               val: '1'
+                                       })
                                );
                                $input.closest( 'form' ).submit();
                        }
                },
                $region: $( '#simpleSearch' )
        } );
-});
+});
\ No newline at end of file


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to