Merge "Update Special:Block to set Partial Blocks"
[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 editingRestrictionWidget = infuseOrNull( 'mw-input-wpEditingRestriction' ),
24 pageRestrictionsWidget = infuseOrNull( 'mw-input-wpPageRestrictions' );
25
26 function updateBlockOptions() {
27 var blocktarget = blockTargetWidget.getValue().trim(),
28 isEmpty = blocktarget === '',
29 isIp = mw.util.isIPAddress( blocktarget, true ),
30 isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
31 isNonEmptyIp = isIp && !isEmpty,
32 expiryValue = expiryWidget.getValue(),
33 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
34 infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
35 isIndefinite = infinityValues.indexOf( expiryValue ) !== -1,
36 editingRestrictionValue = editingRestrictionWidget ? editingRestrictionWidget.getValue() : undefined;
37
38 if ( enableAutoblockField ) {
39 enableAutoblockField.toggle( !( isNonEmptyIp ) );
40 }
41 if ( hideUserField ) {
42 hideUserField.toggle( !( isNonEmptyIp || !isIndefinite ) );
43 }
44 if ( anonOnlyField ) {
45 anonOnlyField.toggle( !( !isIp && !isEmpty ) );
46 }
47 if ( watchUserField ) {
48 watchUserField.toggle( !( isIpRange && !isEmpty ) );
49 }
50 if ( pageRestrictionsWidget ) {
51 pageRestrictionsWidget.setDisabled( editingRestrictionValue === 'sitewide' );
52 }
53 }
54
55 if ( blockTargetWidget ) {
56 // Bind functions so they're checked whenever stuff changes
57 blockTargetWidget.on( 'change', updateBlockOptions );
58 expiryWidget.on( 'change', updateBlockOptions );
59 editingRestrictionWidget.on( 'change', updateBlockOptions );
60
61 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
62 updateBlockOptions();
63 }
64 } );
65 }() );