Merge "(bug 56849) Deprecate dangerous edittime-based content update functions"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.searchSuggest.js
index 8a8871d..282cf6f 100644 (file)
@@ -3,7 +3,7 @@
  */
 ( function ( mw, $ ) {
        $( function () {
-               var map, resultRenderCache, searchboxesSelectors,
+               var api, map, resultRenderCache, searchboxesSelectors,
                        // Region where the suggestions box will appear directly below
                        // (using the same width). Can be a container element or the input
                        // itself, depending on what suits best in the environment.
@@ -66,6 +66,7 @@
                                .wrap(
                                        $( '<a>' )
                                                .attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) )
+                                               .attr( 'title', text )
                                                .addClass( 'mw-searchSuggest-link' )
                                );
                }
                        }
                }
 
-               // General suggestions functionality for all search boxes
+               // Generic suggestions functionality for all search boxes
                searchboxesSelectors = [
                        // Primary searchbox on every page in standard skins
                        '#searchInput',
                        '#powerSearchText',
                        '#searchText',
                        // Generic selector for skins with multiple searchboxes (used by CologneBlue)
+                       // and for MediaWiki itself (special pages with page title inputs)
                        '.mw-searchInput'
                ];
                $( searchboxesSelectors.join( ', ' ) )
                        .suggestions( {
                                fetch: function ( query ) {
-                                       var $el;
-
-                                       if ( query.length !== 0 ) {
-                                               $el = $( this );
-                                               $el.data( 'request', ( new mw.Api() ).get( {
-                                                       action: 'opensearch',
-                                                       search: query,
-                                                       namespace: 0,
-                                                       suggest: ''
-                                               } ).done( function ( data ) {
-                                                       $el.suggestions( 'suggestions', data[1] );
-                                               } ) );
-                                       }
+                                       var $textbox = this,
+                                               node = this[0];
+
+                                       api = api || new mw.Api();
+
+                                       $.data( node, 'request', api.get( {
+                                               action: 'opensearch',
+                                               search: query,
+                                               namespace: 0,
+                                               suggest: ''
+                                       } ).done( function ( data ) {
+                                               $textbox.suggestions( 'suggestions', data[1] );
+                                       } ) );
                                },
                                cancel: function () {
-                                       var apiPromise = $( this ).data( 'request' );
-                                       // If the delay setting has caused the fetch to have not even happened
-                                       // yet, the apiPromise object will have never been set.
-                                       if ( apiPromise && $.isFunction( apiPromise.abort ) ) {
-                                               apiPromise.abort();
-                                               $( this ).removeData( 'request' );
+                                       var node = this[0],
+                                               request = $.data( node, 'request' );
+
+                                       if ( request ) {
+                                               request.abort();
+                                               $.removeData( node, 'request' );
                                        }
                                },
                                result: {
                                        render: renderFunction,
                                        select: function () {
-                                               return true; // allow the form to be submitted
+                                               // allow the form to be submitted
+                                               return true;
                                        }
                                },
                                delay: 120,
                                // make sure paste and cut events from the mouse and drag&drop events
                                // trigger the keypress handler and cause the suggestions to update
                                $( this ).trigger( 'keypress' );
+                       } )
+                       // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
+                       // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
+                       // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
+                       .each( function () {
+                               var $this = $( this );
+                               $this
+                                       .data( 'suggestions-context' )
+                                       .data.$container
+                                               .css( 'fontSize', $this.css( 'fontSize' ) );
                        } );
 
                // Ensure that the thing is actually present!
 
                // Special suggestions functionality for skin-provided search box
                $searchInput.suggestions( {
-                       result: {
-                               render: renderFunction,
-                               select: function () {
-                                       return true; // allow the form to be submitted
-                               }
-                       },
                        special: {
                                render: specialRenderFunction,
                                select: function ( $input ) {
 
                // If the form includes any fallback fulltext search buttons, remove them
                $searchInput.closest( 'form' ).find( '.mw-fallbackSearchButton' ).remove();
-
-               // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
-               // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
-               // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
-               $searchInput
-                       .data( 'suggestions-context' )
-                       .data.$container
-                               .css( 'fontSize', $searchInput.css( 'fontSize' ) );
-
        } );
 
 }( mediaWiki, jQuery ) );