Follow-up r83183, r83202:
authorHappy-melon <happy-melon@users.mediawiki.org>
Sat, 5 Mar 2011 12:48:32 +0000 (12:48 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Sat, 5 Mar 2011 12:48:32 +0000 (12:48 +0000)
* Update SpecialCheckUser.php to new location of IP functions
* Spin out the 'hide-other-field-if-select-box-not-on-other' function as one which should apply to all such fields, especially those created via HTMLForm.  SpecialBlockip and SpecialGlobalBlock should ultimately be converted to use HTMLForm anyway.

includes/HTMLForm.php
includes/specials/SpecialBlockip.php
resources/Resources.php
resources/mediawiki.special/mediawiki.special.block.js
resources/mediawiki/mediawiki.htmlform.js [new file with mode: 0644]

index 2472bc2..f71c7d3 100644 (file)
@@ -184,7 +184,7 @@ class HTMLForm {
                if ( !$class ) {
                        throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
                }
-               
+
                $descriptor['fieldname'] = $fieldname;
 
                $obj = new $class( $descriptor );
@@ -210,7 +210,7 @@ class HTMLForm {
 
        /**
         * Try submitting, with edit token check first
-        * @return Status|boolean 
+        * @return Status|boolean
         */
        function tryAuthorizedSubmit() {
                global $wgUser, $wgRequest;
@@ -368,6 +368,7 @@ class HTMLForm {
 
                # For good measure (it is the default)
                $wgOut->preventClickjacking();
+               $wgOut->addModules( 'mediawiki.htmlform' );
 
                $html = ''
                        . $this->getErrors( $submitResult )
@@ -424,7 +425,6 @@ class HTMLForm {
                global $wgUser;
 
                $html = '';
-               
                if( $this->getMethod() == 'post' ){
                        $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
                        $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
@@ -623,7 +623,7 @@ class HTMLForm {
        function getTitle() {
                return $this->mTitle;
        }
-       
+
        /**
         * Set the method used to submit the form
         * @param $method String
@@ -631,7 +631,7 @@ class HTMLForm {
        public function setMethod( $method='post' ){
                $this->mMethod = $method;
        }
-       
+
        public function getMethod(){
                return $this->mMethod;
        }
@@ -840,12 +840,12 @@ abstract class HTMLFormField {
                if ( isset( $params['name'] ) ) {
                        $this->mName = $params['name'];
                }
-               
+
                $validName = Sanitizer::escapeId( $this->mName );
                if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
                        throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
                }
-               
+
                $this->mID = "mw-input-{$this->mName}";
 
                if ( isset( $params['default'] ) ) {
@@ -887,10 +887,10 @@ abstract class HTMLFormField {
                global $wgRequest;
 
                $errors = $this->validate( $value, $this->mParent->mFieldData );
-               
+
                $cellAttributes = array();
                $verticalLabel = false;
-               
+
                if ( !empty($this->mParams['vertical-label']) ) {
                        $cellAttributes['colspan'] = 2;
                        $verticalLabel = true;
@@ -908,9 +908,9 @@ abstract class HTMLFormField {
                        array( 'class' => 'mw-input' ) + $cellAttributes,
                        $this->getInputHTML( $value ) . "\n$errors"
                );
-               
+
                $fieldType = get_class( $this );
-               
+
                if ($verticalLabel) {
                        $html = Html::rawElement( 'tr',
                                array( 'class' => 'mw-htmlform-vertical-label' ), $label );
@@ -1139,11 +1139,11 @@ class HTMLFloatField extends HTMLTextField {
                if ( $p !== true ) {
                        return $p;
                }
-               
+
                $value = trim( $value );
 
                # http://dev.w3.org/html5/spec/common-microsyntaxes.html#real-numbers
-               # with the addition that a leading '+' sign is ok. 
+               # with the addition that a leading '+' sign is ok.
                if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
                        return wfMsgExt( 'htmlform-float-invalid', 'parse' );
                }
@@ -1182,8 +1182,8 @@ class HTMLIntField extends HTMLFloatField {
                }
 
                # http://dev.w3.org/html5/spec/common-microsyntaxes.html#signed-integers
-               # with the addition that a leading '+' sign is ok. Note that leading zeros 
-               # are fine, and will be left in the input, which is useful for things like 
+               # with the addition that a leading '+' sign is ok. Note that leading zeros
+               # are fine, and will be left in the input, which is useful for things like
                # phone numbers when you know that they are integers (the HTML5 type=tel
                # input does not require its value to be numeric).  If you want a tidier
                # value to, eg, save in the DB, clean it up with intval().
@@ -1415,8 +1415,8 @@ class HTMLMultiSelectField extends HTMLFormField {
                        } else {
                                $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
 
-                               $checkbox = Xml::check( 
-                                       $this->mName . '[]', 
+                               $checkbox = Xml::check(
+                                       $this->mName . '[]',
                                        in_array( $info, $value, true ),
                                        $attribs + $thisAttribs );
                                $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
@@ -1556,7 +1556,7 @@ class HTMLInfoField extends HTMLFormField {
 class HTMLHiddenField extends HTMLFormField {
        public function __construct( $params ) {
                parent::__construct( $params );
-               
+
                # Per HTML5 spec, hidden fields cannot be 'required'
                # http://dev.w3.org/html5/spec/states-of-the-type-attribute.html#hidden-state
                unset( $this->mParams['required'] );
@@ -1605,7 +1605,7 @@ class HTMLSubmitField extends HTMLFormField {
        protected function needsLabel() {
                return false;
        }
-       
+
        /**
         * Button cannot be invalid
         */
index 65bb294..1e2fd5e 100644 (file)
@@ -199,7 +199,9 @@ class IPBlockForm extends SpecialPage {
                        wfMsgForContent( 'ipbreason-dropdown' ),
                        wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
 
-               $wgOut->addModules( 'mediawiki.special.block' );
+               # FIXME: this should actually use HTMLForm, not just some of its JavaScript
+               $wgOut->addModules( array( 'mediawiki.special.block', 'mediawiki.htmlform' ) );
+               
                $wgOut->addHTML(
                        Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'blockip' ) ) .
                        Xml::openElement( 'fieldset' ) .
@@ -218,32 +220,34 @@ class IPBlockForm extends SpecialPage {
                                        ) + ( $this->BlockAddress ? array() : array( 'autofocus' ) ) ). "
                                </td>
                        </tr>
-                       <tr>"
-               );
-               if( $showblockoptions ) {
-                       $wgOut->addHTML("
+                       <tr>
                                <td class='mw-label'>
                                        {$mIpbexpiry}
                                </td>
-                               <td class='mw-input'>" .
+                               <td class='mw-input'>"
+               );
+               if( $showblockoptions ) {
+                       $wgOut->addHTML(
                                        Xml::tags( 'select',
                                                array(
                                                        'id' => 'wpBlockExpiry',
                                                        'name' => 'wpBlockExpiry',
+                                                       'class' => 'mw-htmlform-select-or-other', # FIXME: actually make this use HTMLForm
                                                        'tabindex' => '2' ),
-                                               $blockExpiryFormOptions ) .
-                               "</td>"
+                                               $blockExpiryFormOptions
+                                       ) . "<br/>\n"
                        );
                }
-               $wgOut->addHTML("
-                       </tr>
-                       <tr id='wpBlockOther'>
-                               <td class='mw-label'>
-                                       {$mIpbother}
-                               </td>
-                               <td class='mw-input'>" .
-                                       Xml::input( 'wpBlockOther', 45, $this->BlockOther,
-                                               array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
+               $wgOut->addHTML(
+                                       Xml::input(
+                                               'wpBlockOther',
+                                               45,
+                                               $this->BlockOther,
+                                               array(
+                                                       'tabindex' => '3',
+                                                       'id' => 'wpBlockExpiry-other'
+                                               )
+                                       ) . "
                                </td>
                        </tr>
                        <tr>
index a9b1002..6e558e6 100644 (file)
@@ -382,6 +382,9 @@ return array(
                'debugScripts' => 'resources/mediawiki/mediawiki.log.js',
                'debugRaw' => false,
        ),
+       'mediawiki.htmlform' => array(
+               'scripts' => 'resources/mediawiki/mediawiki.htmlform.js',
+       ),
        'mediawiki.util' => array(
                'scripts' => 'resources/mediawiki.util/mediawiki.util.js',
                'dependencies' => array(
index ed0fa61..d7ed89a 100644 (file)
@@ -1,34 +1,12 @@
 /* JavaScript for Special:Block */
 
-// Fade or snap depending on argument
-jQuery.fn.goIn = function( instantToggle ) {
-       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
-               return jQuery(this).show();
-       }
-       return jQuery(this).stop( true, true ).fadeIn();
-};
-jQuery.fn.goOut = function( instantToggle ) {
-       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
-               return jQuery(this).hide();
-       }
-       return jQuery(this).stop( true, true ).fadeOut();
-};
-
 jQuery( function( $ ) {
 
        var     DO_INSTANT = true,
-               $blockExpiry = $( '#wpBlockExpiry' ),   $blockOther = $( '#wpBlockOther' ),
                $blockTarget = $( '#mw-bi-target' ),    $anonOnlyRow = $( '#wpAnonOnlyRow' ),
                $enableAutoblockRow = $( '#wpEnableAutoblockRow' ),
                $hideUser = $( '#wpEnableHideUser' ),   $watchUser = $( '#wpEnableWatchUser' );
 
-       var considerChangingExpiryFocus = function( instant ) {
-               if ( $blockExpiry.val() == 'other' ) {
-                       $blockOther.goIn( instant );
-               } else {
-                       $blockOther.goOut( instant );
-               }
-       };
        var updateBlockOptions = function( instant ) {
                if ( !$blockTarget.length ) {
                        return;
@@ -59,10 +37,8 @@ jQuery( function( $ ) {
        };
 
        // Bind functions so they're checked whenever stuff changes
-       $blockExpiry.change( considerChangingExpiryFocus );
        $blockTarget.keyup( updateBlockOptions );
 
        // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
-       considerChangingExpiryFocus( DO_INSTANT );
        updateBlockOptions( DO_INSTANT );
 });
\ No newline at end of file
diff --git a/resources/mediawiki/mediawiki.htmlform.js b/resources/mediawiki/mediawiki.htmlform.js
new file mode 100644 (file)
index 0000000..d4265e0
--- /dev/null
@@ -0,0 +1,46 @@
+/**
+ * Utility functions for jazzing up HTMLForm elements
+ */
+
+// Fade or snap depending on argument
+jQuery.fn.goIn = function( instantToggle ) {
+       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
+               return $(this).show();
+       }
+       return jQuery(this).stop( true, true ).fadeIn();
+};
+jQuery.fn.goOut = function( instantToggle ) {
+       if ( typeof instantToggle != 'undefined' && instantToggle === true ) {
+               return $(this).hide();
+       }
+       return jQuery(this).stop( true, true ).fadeOut();
+};
+
+/**
+ * Bind a function to the jQuery object via live(), and also immediately trigger
+ * the function on the objects with an 'instant' paramter set to true
+ * @param callback function taking one paramter, which is Bool true when the event
+ *     is called immediately, and the EventArgs object when triggered from an event
+ */
+jQuery.fn.liveAndTestAtStart = function( callback ){
+       $(this)
+               .live( 'change', callback )
+               .each( function( index, element ){
+                       callback.call( this, true );
+               } );
+}
+
+jQuery( function( $ ) {
+
+       // animate the SelectOrOther fields, to only show the text field when
+       // 'other' is selected
+       $( '.mw-htmlform-select-or-other' ).liveAndTestAtStart( function( instant ){
+               var $other = $( '#' + $(this).attr( 'id' ) + '-other' );
+               if ( $(this).val() == 'other' ) {
+                       $other.goIn( instant );
+               } else {
+                       $other.goOut( instant );
+               }
+       });
+
+});
\ No newline at end of file