summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.special/mediawiki.special.block.js
blob: ba9319510d747bc2e69e904198e208ad887c26d6 (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
54
55
56
57
58
59
60
61
/*!
 * JavaScript for Special:Block
 */
( function ( mw, $ ) {
	// Like OO.ui.infuse(), but if the element doesn't exist, return null instead of throwing an exception.
	function infuseOrNull( elem ) {
		try {
			return OO.ui.infuse( elem );
		} catch ( er ) {
			return null;
		}
	}

	$( function () {
		// This code is also loaded on the "block succeeded" page where there is no form,
		// so username and expiry fields might also be missing.
		var blockTargetWidget = infuseOrNull( 'mw-bi-target' ),
			anonOnlyField = infuseOrNull( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
			enableAutoblockField = infuseOrNull( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
			hideUserField = infuseOrNull( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
			watchUserField = infuseOrNull( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
			// mw.widgets.SelectWithInputWidget
			expiryWidget = infuseOrNull( 'mw-input-wpExpiry' );

		function updateBlockOptions() {
			var blocktarget = blockTargetWidget.getValue().trim(),
				isEmpty = blocktarget === '',
				isIp = mw.util.isIPAddress( blocktarget, true ),
				isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
				isNonEmptyIp = isIp && !isEmpty,
				expiryValue = expiryWidget.dropdowninput.getValue(),
				// infinityValues  are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
				infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
				isIndefinite = infinityValues.indexOf( expiryValue ) !== -1 ||
					( expiryValue === 'other' && infinityValues.indexOf( expiryWidget.textinput.getValue() ) !== -1 );

			if ( enableAutoblockField ) {
				enableAutoblockField.toggle( !( isNonEmptyIp ) );
			}
			if ( hideUserField ) {
				hideUserField.toggle( !( isNonEmptyIp || !isIndefinite ) );
			}
			if ( anonOnlyField ) {
				anonOnlyField.toggle( !( !isIp && !isEmpty ) );
			}
			if ( watchUserField ) {
				watchUserField.toggle( !( isIpRange && !isEmpty ) );
			}
		}

		if ( blockTargetWidget ) {
			// Bind functions so they're checked whenever stuff changes
			blockTargetWidget.on( 'change', updateBlockOptions );
			expiryWidget.dropdowninput.on( 'change', updateBlockOptions );
			expiryWidget.textinput.on( 'change', updateBlockOptions );

			// Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
			updateBlockOptions();
		}
	} );
}( mediaWiki, jQuery ) );