summaryrefslogtreecommitdiff
path: root/resources/mediawiki.special/mediawiki.special.search.js
blob: 2dab3026c4ac0eeb121979b207517bcee4d31dd4 (plain)
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
47
48
49
50
51
52
53
/**
 * JavaScript for Special:Search
 */
( function ( mw, $ ) {
	$( document ).ready( function () {
		var $checkboxes, $headerLinks;

		// Emulate HTML5 autofocus behavior in non HTML5 compliant browsers
		if ( !( 'autofocus' in document.createElement( 'input' ) ) ) {
			$( 'input[autofocus]' ).eq( 0 ).focus();
		}

		// Create check all/none button
		$checkboxes = $('#powersearch input[id^=mw-search-ns]');
		$('#mw-search-togglebox').append(
			$('<label>')
				.text(mw.msg('powersearch-togglelabel'))
		).append(
			$('<input type="button" />')
				.attr( 'id', 'mw-search-toggleall' )
				.prop( 'value', mw.msg('powersearch-toggleall' ) )
				.click( function () {
					$checkboxes.prop('checked', true);
				} )
		).append(
			$('<input type="button" />')
				.attr( 'id', 'mw-search-togglenone' )
				.prop( 'value', mw.msg('powersearch-togglenone' ) )
				.click( function() {
					$checkboxes.prop( 'checked', false );
				} )
		);

		// Change the header search links to what user entered
		$headerLinks = $( '.search-types a' );
		$( '#searchText, #powerSearchText' ).change( function () {
			var searchterm = $(this).val();
			$headerLinks.each( function () {
				var parts = $(this).attr('href').split( 'search=' ),
					lastpart = '',
					prefix = 'search=';
				if ( parts.length > 1 && parts[1].indexOf('&') >= 0 ) {
					lastpart = parts[1].substring( parts[1].indexOf('&') );
				} else {
					prefix = '&search=';
				}
				this.href = parts[0] + prefix + encodeURIComponent( searchterm ) + lastpart;
			});
		}).trigger( 'change' );

	} );

}( mediaWiki, jQuery ) );