http://www.mediawiki.org/wiki/Special:Code/MediaWiki/57867
Revision: 57867 Author: btongminh Date: 2009-10-18 19:29:35 +0000 (Sun, 18 Oct 2009) Log Message: ----------- Update HTMLForm for upcoming Special:Upload rewrite. * Add support for edittools * Add support for multipart/form-data * Set id for wpEditToken * Add support for tooltip and accesskey * Allow setting a name for the submit button * Give sections an id Modified Paths: -------------- trunk/phase3/includes/HTMLForm.php trunk/phase3/includes/Linker.php Modified: trunk/phase3/includes/HTMLForm.php =================================================================== --- trunk/phase3/includes/HTMLForm.php 2009-10-18 19:15:28 UTC (rev 57866) +++ trunk/phase3/includes/HTMLForm.php 2009-10-18 19:29:35 UTC (rev 57867) @@ -63,6 +63,7 @@ 'selectorother' => 'HTMLSelectOrOtherField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', + 'edittools' => 'HTMLEditTools', # HTMLTextField will output the correct type="" attribute automagically. # There are about four zillion other HTML 5 input types, like url, but @@ -83,11 +84,15 @@ protected $mPre = ''; protected $mHeader = ''; protected $mPost = ''; + protected $mId; protected $mSubmitID; + protected $mSubmitName; protected $mSubmitText; + protected $mSubmitTooltip; protected $mTitle; + protected $mUseMultipart = false; protected $mHiddenFields = array(); protected $mButtons = array(); @@ -112,6 +117,9 @@ $info['name'] = $fieldname; + if ( isset( $info['type'] ) && $info['type'] == 'file' ) + $this->mUseMultipart = true; + $field = self::loadInputFromParameters( $info ); $field->mParent = $this; @@ -287,8 +295,8 @@ $this->mHiddenFields[ $name ] = $value; } - public function addButton( $name, $value, $id=null ){ - $this->mButtons[] = compact( 'name', 'value', 'id' ); + public function addButton( $name, $value, $id=null, $attribs=null ){ + $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' ); } /** @@ -327,19 +335,24 @@ function wrapForm( $html ) { # Include a <fieldset> wrapper for style, if requested. - if( $this->mWrapperLegend !== false ){ + if ( $this->mWrapperLegend !== false ){ $html = Xml::fieldset( $this->mWrapperLegend, $html ); } - - return Html::rawElement( - 'form', - array( - 'action' => $this->getTitle()->getFullURL(), - 'method' => 'post', - 'class' => 'visualClear', - ), - $html + # Use multipart/form-data + $encType = $this->mUseMultipart + ? 'multipart/form-data' + : 'application/x-www-form-urlencoded'; + # Attributes + $attribs = array( + 'action' => $this->getTitle()->getFullURL(), + 'method' => 'post', + 'class' => 'visualClear', + 'enctype' => $encType, ); + if ( !empty( $this->mId ) ) + $attribs['id'] = $this->mId; + + return Html::rawElement( 'form', $attribs, $html ); } /** @@ -350,7 +363,7 @@ global $wgUser; $html = ''; - $html .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n"; + $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n"; $html .= Html::hidden( 'title', $this->getTitle() ) . "\n"; foreach( $this->mHiddenFields as $name => $value ){ @@ -371,6 +384,12 @@ if ( isset( $this->mSubmitID ) ) $attribs['id'] = $this->mSubmitID; + if ( isset( $this->mSubmitName ) ) + $attribs['name'] = $this->mSubmitName; + if ( isset( $this->mSubmitTooltip ) ) { + global $wgUser; + $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip ); + } $attribs['class'] = 'mw-htmlform-submit'; @@ -392,6 +411,8 @@ 'name' => $button['name'], 'value' => $button['value'] ); + if ( $button['attribs'] ) + $attrs += $button['attribs']; if( isset( $button['id'] ) ) $attrs['id'] = $button['id']; $html .= Html::element( 'input', $attrs ); @@ -467,7 +488,16 @@ ? $this->mSubmitText : wfMsg( 'htmlform-submit' ); } + + public function setSubmitName( $name ) { + $this->mSubmitName = $name; + } + + public function setSubmitTooltip( $name ) { + $this->mSubmitTooltip = $name; + } + /** * Set the id for the submit button. * @param $t String. FIXME: Integrity is *not* validated @@ -476,6 +506,9 @@ $this->mSubmitID = $t; } + public function setId( $id ) { + $this->mId = $id; + } /** * Prompt the whole form to be wrapped in a <fieldset>, with * this text as its <legend> element. @@ -514,7 +547,7 @@ * TODO: Document * @param $fields */ - function displaySection( $fields ) { + function displaySection( $fields, $sectionName = '' ) { $tableHtml = ''; $subsectionHtml = ''; $hasLeftColumn = false; @@ -529,7 +562,7 @@ if( $value->getLabel() != ' ' ) $hasLeftColumn = true; } elseif ( is_array( $value ) ) { - $section = $this->displaySection( $value ); + $section = $this->displaySection( $value, $key ); $legend = wfMsg( "{$this->mMessagePrefix}-$key" ); $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n"; } @@ -538,9 +571,13 @@ $classes = array(); if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist $classes[] = 'mw-htmlform-nolabel'; - $classes = implode( ' ', $classes ); + $attribs = array( + 'classes' => implode( ' ', $classes ), + ); + if ( $sectionName ) + $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" ); - $tableHtml = Html::rawElement( 'table', array( 'class' => $classes ), + $tableHtml = Html::rawElement( 'table', $attribs, Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n"; return $subsectionHtml . "\n" . $tableHtml; @@ -791,7 +828,20 @@ return null; } } + + /** + * Returns the attributes required for the tooltip and accesskey. + * + * @return array Attributes + */ + public function getTooltipAndAccessKey() { + if ( empty( $this->mParams['tooltip'] ) ) + return array(); + global $wgUser; + return $wgUser->getSkin()->tooltipAndAccessKeyAttribs(); + } + /** * flatten an array of options to a single array, for instance, * a set of <options> inside <optgroups>. @@ -812,6 +862,7 @@ return $flatOpts; } + } class HTMLTextField extends HTMLFormField { @@ -829,7 +880,7 @@ 'name' => $this->mName, 'size' => $this->getSize(), 'value' => $value, - ); + ) + $this->getTooltipAndAccessKey(); if ( isset( $this->mParams['maxlength'] ) ) { $attribs['maxlength'] = $this->mParams['maxlength']; @@ -908,7 +959,7 @@ 'name' => $this->mName, 'cols' => $this->getCols(), 'rows' => $this->getRows(), - ); + ) + $this->getTooltipAndAccessKey(); if ( !empty( $this->mParams['disabled'] ) ) { @@ -996,7 +1047,8 @@ if ( !empty( $this->mParams['invert'] ) ) $value = !$value; - $attr = array( 'id' => $this->mID ); + $attr = $this->getTooltipAndAccessKey(); + $attr['id'] = $this->mID; if( !empty( $this->mParams['disabled'] ) ) { $attr['disabled'] = 'disabled'; } @@ -1329,9 +1381,22 @@ $this->mParent->addButton( $this->mParams['name'], $this->mParams['default'], - isset($this->mParams['id']) ? $this->mParams['id'] : null + isset($this->mParams['id']) ? $this->mParams['id'] : null, + $this->getTooltipAndAccessKey() ); } public function getInputHTML( $value ){ return ''; } +} + +class HTMLEditTools extends HTMLFormField { + public function getInputHTML( $value ) { + return ''; + } + public function getTableRow( $value ) { + return "<tr><td></td><td class=\"mw-input\">" + . '<div class="mw-editTools">' + . wfMsgForContent( 'edittools' ) + . "</div></td></tr>\n"; + } } \ No newline at end of file Modified: trunk/phase3/includes/Linker.php =================================================================== --- trunk/phase3/includes/Linker.php 2009-10-18 19:15:28 UTC (rev 57866) +++ trunk/phase3/includes/Linker.php 2009-10-18 19:29:35 UTC (rev 57867) @@ -1938,9 +1938,9 @@ } /** - * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey() + * Returns the attributes for the tooltip and access key */ - public function tooltipAndAccesskey( $name ) { + public function tooltipAndAccesskeyAttribs( $name ) { global $wgEnableTooltipsAndAccesskeys; if ( !$wgEnableTooltipsAndAccesskeys ) return ''; @@ -1957,8 +1957,15 @@ if ( $attribs['accesskey'] === false ) { unset( $attribs['accesskey'] ); } - return Xml::expandAttributes( $attribs ); + return $attribs; } + /** + * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey() + */ + public function tooltipAndAccesskey( $name ) { + return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) ); + } + /** @deprecated Returns raw bits of HTML, use titleAttrib() */ public function tooltip( $name, $options = null ) { _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs