Merge "Remove comments documenting constructors as "constructors""
[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 infuseIfExists( $el ) {
7 if ( !$el.length ) {
8 return null;
9 }
10 return OO.ui.infuse( $el );
11 }
12
13 $( function () {
14 // This code is also loaded on the "block succeeded" page where there is no form,
15 // so username and expiry fields might also be missing.
16 var blockTargetWidget = infuseIfExists( $( '#mw-bi-target' ) ),
17 anonOnlyField = infuseIfExists( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
18 enableAutoblockField = infuseIfExists( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
19 hideUserField = infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
20 watchUserField = infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
21 expiryWidget = infuseIfExists( $( '#mw-input-wpExpiry' ) ),
22 editingRestrictionWidget = infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
23 preventTalkPageEdit = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) ),
24 pageRestrictionsWidget = infuseIfExists( $( '#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 if ( preventTalkPageEdit ) {
54 // TODO: (T210475) this option is disabled for partial blocks unless
55 // a namespace restriction for User_talk namespace is in place.
56 // This needs to be updated once Namespace restrictions is available
57 if ( editingRestrictionValue === 'partial' ) {
58 preventTalkPageEdit.setDisabled( true );
59 } else {
60 preventTalkPageEdit.setDisabled( false );
61 }
62 }
63
64 }
65
66 if ( blockTargetWidget ) {
67 // Bind functions so they're checked whenever stuff changes
68 blockTargetWidget.on( 'change', updateBlockOptions );
69 expiryWidget.on( 'change', updateBlockOptions );
70 if ( editingRestrictionWidget ) {
71 editingRestrictionWidget.on( 'change', updateBlockOptions );
72 }
73
74 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
75 updateBlockOptions();
76 }
77 } );
78 }() );