Merge "RCFilters: Convert saved queries from filters to parameters"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.block.js
index aca335e..491a1ff 100644 (file)
@@ -2,44 +2,60 @@
  * JavaScript for Special:Block
  */
 ( function ( mw, $ ) {
+       // Like OO.ui.infuse(), but if the element doesn't exist, return null instead of throwing an exception.
+       function infuseOrNull( elem ) {
+               try {
+                       return OO.ui.infuse( elem );
+               } catch ( er ) {
+                       return null;
+               }
+       }
+
        $( function () {
-               var $blockTarget = $( '#mw-bi-target' ),
-                       $anonOnlyRow = $( '#mw-input-wpHardBlock' ).closest( 'tr' ),
-                       $enableAutoblockRow = $( '#mw-input-wpAutoBlock' ).closest( 'tr' ),
-                       $hideUser = $( '#mw-input-wpHideUser' ).closest( 'tr' ),
-                       $watchUser = $( '#mw-input-wpWatch' ).closest( 'tr' );
+               // This code is also loaded on the "block succeeded" page where there is no form,
+               // so username and expiry fields might also be missing.
+               var blockTargetWidget = infuseOrNull( 'mw-bi-target' ),
+                       anonOnlyField = infuseOrNull( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
+                       enableAutoblockField = infuseOrNull( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
+                       hideUserField = infuseOrNull( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
+                       watchUserField = infuseOrNull( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
+                       // mw.widgets.SelectWithInputWidget
+                       expiryWidget = infuseOrNull( 'mw-input-wpExpiry' );
 
-               function updateBlockOptions( instant ) {
-                       var blocktarget = $.trim( $blockTarget.val() ),
+               function updateBlockOptions() {
+                       var blocktarget = $.trim( blockTargetWidget.getValue() ),
                                isEmpty = blocktarget === '',
                                isIp = mw.util.isIPAddress( blocktarget, true ),
-                               isIpRange = isIp && blocktarget.match( /\/\d+$/ );
+                               isIpRange = isIp && blocktarget.match( /\/\d+$/ ),
+                               isNonEmptyIp = isIp && !isEmpty,
+                               expiryValue = expiryWidget.dropdowninput.getValue(),
+                               // infinityValues  are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
+                               infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
+                               isIndefinite = $.inArray( expiryValue, infinityValues ) !== -1 ||
+                                       ( expiryValue === 'other' && $.inArray( expiryWidget.textinput.getValue(), infinityValues ) !== -1 );
 
-                       if ( isIp && !isEmpty ) {
-                               $enableAutoblockRow.goOut( instant );
-                               $hideUser.goOut( instant );
-                       } else {
-                               $enableAutoblockRow.goIn( instant );
-                               $hideUser.goIn( instant );
+                       if ( enableAutoblockField ) {
+                               enableAutoblockField.toggle( !( isNonEmptyIp ) );
+                       }
+                       if ( hideUserField ) {
+                               hideUserField.toggle( !( isNonEmptyIp || !isIndefinite ) );
                        }
-                       if ( !isIp && !isEmpty ) {
-                               $anonOnlyRow.goOut( instant );
-                       } else {
-                               $anonOnlyRow.goIn( instant );
+                       if ( anonOnlyField ) {
+                               anonOnlyField.toggle( !( !isIp && !isEmpty ) );
                        }
-                       if ( isIpRange && !isEmpty ) {
-                               $watchUser.goOut( instant );
-                       } else {
-                               $watchUser.goIn( instant );
+                       if ( watchUserField ) {
+                               watchUserField.toggle( !( isIpRange && !isEmpty ) );
                        }
                }
 
-               if ( $blockTarget.length ) {
+               if ( blockTargetWidget ) {
                        // Bind functions so they're checked whenever stuff changes
-                       $blockTarget.keyup( updateBlockOptions );
+                       blockTargetWidget.on( 'change', updateBlockOptions );
+                       expiryWidget.dropdowninput.on( 'change', updateBlockOptions );
+                       expiryWidget.textinput.on( 'change', updateBlockOptions );
 
                        // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
-                       updateBlockOptions( /* instant= */ true );
+                       updateBlockOptions();
                }
        } );
 }( mediaWiki, jQuery ) );