Merge "Convert -{}- markups in title="" and alt=""."
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.searchSuggest.js
1 /**
2 * Add search suggestions to the search form.
3 */
4 ( function ( mw, $ ) {
5 $( document ).ready( function ( $ ) {
6 var map,
7 // Region where the suggestions box will appear directly below
8 // (using the same width). Can be a container element or the input
9 // itself, depending on what suits best in the environment.
10 // For Vector the suggestion box should align with the simpleSearch
11 // container's borders, in other skins it should align with the input
12 // element (not the search form, as that would leave the buttons
13 // vertically between the input and the suggestions).
14 $searchRegion = $( '#simpleSearch, #searchInput' ).first(),
15 $searchInput = $( '#searchInput' );
16
17 // Ensure that the thing is actually present!
18 if ( $searchRegion.length === 0 ) {
19 // Don't try to set anything up if simpleSearch is disabled sitewide.
20 // The loader code loads us if the option is present, even if we're
21 // not actually enabled (anymore).
22 return;
23 }
24
25 // Compatibility map
26 map = {
27 browsers: {
28 // Left-to-right languages
29 ltr: {
30 // SimpleSearch is broken in Opera < 9.6
31 opera: [['>=', 9.6]],
32 docomo: false,
33 blackberry: false,
34 ipod: false,
35 iphone: false
36 },
37 // Right-to-left languages
38 rtl: {
39 opera: [['>=', 9.6]],
40 docomo: false,
41 blackberry: false,
42 ipod: false,
43 iphone: false
44 }
45 }
46 };
47
48 if ( !$.client.test( map ) ) {
49 return;
50 }
51
52 // Placeholder text for search box
53 $searchInput
54 .attr( 'placeholder', mw.msg( 'searchsuggest-search' ) )
55 .placeholder();
56
57 // General suggestions functionality for all search boxes
58 $( '#searchInput, #searchInput2, #powerSearchText, #searchText' )
59 .suggestions( {
60 fetch: function ( query ) {
61 var $el, jqXhr;
62
63 if ( query.length !== 0 ) {
64 $el = $(this);
65 jqXhr = $.ajax( {
66 url: mw.util.wikiScript( 'api' ),
67 data: {
68 format: 'json',
69 action: 'opensearch',
70 search: query,
71 namespace: 0,
72 suggest: ''
73 },
74 dataType: 'json',
75 success: function ( data ) {
76 if ( $.isArray( data ) && data.length ) {
77 $el.suggestions( 'suggestions', data[1] );
78 }
79 }
80 });
81 $el.data( 'request', jqXhr );
82 }
83 },
84 cancel: function () {
85 var jqXhr = $(this).data( 'request' );
86 // If the delay setting has caused the fetch to have not even happened
87 // yet, the jqXHR object will have never been set.
88 if ( jqXhr && $.isFunction ( jqXhr.abort ) ) {
89 jqXhr.abort();
90 $(this).removeData( 'request' );
91 }
92 },
93 result: {
94 select: function ( $input ) {
95 $input.closest( 'form' ).submit();
96 }
97 },
98 delay: 120,
99 highlightInput: true
100 } )
101 .bind( 'paste cut drop', function () {
102 // make sure paste and cut events from the mouse and drag&drop events
103 // trigger the keypress handler and cause the suggestions to update
104 $( this ).trigger( 'keypress' );
105 } );
106
107 // Special suggestions functionality for skin-provided search box
108 $searchInput.suggestions( {
109 result: {
110 select: function ( $input ) {
111 $input.closest( 'form' ).submit();
112 }
113 },
114 special: {
115 render: function ( query ) {
116 var $el = this;
117 if ( $el.children().length === 0 ) {
118 $el
119 .append(
120 $( '<div>' )
121 .addClass( 'special-label' )
122 .text( mw.msg( 'searchsuggest-containing' ) )
123 )
124 .append(
125 $( '<div>' )
126 .addClass( 'special-query' )
127 .text( query )
128 .autoEllipsis()
129 )
130 .show();
131 } else {
132 $el.find( '.special-query' )
133 .empty()
134 .text( query )
135 .autoEllipsis();
136 }
137 },
138 select: function ( $input ) {
139 $input.closest( 'form' ).append(
140 $( '<input type="hidden"/>', {
141 name: 'fulltext',
142 val: '1'
143 })
144 );
145 $input.closest( 'form' ).submit();
146 }
147 },
148 $region: $searchRegion
149 } );
150
151 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
152 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
153 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
154 $searchInput
155 .data( 'suggestions-context' )
156 .data.$container
157 .css( 'fontSize', $searchInput.css( 'fontSize' ) );
158
159 } );
160
161 }( mediaWiki, jQuery ) );