2023-06-30 21:49:17 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
2024-10-20 12:01:19 +00:00
|
|
|
use MediaWiki\Language\Language;
|
2024-06-14 10:29:40 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2023-06-30 21:49:17 +00:00
|
|
|
use MediaWiki\StubObject\StubUserLang;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VectorComponentVariants component
|
|
|
|
*/
|
|
|
|
class VectorComponentVariants implements VectorComponent {
|
2024-06-14 10:29:40 +00:00
|
|
|
private LanguageConverterFactory $languageConverterFactory;
|
2023-06-30 21:49:17 +00:00
|
|
|
/** @var array */
|
|
|
|
private $menuData;
|
|
|
|
/** @var Language|StubUserLang */
|
|
|
|
private $pageLang;
|
|
|
|
/** @var string */
|
|
|
|
private $ariaLabel;
|
|
|
|
|
|
|
|
/**
|
2024-06-14 10:29:40 +00:00
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
2023-06-30 21:49:17 +00:00
|
|
|
* @param array $menuData
|
|
|
|
* @param Language|StubUserLang $pageLang
|
|
|
|
* @param string $ariaLabel
|
|
|
|
*/
|
2024-06-14 10:29:40 +00:00
|
|
|
public function __construct(
|
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
|
|
|
array $menuData,
|
|
|
|
$pageLang,
|
|
|
|
string $ariaLabel
|
|
|
|
) {
|
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
2023-06-30 21:49:17 +00:00
|
|
|
$this->menuData = $menuData;
|
|
|
|
$this->pageLang = $pageLang;
|
|
|
|
$this->ariaLabel = $ariaLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the selected variant for the dropdown label
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getDropdownLabel(): string {
|
2024-06-14 10:29:40 +00:00
|
|
|
$converter = $this->languageConverterFactory->getLanguageConverter( $this->pageLang );
|
2023-06-30 21:49:17 +00:00
|
|
|
return $this->pageLang->getVariantname(
|
|
|
|
$converter->getPreferredVariant()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the variants dropdown data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getDropdownData() {
|
|
|
|
$dropdown = new VectorComponentDropdown(
|
2024-05-28 15:16:33 +00:00
|
|
|
'vector-variants-dropdown',
|
2023-06-30 21:49:17 +00:00
|
|
|
$this->getDropdownLabel(),
|
|
|
|
// Hide dropdown if menu is empty
|
|
|
|
$this->menuData[ 'is-empty' ] ? 'emptyPortlet' : ''
|
|
|
|
);
|
|
|
|
$dropdownData = $dropdown->getTemplateData();
|
|
|
|
$dropdownData['aria-label'] = $this->ariaLabel;
|
|
|
|
return $dropdownData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the variants menu data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getMenuDropdownData() {
|
|
|
|
// Remove label from variants menu
|
|
|
|
$this->menuData['label'] = null;
|
|
|
|
$menu = new VectorComponentMenu( $this->menuData );
|
|
|
|
return $menu->getTemplateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
|
|
|
return [
|
|
|
|
'data-variants-dropdown' => $this->getDropdownData(),
|
|
|
|
'data-variants-menu' => $this->getMenuDropdownData()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|