aboutsummaryrefslogtreecommitdiffstats
path: root/resources/src/mediawiki.special.search/search.js
blob: 020f01eaf74df0a9bfc8e9ee8c17cc8e9bee7424 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*!
 * JavaScript for Special:Search
 */
( function () {
	$( () => {
		// Emulate HTML5 autofocus behavior in non HTML5 compliant browsers
		if ( !( 'autofocus' in document.createElement( 'input' ) ) ) {
			$( 'input[autofocus]' ).eq( 0 ).trigger( 'focus' );
		}

		// Attach handler for check all/none buttons
		const $checkboxes = $( '#powersearch input[id^=mw-search-ns]' );
		$( '#mw-search-toggleall' ).on( 'click', () => {
			$checkboxes.prop( 'checked', true );
		} );
		$( '#mw-search-togglenone' ).on( 'click', () => {
			$checkboxes.prop( 'checked', false );
		} );

		// Change the header search links to what user entered
		const $headerLinks = $( '.search-types a' );
		const searchWidget = OO.ui.infuse( $( '#searchText' ) );
		const updateHeaderLinks = function ( value ) {
			$headerLinks.each( function () {
				const parts = $( this ).attr( 'href' ).split( 'search=' );
				let lastpart = '',
					prefix = 'search=';
				if ( parts.length > 1 && parts[ 1 ].includes( '&' ) ) {
					lastpart = parts[ 1 ].slice( parts[ 1 ].indexOf( '&' ) );
				} else {
					prefix = '&search=';
				}
				this.href = parts[ 0 ] + prefix + encodeURIComponent( value ) + lastpart;
			} );
		};
		searchWidget.on( 'change', updateHeaderLinks );
		updateHeaderLinks( searchWidget.getValue() );

		// When saving settings, use the proper request method (POST instead of GET).
		$( '#mw-search-powersearch-remember' ).on( 'change', function () {
			this.form.method = this.checked ? 'post' : 'get';
		} ).trigger( 'change' );

	} );

}() );