From: jdlrobson Date: Mon, 29 Sep 2014 23:45:45 +0000 (-0700) Subject: Hygiene: Make construction of buttons easier X-Git-Tag: 1.31.0-rc.0~13712^2 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=a155ac55ea0854b0989efe23158d33b5a548f14e;p=lhc%2Fweb%2Fwiklou.git Hygiene: Make construction of buttons easier Stop littering MediaWiki with globals, provide a common api for generating them similar to how we do text input attributes before things get out of control. Adds * submitButton * linkButton Change-Id: I61bb3c358f755ed9f2153d94b744c1a9da02c456 --- diff --git a/includes/EditPage.php b/includes/EditPage.php index 8a55b1df6d..0674dcd904 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -3190,7 +3190,7 @@ HTML } protected function showStandardInputs( &$tabindex = 2 ) { - global $wgOut, $wgUseMediaWikiUIEverywhere; + global $wgOut; $wgOut->addHTML( "
\n" ); if ( $this->section != 'new' ) { @@ -3222,10 +3222,8 @@ HTML 'target' => 'helpwindow', 'href' => $edithelpurl, ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attrs['class'] = 'mw-ui-button mw-ui-quiet'; - } - $edithelp = Html::element( 'a', $attrs, wfMessage( 'edithelp' )->text() ) . + $edithelp = Html::linkButton( wfMessage( 'edithelp' )->text(), + $attrs, array( 'mw-ui-quiet' ) ) . wfMessage( 'word-separator' )->escaped() . wfMessage( 'newwindow' )->parse(); @@ -3268,20 +3266,16 @@ HTML * @return string */ public function getCancelLink() { - global $wgUseMediaWikiUIEverywhere; $cancelParams = array(); if ( !$this->isConflict && $this->oldid > 0 ) { $cancelParams['oldid'] = $this->oldid; } $attrs = array( 'id' => 'mw-editform-cancel' ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attrs['class'] = 'mw-ui-button mw-ui-quiet'; - } return Linker::linkKnown( $this->getContextTitle(), wfMessage( 'cancel' )->parse(), - $attrs, + Html::buttonAttributes( $attrs, array( 'mw-ui-quiet' ) ), $cancelParams ); } @@ -3750,47 +3744,33 @@ HTML * @return array */ public function getEditButtons( &$tabindex ) { - global $wgUseMediaWikiUIEverywhere; - $buttons = array(); $attribs = array( 'id' => 'wpSave', 'name' => 'wpSave', - 'type' => 'submit', 'tabindex' => ++$tabindex, - 'value' => wfMessage( 'savearticle' )->text(), ) + Linker::tooltipAndAccesskeyAttribs( 'save' ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attribs['class'] = 'mw-ui-button mw-ui-constructive'; - } - $buttons['save'] = Xml::element( 'input', $attribs, '' ); + $buttons['save'] = Html::submitButton( wfMessage( 'savearticle' )->text(), + $attribs, array( 'mw-ui-constructive' ) ); ++$tabindex; // use the same for preview and live preview $attribs = array( 'id' => 'wpPreview', 'name' => 'wpPreview', - 'type' => 'submit', 'tabindex' => $tabindex, - 'value' => wfMessage( 'showpreview' )->text(), ) + Linker::tooltipAndAccesskeyAttribs( 'preview' ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attribs['class'] = 'mw-ui-button mw-ui-progressive'; - } - $buttons['preview'] = Xml::element( 'input', $attribs, '' ); + $buttons['preview'] = Html::submitButton( wfMessage( 'showpreview' )->text(), + $attribs, array( 'mw-ui-progressive' ) ); $buttons['live'] = ''; $attribs = array( 'id' => 'wpDiff', 'name' => 'wpDiff', - 'type' => 'submit', 'tabindex' => ++$tabindex, - 'value' => wfMessage( 'showdiff' )->text(), ) + Linker::tooltipAndAccesskeyAttribs( 'diff' ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attribs['class'] = 'mw-ui-button mw-ui-progressive'; - } - $buttons['diff'] = Xml::element( 'input', $attribs, '' ); + $buttons['diff'] = Html::submitButton( wfMessage( 'showdiff' )->text(), + $attribs, array( 'mw-ui-progressive' ) ); wfRunHooks( 'EditPageBeforeEditButtons', array( &$this, &$buttons, &$tabindex ) ); return $buttons; diff --git a/includes/Html.php b/includes/Html.php index 1e16e39430..fc5916da53 100644 --- a/includes/Html.php +++ b/includes/Html.php @@ -101,6 +101,34 @@ class Html { 'itemscope', ); + /** + * Modifies a set of attributes meant for button elements + * and apply a set of default attributes when $wgUseMediaWikiUIEverywhere enabled. + * @param array $modifiers to add to the button + * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers + * @return array $attrs A modified attribute array + */ + public static function buttonAttributes( $attrs, $modifiers = array() ) { + global $wgUseMediaWikiUIEverywhere; + if ( $wgUseMediaWikiUIEverywhere ) { + if ( isset( $attrs['class'] ) ) { + if ( is_array( $attrs['class'] ) ) { + $attrs['class'][] = 'mw-ui-button'; + $attrs = array_merge( $attrs, $modifiers ); + // ensure compatibility with Xml + $attrs['class'] = implode( ' ', $attrs['class'] ); + } else { + $attrs['class'] .= ' mw-ui-button ' . implode( ' ', $modifiers ); + } + } else { + $attrs['class'] = array( 'mw-ui-button' ); + // ensure compatibility with Xml + $attrs['class'] = implode( ' ', array_merge( $attrs['class'], $modifiers ) ); + } + } + return $attrs; + } + /** * Modifies a set of attributes meant for text input elements * and apply a set of default attributes. @@ -130,6 +158,43 @@ class Html { return $attrs; } + /** + * Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled). + * + * @param string $contents The raw HTML contents of the element: *not* + * escaped! + * @param array $attrs Associative array of attributes, e.g., array( + * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for + * further documentation. + * @param array $modifiers to add to the button + * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers + * @return string Raw HTML + */ + public static function linkButton( $contents, $attrs, $modifiers = array() ) { + return Html::element( 'a', + self::buttonAttributes( $attrs, $modifiers ), + $contents + ); + } + + /** + * Returns an HTML link element in a string styled as a button (when $wgUseMediaWikiUIEverywhere is enabled). + * + * @param string $contents The raw HTML contents of the element: *not* + * escaped! + * @param array $attrs Associative array of attributes, e.g., array( + * 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for + * further documentation. + * @param array $modifiers to add to the button + * @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers + * @return string Raw HTML + */ + public static function submitButton( $contents, $attrs, $modifiers = array() ) { + $attrs['type'] = 'submit'; + $attrs['value'] = $contents; + return Html::element( 'input', self::buttonAttributes( $attrs, $modifiers ) ); + } + /** * Returns an HTML element in a string. The major advantage here over * manually typing out the HTML is that it will escape all attribute diff --git a/includes/Preferences.php b/includes/Preferences.php index 8fa2f9adda..582a4a5bd5 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -1518,12 +1518,8 @@ class PreferencesForm extends HTMLForm { * @return string */ function getButtons() { - global $wgUseMediaWikiUIEverywhere; $attrs = array( 'id' => 'mw-prefs-restoreprefs' ); - if ( $wgUseMediaWikiUIEverywhere ) { - $attrs['class'] = 'mw-ui-button mw-ui-quiet'; - } if ( !$this->getModifiedUser()->isAllowedAny( 'editmyprivateinfo', 'editmyoptions' ) ) { return ''; @@ -1535,7 +1531,7 @@ class PreferencesForm extends HTMLForm { $t = SpecialPage::getTitleFor( 'Preferences', 'reset' ); $html .= "\n" . Linker::link( $t, $this->msg( 'restoreprefs' )->escaped(), - $attrs ); + Html::buttonAttributes( $attrs, array( 'mw-ui-quiet' ) ) ); $html = Xml::tags( 'div', array( 'class' => 'mw-prefs-buttons' ), $html ); } diff --git a/includes/actions/HistoryAction.php b/includes/actions/HistoryAction.php index 99006ed10d..dd0c94c710 100644 --- a/includes/actions/HistoryAction.php +++ b/includes/actions/HistoryAction.php @@ -182,9 +182,6 @@ class HistoryAction extends FormlessAction { // Add the general form $action = htmlspecialchars( wfScript() ); $className = 'historysubmit mw-history-compareselectedversions-button'; - if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) { - $className .= ' mw-ui-button mw-ui-progressive'; - } $out->addHTML( "
" . Xml::fieldset( @@ -200,9 +197,10 @@ class HistoryAction extends FormlessAction { ) . ' ' . ( $tagSelector ? ( implode( ' ', $tagSelector ) . ' ' ) : '' ) . $checkDeleted . - Xml::submitButton( + Html::submitButton( $this->msg( 'allpagessubmit' )->text(), - array( 'class' => $className ) + $attrs, + array( 'mw-ui-progressive' ) ) . "\n" . '
' ); @@ -492,12 +490,10 @@ class HistoryPager extends ReverseChronologicalPager { // Button container stored in $this->buttons for re-use in getEndBody() $this->buttons = '
'; $className = 'historysubmit mw-history-compareselectedversions-button'; - if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) { - $className .= ' mw-ui-button'; - } + $attrs = array( 'class' => $className ) + + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' ); $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(), - array( 'class' => $className ) - + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' ) + $attrs ) . "\n"; if ( $this->getUser()->isAllowed( 'deleterevision' ) ) { @@ -568,7 +564,7 @@ class HistoryPager extends ReverseChronologicalPager { function submitButton( $message, $attributes = array() ) { # Disable submit button if history has 1 revision only if ( $this->getNumRows() > 1 ) { - return Xml::submitButton( $message, $attributes ); + return Html::submitButton( $message, $attributes ); } else { return ''; } diff --git a/includes/specials/SpecialBooksources.php b/includes/specials/SpecialBooksources.php index e6750e125e..7395b8e656 100644 --- a/includes/specials/SpecialBooksources.php +++ b/includes/specials/SpecialBooksources.php @@ -134,16 +134,10 @@ class SpecialBookSources extends SpecialPage { array( 'autofocus' => true, 'class' => 'mw-ui-input-inline' ) ); - if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) { - $form .= ' ' . Xml::submitButton( - $this->msg( 'booksources-search' )->text(), - array( 'class' => 'mw-ui-button mw-ui-progressive' ) - ) . "

\n"; - } else { - $form .= ' ' . Xml::submitButton( - $this->msg( 'booksources-search' )->text() - ) . "

\n"; - } + $form .= ' ' . Html::submitButton( + $this->msg( 'booksources-search' )->text(), + array(), array( 'mw-ui-progressive' ) + ) . "

\n"; $form .= Html::closeElement( 'form' ) . "\n"; $form .= Html::closeElement( 'fieldset' ) . "\n"; diff --git a/includes/specials/SpecialCategories.php b/includes/specials/SpecialCategories.php index bf6bb35e9a..3a13b7ed60 100644 --- a/includes/specials/SpecialCategories.php +++ b/includes/specials/SpecialCategories.php @@ -180,10 +180,6 @@ class CategoryPager extends AlphabeticPager { } public function getStartForm( $from ) { - $submitClassName = ''; - if ( $this->getConfig( 'UseMediaWikiUIEverywhere' ) ) { - $submitClassName = 'mw-ui-button mw-ui-progressive'; - } return Xml::tags( 'form', array( 'method' => 'get', 'action' => wfScript() ), @@ -194,9 +190,9 @@ class CategoryPager extends AlphabeticPager { $this->msg( 'categoriesfrom' )->text(), 'from', 'from', 20, $from, array( 'class' => 'mw-ui-input-inline' ) ) . ' ' . - Xml::submitButton( + Html::submitButton( $this->msg( 'allpagessubmit' )->text(), - array( 'class' => $submitClassName ) + array(), array( 'mw-ui-progressive' ) ) ) ); diff --git a/includes/specials/SpecialContributions.php b/includes/specials/SpecialContributions.php index 4b4f5452ff..5631c3aff4 100644 --- a/includes/specials/SpecialContributions.php +++ b/includes/specials/SpecialContributions.php @@ -603,19 +603,14 @@ class SpecialContributions extends IncludableSpecialPage { $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly ); - $className = 'mw-submit'; - if ( $this->getConfig( 'UseMediaWikiUIEverywhere') ) { - $className .= ' mw-ui-button mw-ui-progressive'; - } - $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ), Xml::dateMenu( $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'], $this->opts['month'] ) . ' ' . - Xml::submitButton( + Html::submitButton( $this->msg( 'sp-contributions-submit' )->text(), - array( 'class' => $className ) + array( 'class' => 'mw-submit' ), array( 'mw-ui-progressive' ) ) ); diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index adc248eafa..81dc77fd1d 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -1068,9 +1068,9 @@ class SpecialSearch extends SpecialPage { 'class' => 'mw-ui-input mw-ui-input-inline', ) ) . "\n"; $out .= Html::hidden( 'fulltext', 'Search' ) . "\n"; - $out .= Xml::submitButton( + $out .= Html::submitButton( $this->msg( 'searchbutton' )->text(), - array( 'class' => array( 'mw-ui-button', 'mw-ui-progressive' ) ) + array(), array( 'mw-ui-progressive' ) ) . "\n"; // Results-info diff --git a/includes/templates/Usercreate.php b/includes/templates/Usercreate.php index 01da0bd790..d435615f34 100644 --- a/includes/templates/Usercreate.php +++ b/includes/templates/Usercreate.php @@ -254,14 +254,16 @@ class UsercreateTemplate extends BaseTemplate { ?>
getMsg( $this->data['loggedin'] ? 'createacct-another-submit' : 'createacct-submit' ), - 'submit', - array( - 'class' => "mw-ui-button mw-ui-big mw-ui-block mw-ui-constructive", + $attrs = array( 'id' => 'wpCreateaccount', 'tabindex' => $tabIndex++ + ), + array( + 'mw-ui-big', + 'mw-ui-block', + 'mw-ui-constructive', ) ); ?> diff --git a/includes/templates/Userlogin.php b/includes/templates/Userlogin.php index 8bba4265f0..99fe2185d2 100644 --- a/includes/templates/Userlogin.php +++ b/includes/templates/Userlogin.php @@ -148,11 +148,14 @@ class UserloginTemplate extends BaseTemplate {
getMsg( 'pt-login-button' )->text(), 'submit', array( + $attrs = array( 'id' => 'wpLoginAttempt', 'tabindex' => '6', - 'class' => 'mw-ui-button mw-ui-big mw-ui-block mw-ui-constructive' - ) ); + ); + $modifiers = array( + 'mw-ui-big', 'mw-ui-block', 'mw-ui-constructive', + ); + echo Html::submitButton( $this->getMsg( 'pt-login-button' )->text(), $attrs, $modifiers ); ?>