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