Component: VariantsMenu

Bug: T319355
Change-Id: I10a8e796f6f6744fcc79ff6b1eb8f1d42389acba
This commit is contained in:
Jon Robson 2023-01-06 17:08:39 -08:00 committed by Jdlrobson
parent e153561255
commit bfeb729f84
5 changed files with 60 additions and 23 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace MediaWiki\Skins\Vector\Components;
use Language;
use MediaWiki\MediaWikiServices;
use MediaWiki\StubObject\StubUserLang;
/**
* VectorComponentMenuVariants component
*/
class VectorComponentMenuVariants extends VectorComponentMenu {
/**
* @param array $data
* @param Language|StubUserLang $pageLang
* @param string $ariaLabel
*/
public function __construct( array $data, $pageLang, string $ariaLabel ) {
parent::__construct( $this->updateVariantsMenuLabel( $data, $pageLang, $ariaLabel ) );
}
/**
* Change the portlets menu so the label is the selected variant
* @param array $portletData
* @param Language|StubUserLang $pageLang
* @param string $ariaLabel
* @return array
*/
private function updateVariantsMenuLabel( array $portletData, $pageLang, string $ariaLabel ): array {
$languageConverterFactory = MediaWikiServices::getInstance()->getLanguageConverterFactory();
$converter = $languageConverterFactory->getLanguageConverter( $pageLang );
$portletData['label'] = $pageLang->getVariantname(
$converter->getPreferredVariant()
);
// T289523 Add aria-label data to the language variant switcher.
$portletData['aria-label'] = $ariaLabel;
return $portletData;
}
/**
* @inheritDoc
*/
public function getTemplateData(): array {
return parent::getTemplateData();
}
}

View file

@ -205,23 +205,6 @@ abstract class SkinVector extends SkinMustache {
return ExtensionRegistry::getInstance()->isLoaded( 'UniversalLanguageSelector' ); return ExtensionRegistry::getInstance()->isLoaded( 'UniversalLanguageSelector' );
} }
/**
* Change the portlets menu so the label is the selected variant
* @param array $portletData
* @return array
*/
final protected function updateVariantsMenuLabel( array $portletData ): array {
$languageConverterFactory = MediaWikiServices::getInstance()->getLanguageConverterFactory();
$pageLang = $this->getTitle()->getPageLanguage();
$converter = $languageConverterFactory->getLanguageConverter( $pageLang );
$portletData['label'] = $pageLang->getVariantname(
$converter->getPreferredVariant()
);
// T289523 Add aria-label data to the language variant switcher.
$portletData['aria-label'] = $this->msg( 'vector-language-variant-switcher-label' );
return $portletData;
}
/** /**
* Generate data needed to generate the sticky header. * Generate data needed to generate the sticky header.
* FIXME: Move to VectorComponentStickyHeader * FIXME: Move to VectorComponentStickyHeader

View file

@ -9,6 +9,7 @@ use MediaWiki\Skins\Vector\Components\VectorComponentLanguageDropdown;
use MediaWiki\Skins\Vector\Components\VectorComponentMainMenu; use MediaWiki\Skins\Vector\Components\VectorComponentMainMenu;
use MediaWiki\Skins\Vector\Components\VectorComponentMenu; use MediaWiki\Skins\Vector\Components\VectorComponentMenu;
use MediaWiki\Skins\Vector\Components\VectorComponentMenuListItem; use MediaWiki\Skins\Vector\Components\VectorComponentMenuListItem;
use MediaWiki\Skins\Vector\Components\VectorComponentMenuVariants;
use MediaWiki\Skins\Vector\Components\VectorComponentPageTools; use MediaWiki\Skins\Vector\Components\VectorComponentPageTools;
use MediaWiki\Skins\Vector\Components\VectorComponentSearchBox; use MediaWiki\Skins\Vector\Components\VectorComponentSearchBox;
use MediaWiki\Skins\Vector\Components\VectorComponentStickyHeader; use MediaWiki\Skins\Vector\Components\VectorComponentStickyHeader;
@ -170,10 +171,6 @@ class SkinVector22 extends SkinVector {
$parentData = $this->mergeViewOverflowIntoActions( $parentData ); $parentData = $this->mergeViewOverflowIntoActions( $parentData );
$portlets = $parentData['data-portlets']; $portlets = $parentData['data-portlets'];
$parentData['data-portlets']['data-variants'] = $this->updateVariantsMenuLabel(
$parentData['data-portlets']['data-variants']
);
$langData = $parentData['data-portlets']['data-languages'] ?? null; $langData = $parentData['data-portlets']['data-languages'] ?? null;
$config = $this->getConfig(); $config = $this->getConfig();
@ -192,6 +189,11 @@ class SkinVector22 extends SkinVector {
$loginLinkData = $this->buildLoginData( $returnto, $this->useCombinedLoginLink() ); $loginLinkData = $this->buildLoginData( $returnto, $this->useCombinedLoginLink() );
$createAccountData = $this->buildCreateAccountData( $returnto ); $createAccountData = $this->buildCreateAccountData( $returnto );
$components = [ $components = [
'data-vector-variants' => new VectorComponentMenuVariants(
$parentData['data-portlets']['data-variants'],
$this->getTitle()->getPageLanguage(),
$this->msg( 'vector-language-variant-switcher-label' )
),
'data-vector-user-links' => new VectorComponentUserLinks( 'data-vector-user-links' => new VectorComponentUserLinks(
$this->getContext(), $this->getContext(),
$user, $user,

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Skins\Vector; namespace MediaWiki\Skins\Vector;
use MediaWiki\Skins\Vector\Components\VectorComponentMenuVariants;
use MediaWiki\Skins\Vector\Components\VectorComponentSearchBox; use MediaWiki\Skins\Vector\Components\VectorComponentSearchBox;
/** /**
@ -118,7 +119,12 @@ class SkinVectorLegacy extends SkinVector {
// Special casing for Variant to change label to selected. // Special casing for Variant to change label to selected.
// Hopefully we can revisit and possibly remove this code when the language switcher is moved. // Hopefully we can revisit and possibly remove this code when the language switcher is moved.
if ( $key === 'data-variants' ) { if ( $key === 'data-variants' ) {
$portletData = $this->updateVariantsMenuLabel( $portletData ); $variantMenu = new VectorComponentMenuVariants(
$portletData,
$this->getTitle()->getPageLanguage(),
$this->msg( 'vector-language-variant-switcher-label' )
);
$portletData = $variantMenu->getTemplateData();
} }
$portletData = $this->updatePortletClasses( $portletData = $this->updatePortletClasses(

View file

@ -4,7 +4,7 @@
<div id="left-navigation"> <div id="left-navigation">
<nav aria-label="{{msg-namespaces}}"> <nav aria-label="{{msg-namespaces}}">
{{#data-associated-pages}}{{>Tabs}}{{/data-associated-pages}} {{#data-associated-pages}}{{>Tabs}}{{/data-associated-pages}}
{{#data-variants}}{{>VariantsDropdown}}{{/data-variants}} {{#data-vector-variants}}{{>VariantsDropdown}}{{/data-vector-variants}}
</nav> </nav>
</div> </div>
<div id="right-navigation" class="vector-collapsible"> <div id="right-navigation" class="vector-collapsible">