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