Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / resources / src / mediawiki.special.block.js
1 /*!
2 * JavaScript for Special:Block
3 */
4 ( function () {
5 // Like OO.ui.infuse(), but if the element doesn't exist, return null instead of throwing an exception.
6 function infuseOrNull( elem ) {
7 try {
8 return OO.ui.infuse( elem );
9 } catch ( er ) {
10 return null;
11 }
12 }
13
14 $( function () {
15 // This code is also loaded on the "block succeeded" page where there is no form,
16 // so username and expiry fields might also be missing.
17 var blockTargetWidget = infuseOrNull( 'mw-bi-target' ),
18 anonOnlyField = infuseOrNull( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
19 enableAutoblockField = infuseOrNull( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
20 hideUserField = infuseOrNull( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
21 watchUserField = infuseOrNull( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
22 expiryWidget = infuseOrNull( 'mw-input-wpExpiry' );
23
24 function updateBlockOptions() {
25 var blocktarget = blockTargetWidget.getValue().trim(),
26 isEmpty = blocktarget === '',
27 isIp = mw.util.isIPAddress( blocktarget, true ),
28 isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
29 isNonEmptyIp = isIp && !isEmpty,
30 expiryValue = expiryWidget.getValue(),
31 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
32 infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
33 isIndefinite = infinityValues.indexOf( expiryValue ) !== -1;
34
35 if ( enableAutoblockField ) {
36 enableAutoblockField.toggle( !( isNonEmptyIp ) );
37 }
38 if ( hideUserField ) {
39 hideUserField.toggle( !( isNonEmptyIp || !isIndefinite ) );
40 }
41 if ( anonOnlyField ) {
42 anonOnlyField.toggle( !( !isIp && !isEmpty ) );
43 }
44 if ( watchUserField ) {
45 watchUserField.toggle( !( isIpRange && !isEmpty ) );
46 }
47 }
48
49 if ( blockTargetWidget ) {
50 // Bind functions so they're checked whenever stuff changes
51 blockTargetWidget.on( 'change', updateBlockOptions );
52 expiryWidget.on( 'change', updateBlockOptions );
53
54 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
55 updateBlockOptions();
56 }
57 } );
58 }() );