1 jQuery( document
).ready( function( $ ) {
2 var $container
= $( '<div>', { 'class' : 'open-search-suggestions' } ),
5 url
= mw
.util
.wikiScript( 'api' ),
8 //Append the container which will hold the menu to the body
9 $( 'body' ).append( $container
);
11 /* Grabs namespaces from search form or
12 * in case we're not on a search page, take it from wgSearchNamespaces.
13 * @return Array: List of Namespaces that should be searched
15 var getNamespaces = function() {
17 $( 'form#powersearch, form#search' ).find( '[name^="ns"]' ).each(function() {
18 if ( this.checked
|| ( this.type
== 'hidden' && this.value
== '1' ) ) {
19 namespaces
.push( this.name
.substring( 2 ) );
22 if ( !namespaces
.length
) {
23 namespaces
= mw
.config
.get( 'wgSearchNamespaces' );
25 return namespaces
.join('|');
28 /* Helper function to make sure that the list doesn't expand below the visible part of the window */
29 var deliverResult = function( obj
, response
) {
30 if ( obj
&& obj
.length
> 1 ) {
32 // Get the lowest from multiple numbers using fn.apply
33 var maxRow
= Math
.min
.apply( Math
, [7, obj
[1].length
, maxRowWindow
] );
34 $suggestionList
.css( 'height', maxRow
* $suggestionList
.find( '.ui-menu-item' ).eq( 0 ).height() );
40 /* The actual autocomplete setup */
41 $( "#searchInput" ).autocomplete({
43 source: function ( request
, response
) {
44 var namespaces
= getNamespaces();
45 // We're caching queries for performance
46 var term
= request
.term
+ namespaces
;
47 if ( term
in cache
) {
48 deliverResult( cache
[term
], response
);
53 action
: 'opensearch',
54 search
: request
.term
,
55 namespace : namespaces
57 $.getJSON( url
, params
, function ( obj
) {
60 deliverResult( obj
, response
);
64 $( '#searchGoButton' ).click();
67 $suggestionList
= $container
.find( 'ul' );
69 appendTo
: '.open-search-suggestions',
71 maxRowWindow
= Math
.floor(
72 ( $( window
).height() - $suggestionList
.offset().top
+ $( window
).scrollTop() ) /
73 $suggestionList
.find( '.ui-menu-item' ).eq( 0 ).height()
78 /* Legacy teardown, called when things like SimpleSearch need to disable MWSuggest */
79 window
.os_MWSuggestDisable = function() {
80 return $("#searchInput").autocomplete( "destroy" );