| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
( function () { |
| 5 |
|
| 6 |
const searchNS = $.map( mw.config.get( 'wgFormattedNamespaces' ), ( nsName, nsID ) => { |
| 7 |
if ( nsID >= 0 && mw.user.options.get( 'searchNs' + nsID ) ) { |
| 8 |
|
| 9 |
return Number( nsID ); |
| 10 |
} |
| 11 |
} ), |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
shouldTestSearch = !!( mw.config.get( 'skin' ) === 'vector' && |
| 16 |
window.performance && |
| 17 |
performance.mark && |
| 18 |
performance.measure && |
| 19 |
|
| 20 |
performance.getEntriesByName && |
| 21 |
performance.clearMarks ), |
| 22 |
|
| 23 |
loadStartMark = 'mwVectorLegacySearchLoadStart', |
| 24 |
queryMark = 'mwVectorLegacySearchQuery', |
| 25 |
renderMark = 'mwVectorLegacySearchRender', |
| 26 |
queryToRenderMeasure = 'mwVectorLegacySearchQueryToRender', |
| 27 |
loadStartToFirstRenderMeasure = 'mwVectorLegacySearchLoadStartToFirstRender'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
mw.searchSuggest = { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
request: function ( api, query, response, limit, namespace ) { |
| 65 |
return api.get( { |
| 66 |
formatversion: 2, |
| 67 |
action: 'opensearch', |
| 68 |
search: query, |
| 69 |
namespace: namespace || searchNS, |
| 70 |
limit |
| 71 |
} ).done( ( data, jqXHR ) => { |
| 72 |
response( data[ 1 ], { |
| 73 |
type: jqXHR.getResponseHeader( 'X-OpenSearch-Type' ), |
| 74 |
searchId: jqXHR.getResponseHeader( 'X-Search-ID' ), |
| 75 |
query |
| 76 |
} ); |
| 77 |
} ); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
$( () => { |
| 82 |
let api; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const $searchRegion = $( '#simpleSearch, #searchInput' ).first(), |
| 91 |
$searchInput = $( '#searchInput' ); |
| 92 |
let previousSearchText = $searchInput.val(); |
| 93 |
|
| 94 |
function serializeObject( fields ) { |
| 95 |
const obj = {}; |
| 96 |
|
| 97 |
for ( let i = 0; i < fields.length; i++ ) { |
| 98 |
obj[ fields[ i ].name ] = fields[ i ].value; |
| 99 |
} |
| 100 |
|
| 101 |
return obj; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
function getFormData( context ) { |
| 106 |
if ( !context.formData ) { |
| 107 |
|
| 108 |
const $form = context.config.$region.closest( 'form' ); |
| 109 |
|
| 110 |
let baseHref = $form.attr( 'action' ) || ''; |
| 111 |
baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?'; |
| 112 |
|
| 113 |
const linkParams = serializeObject( $form.serializeArray() ); |
| 114 |
|
| 115 |
context.formData = { |
| 116 |
textParam: context.data.$textbox.attr( 'name' ), |
| 117 |
linkParams: linkParams, |
| 118 |
baseHref: baseHref |
| 119 |
}; |
| 120 |
} |
| 121 |
|
| 122 |
return context.formData; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
function onBeforeUpdate() { |
| 132 |
const searchText = this.val(); |
| 133 |
|
| 134 |
if ( searchText && searchText !== previousSearchText ) { |
| 135 |
mw.track( 'mediawiki.searchSuggest', { |
| 136 |
action: 'session-start' |
| 137 |
} ); |
| 138 |
} |
| 139 |
previousSearchText = searchText; |
| 140 |
|
| 141 |
if ( !shouldTestSearch ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if ( performance.getEntriesByName( queryMark ).length ) { |
| 150 |
performance.clearMarks( queryMark ); |
| 151 |
} |
| 152 |
|
| 153 |
performance.mark( queryMark ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
function getInputLocation( context ) { |
| 168 |
return context.config.$region |
| 169 |
.closest( 'form' ) |
| 170 |
.find( '[data-search-loc]' ) |
| 171 |
.data( 'search-loc' ) || 'header'; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
function onAfterUpdate( metadata ) { |
| 182 |
const context = this.data( 'suggestionsContext' ); |
| 183 |
|
| 184 |
mw.track( 'mediawiki.searchSuggest', { |
| 185 |
action: 'impression-results', |
| 186 |
numberOfResults: context.config.suggestions.length, |
| 187 |
resultSetType: metadata.type || 'unknown', |
| 188 |
searchId: metadata.searchId || null, |
| 189 |
query: metadata.query, |
| 190 |
inputLocation: getInputLocation( context ) |
| 191 |
} ); |
| 192 |
|
| 193 |
if ( shouldTestSearch ) { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
requestAnimationFrame( () => { |
| 200 |
requestAnimationFrame( () => { |
| 201 |
if ( !performance.getEntriesByName( queryMark ).length ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
performance.mark( renderMark ); |
| 206 |
performance.measure( queryToRenderMeasure, queryMark, renderMark ); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if ( performance.getEntriesByName( loadStartMark ).length && |
| 211 |
!performance.getEntriesByName( loadStartToFirstRenderMeasure ).length ) { |
| 212 |
performance.measure( loadStartToFirstRenderMeasure, loadStartMark, renderMark ); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
performance.clearMarks( queryMark ); |
| 218 |
performance.clearMarks( renderMark ); |
| 219 |
} ); |
| 220 |
} ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
function renderFunction( text, context ) { |
| 226 |
const formData = getFormData( context ), |
| 227 |
textboxConfig = context.data.$textbox.data( 'mw-searchsuggest' ) || {}; |
| 228 |
|
| 229 |
|
| 230 |
formData.linkParams[ formData.textParam ] = text; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
mw.track( 'mediawiki.searchSuggest', { |
| 235 |
action: 'render-one', |
| 236 |
formData: formData, |
| 237 |
index: context.config.suggestions.indexOf( text ) |
| 238 |
} ); |
| 239 |
|
| 240 |
|
| 241 |
this.text( text ); |
| 242 |
|
| 243 |
|
| 244 |
if ( textboxConfig.wrapAsLink !== false ) { |
| 245 |
this.wrap( |
| 246 |
$( '<a>' ) |
| 247 |
.attr( 'href', formData.baseHref + $.param( formData.linkParams ) ) |
| 248 |
.attr( 'title', text ) |
| 249 |
.addClass( 'mw-searchSuggest-link' ) |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
function selectFunction( $input, source ) { |
| 256 |
const context = $input.data( 'suggestionsContext' ), |
| 257 |
text = $input.val(), |
| 258 |
url = $( this ).parent( 'a' ).attr( 'href' ); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if ( source === 'keyboard' && url || source !== 'keyboard' ) { |
| 271 |
mw.track( 'mediawiki.searchSuggest', { |
| 272 |
action: 'click-result', |
| 273 |
numberOfResults: context.config.suggestions.length, |
| 274 |
index: context.config.suggestions.indexOf( text ) |
| 275 |
} ); |
| 276 |
|
| 277 |
if ( source === 'keyboard' ) { |
| 278 |
window.location.assign( url ); |
| 279 |
|
| 280 |
return false; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
function specialRenderFunction( query, context ) { |
| 289 |
const $el = this, |
| 290 |
formData = getFormData( context ); |
| 291 |
|
| 292 |
|
| 293 |
formData.linkParams[ formData.textParam ] = query; |
| 294 |
|
| 295 |
mw.track( 'mediawiki.searchSuggest', { |
| 296 |
action: 'render-one', |
| 297 |
formData: formData, |
| 298 |
index: context.config.suggestions.indexOf( query ) |
| 299 |
} ); |
| 300 |
|
| 301 |
if ( mw.user.options.get( 'search-match-redirect' ) && $el.children().length === 0 ) { |
| 302 |
$el |
| 303 |
.append( |
| 304 |
$( '<div>' ) |
| 305 |
.addClass( 'special-label' ) |
| 306 |
.text( mw.msg( 'searchsuggest-containing' ) ), |
| 307 |
$( '<div>' ) |
| 308 |
.addClass( 'special-query' ) |
| 309 |
.text( query ) |
| 310 |
) |
| 311 |
.show(); |
| 312 |
} else { |
| 313 |
$el.find( '.special-query' ) |
| 314 |
.text( query ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
if ( $el.parent().hasClass( 'mw-searchSuggest-link' ) ) { |
| 319 |
$el.parent().attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' ); |
| 320 |
} else { |
| 321 |
$el.wrap( |
| 322 |
$( '<a>' ) |
| 323 |
.attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' ) |
| 324 |
.addClass( 'mw-searchSuggest-link' ) |
| 325 |
); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
const searchboxesSelectors = [ |
| 331 |
|
| 332 |
'#searchInput', |
| 333 |
|
| 334 |
|
| 335 |
'.mw-searchInput' |
| 336 |
]; |
| 337 |
$( searchboxesSelectors.join( ', ' ) ) |
| 338 |
.suggestions( { |
| 339 |
fetch: function ( query, response, maxRows ) { |
| 340 |
const node = this[ 0 ]; |
| 341 |
|
| 342 |
api = api || new mw.Api(); |
| 343 |
|
| 344 |
$.data( node, 'request', mw.searchSuggest.request( api, query, response, maxRows ) ); |
| 345 |
}, |
| 346 |
cancel: function () { |
| 347 |
const node = this[ 0 ], |
| 348 |
request = $.data( node, 'request' ); |
| 349 |
|
| 350 |
if ( request ) { |
| 351 |
request.abort(); |
| 352 |
$.removeData( node, 'request' ); |
| 353 |
} |
| 354 |
}, |
| 355 |
result: { |
| 356 |
render: renderFunction, |
| 357 |
select: function () { |
| 358 |
|
| 359 |
return true; |
| 360 |
} |
| 361 |
}, |
| 362 |
update: { |
| 363 |
before: onBeforeUpdate, |
| 364 |
after: onAfterUpdate |
| 365 |
}, |
| 366 |
cache: true, |
| 367 |
highlightInput: true |
| 368 |
} ) |
| 369 |
.on( 'paste cut drop', function () { |
| 370 |
|
| 371 |
|
| 372 |
$( this ).trigger( 'keypress' ); |
| 373 |
} ) |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
.each( function () { |
| 378 |
const $this = $( this ); |
| 379 |
$this |
| 380 |
.data( 'suggestions-context' ) |
| 381 |
.data.$container.css( 'fontSize', $this.css( 'fontSize' ) ); |
| 382 |
} ); |
| 383 |
|
| 384 |
|
| 385 |
if ( $searchRegion.length === 0 ) { |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
$searchInput.suggestions( { |
| 394 |
update: { |
| 395 |
before: onBeforeUpdate, |
| 396 |
after: onAfterUpdate |
| 397 |
}, |
| 398 |
result: { |
| 399 |
render: renderFunction, |
| 400 |
select: selectFunction |
| 401 |
}, |
| 402 |
special: { |
| 403 |
render: specialRenderFunction, |
| 404 |
select: function ( $input, source ) { |
| 405 |
const context = $input.data( 'suggestionsContext' ), |
| 406 |
text = $input.val(); |
| 407 |
if ( source === 'mouse' ) { |
| 408 |
|
| 409 |
mw.track( 'mediawiki.searchSuggest', { |
| 410 |
action: 'click-result', |
| 411 |
numberOfResults: context.config.suggestions.length, |
| 412 |
index: context.config.suggestions.indexOf( text ) |
| 413 |
} ); |
| 414 |
} else { |
| 415 |
$input.closest( 'form' ) |
| 416 |
.append( |
| 417 |
$( '<input>' ) |
| 418 |
.prop( { |
| 419 |
type: 'hidden', |
| 420 |
value: 1 |
| 421 |
} ) |
| 422 |
.attr( 'name', 'fulltext' ) |
| 423 |
); |
| 424 |
} |
| 425 |
return true; |
| 426 |
} |
| 427 |
}, |
| 428 |
$region: $searchRegion |
| 429 |
} ); |
| 430 |
|
| 431 |
const $searchForm = $searchInput.closest( 'form' ); |
| 432 |
$searchForm |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
.on( 'submit', () => { |
| 437 |
const context = $searchInput.data( 'suggestionsContext' ); |
| 438 |
mw.track( 'mediawiki.searchSuggest', { |
| 439 |
action: 'submit-form', |
| 440 |
numberOfResults: context.config.suggestions.length, |
| 441 |
$form: context.config.$region.closest( 'form' ), |
| 442 |
inputLocation: getInputLocation( context ), |
| 443 |
index: context.config.suggestions.indexOf( |
| 444 |
context.data.$textbox.val() |
| 445 |
) |
| 446 |
} ); |
| 447 |
} ); |
| 448 |
|
| 449 |
|
| 450 |
if ( $searchForm.find( '.mw-fallbackSearchButton ~ .searchButton' ).length ) { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
$searchForm.find( '.mw-fallbackSearchButton' ).remove(); |
| 459 |
} |
| 460 |
} ); |
| 461 |
|
| 462 |
}() ); |