From: Happy-melon Date: Sat, 5 Mar 2011 12:48:32 +0000 (+0000) Subject: Follow-up r83183, r83202: X-Git-Tag: 1.31.0-rc.0~31643 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=1c866270b315fe5787a3a5711836a69374585190;p=lhc%2Fweb%2Fwiklou.git Follow-up r83183, r83202: * 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. --- diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 2472bc2d60..f71c7d3e7f 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -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 .= ' ' . 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 */ diff --git a/includes/specials/SpecialBlockip.php b/includes/specials/SpecialBlockip.php index 65bb294ac5..1e2fd5eea3 100644 --- a/includes/specials/SpecialBlockip.php +++ b/includes/specials/SpecialBlockip.php @@ -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' ) ) ). " - " - ); - if( $showblockoptions ) { - $wgOut->addHTML(" + {$mIpbexpiry} - " . + " + ); + 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 ) . - "" + $blockExpiryFormOptions + ) . "
\n" ); } - $wgOut->addHTML(" - - - - {$mIpbother} - - " . - 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' + ) + ) . " diff --git a/resources/Resources.php b/resources/Resources.php index a9b1002600..6e558e6c57 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -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( diff --git a/resources/mediawiki.special/mediawiki.special.block.js b/resources/mediawiki.special/mediawiki.special.block.js index ed0fa61beb..d7ed89aefb 100644 --- a/resources/mediawiki.special/mediawiki.special.block.js +++ b/resources/mediawiki.special/mediawiki.special.block.js @@ -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 index 0000000000..d4265e06a0 --- /dev/null +++ b/resources/mediawiki/mediawiki.htmlform.js @@ -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