Merge "Add BeforeResetNotificationTimestamp hook"
[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 namespaceRestrictionsWidget = infuseIfExists( $( '#mw-input-wpNamespaceRestrictions' ) );
27
28 function updateBlockOptions() {
29 var blocktarget = blockTargetWidget.getValue().trim(),
30 isEmpty = blocktarget === '',
31 isIp = mw.util.isIPAddress( blocktarget, true ),
32 isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
33 isNonEmptyIp = isIp && !isEmpty,
34 expiryValue = expiryWidget.getValue(),
35 // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
36 infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
37 isIndefinite = infinityValues.indexOf( expiryValue ) !== -1,
38 editingRestrictionValue = editingRestrictionWidget ? editingRestrictionWidget.getValue() : undefined,
39 editingIsSelected = editingWidget ? editingWidget.isSelected() : false;
40
41 if ( enableAutoblockField ) {
42 enableAutoblockField.toggle( !isNonEmptyIp );
43 }
44 if ( hideUserField ) {
45 hideUserField.toggle( !isNonEmptyIp && isIndefinite );
46 }
47 if ( anonOnlyField ) {
48 anonOnlyField.toggle( isIp || isEmpty );
49 }
50 if ( watchUserField ) {
51 watchUserField.toggle( !isIpRange || isEmpty );
52 }
53 if ( editingRestrictionWidget ) {
54 editingRestrictionWidget.setDisabled( !editingIsSelected );
55 }
56 if ( pageRestrictionsWidget ) {
57 pageRestrictionsWidget.setDisabled( !editingIsSelected || editingRestrictionValue === 'sitewide' );
58 }
59 if ( namespaceRestrictionsWidget ) {
60 namespaceRestrictionsWidget.setDisabled( !editingIsSelected || editingRestrictionValue === 'sitewide' );
61 }
62 if ( preventTalkPageEdit && namespaceRestrictionsWidget ) {
63 // This option is disabled for partial blocks unless a namespace restriction
64 // for the User_talk namespace is in place.
65 preventTalkPageEdit.setDisabled(
66 editingIsSelected &&
67 editingRestrictionValue === 'partial' &&
68 namespaceRestrictionsWidget.getValue().indexOf(
69 String( mw.config.get( 'wgNamespaceIds' ).user_talk )
70 ) === -1
71 );
72 }
73
74 }
75
76 if ( blockTargetWidget ) {
77 // Bind functions so they're checked whenever stuff changes
78 blockTargetWidget.on( 'change', updateBlockOptions );
79 expiryWidget.on( 'change', updateBlockOptions );
80 if ( editingWidget ) {
81 editingWidget.on( 'change', updateBlockOptions );
82 }
83 if ( editingRestrictionWidget ) {
84 editingRestrictionWidget.on( 'change', updateBlockOptions );
85 }
86 if ( namespaceRestrictionsWidget ) {
87 namespaceRestrictionsWidget.on( 'change', updateBlockOptions );
88 }
89
90 // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
91 updateBlockOptions();
92 }
93 } );
94 }() );