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