b6d9b48164a50fd136d8e8963464de099dca45f4
[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 editingWidget = infuseIfExists( $( '#mw-input-wpEditing' ) ),
23 editingRestrictionWidget = infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
24 preventTalkPageEdit = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) ),
25 pageRestrictionsWidget = infuseIfExists( $( '#mw-input-wpPageRestrictions' ) );
26
27 function updateBlockOptions() {
28 var blocktarget = blockTargetWidget.getValue().trim(),
29 isEmpty = blocktarget === '',
30 isIp = mw.util.isIPAddress( blocktarget, true ),
31 isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
32 isNonEmptyIp = isIp && !isEmpty,
33 expiryValue = expiryWidget.getValue(),
34 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
35 infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
36 isIndefinite = infinityValues.indexOf( expiryValue ) !== -1,
37 editingRestrictionValue = editingRestrictionWidget ? editingRestrictionWidget.getValue() : undefined,
38 editingIsSelected = editingWidget ? editingWidget.isSelected() : false;
39
40 if ( enableAutoblockField ) {
41 enableAutoblockField.toggle( !isNonEmptyIp );
42 }
43 if ( hideUserField ) {
44 hideUserField.toggle( !isNonEmptyIp && isIndefinite );
45 }
46 if ( anonOnlyField ) {
47 anonOnlyField.toggle( isIp || isEmpty );
48 }
49 if ( watchUserField ) {
50 watchUserField.toggle( !isIpRange || isEmpty );
51 }
52 if ( pageRestrictionsWidget ) {
53 editingRestrictionWidget.setDisabled( !editingIsSelected );
54 pageRestrictionsWidget.setDisabled( !editingIsSelected || editingRestrictionValue === 'sitewide' );
55 }
56 if ( preventTalkPageEdit ) {
57 // TODO: (T210475) this option is disabled for partial blocks unless
58 // a namespace restriction for User_talk namespace is in place.
59 // This needs to be updated once Namespace restrictions is available
60 preventTalkPageEdit.setDisabled( editingRestrictionValue === 'partial' && editingIsSelected );
61 }
62
63 }
64
65 if ( blockTargetWidget ) {
66 // Bind functions so they're checked whenever stuff changes
67 blockTargetWidget.on( 'change', updateBlockOptions );
68 expiryWidget.on( 'change', updateBlockOptions );
69 if ( editingRestrictionWidget ) {
70 editingRestrictionWidget.on( 'change', updateBlockOptions );
71 }
72 if ( editingWidget ) {
73 editingWidget.on( 'change', updateBlockOptions );
74 }
75
76 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
77 updateBlockOptions();
78 }
79 } );
80 }() );