mediawiki-skins-Vector/includes/Components/VectorComponentMainMenu.php
bwang 97de09dcba Refactor page tools, main menu, and TOC components
- getTocData is moved into VectorComponentTableOfContents and it's test file
The following changes were made to the main menu, toc and page tools PHP components
- Avoid passing in $skin to the constructor
- Handle isPinned logic inside the component
- Add a public ID constant to the components
- Dropdown data for each feature use the same naming convention

Bug: T317900
Change-Id: I77a617a6c1d93bccd3b6e59353299f5534624e53
2022-12-15 14:23:25 -08:00

86 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Components;
use MessageLocalizer;
use Skin;
use User;
/**
* VectorComponentMainMenu component
*/
class VectorComponentMainMenu implements VectorComponent {
/** @var VectorComponent|null */
private $optOut;
/** @var VectorComponent|null */
private $alert;
/** @var array */
private $sidebarData;
/** @var array */
private $languageData;
/** @var MessageLocalizer */
private $localizer;
/** @var VectorComponentPinnableHeader|null */
private $pinnableHeader;
/** @var string */
public const ID = 'vector-main-menu';
/**
* @param array $sidebarData
* @param bool $shouldLanguageAlertBeInSidebar
* @param array $languageData
* @param MessageLocalizer $localizer
* @param User $user
* @param Skin $skin
*/
public function __construct(
array $sidebarData,
bool $shouldLanguageAlertBeInSidebar,
array $languageData,
MessageLocalizer $localizer,
User $user,
Skin $skin
) {
$this->sidebarData = $sidebarData;
$this->languageData = $languageData;
$this->localizer = $localizer;
if ( $user->isRegistered() ) {
$this->optOut = new VectorComponentMainMenuActionOptOut( $skin );
$this->pinnableHeader = new VectorComponentPinnableHeader(
$this->localizer,
false,
self::ID,
null
);
}
if ( $shouldLanguageAlertBeInSidebar ) {
$this->alert = new VectorComponentMainMenuActionLanguageSwitchAlert( $skin );
}
}
/**
* @inheritDoc
*/
public function getTemplateData(): array {
$action = $this->optOut;
$alert = $this->alert;
$pinnableHeader = $this->pinnableHeader;
$portletsRest = [];
foreach ( $this->sidebarData[ 'array-portlets-rest' ] as $data ) {
$portletsRest[] = ( new VectorComponentMenu( $data ) )->getTemplateData();
}
$firstPortlet = new VectorComponentMenu( $this->sidebarData['data-portlets-first'] );
$languageMenu = new VectorComponentMenu( $this->languageData );
return [
'data-portlets-first' => $firstPortlet->getTemplateData(),
'array-portlets-rest' => $portletsRest,
'data-main-menu-action' => $action ? $action->getTemplateData() : null,
// T295555 Add language switch alert message temporarily (to be removed).
'data-vector-language-switch-alert' => $alert ? $alert->getTemplateData() : null,
'data-pinnable-header' => $pinnableHeader ? $pinnableHeader->getTemplateData() : null,
'data-languages' => $languageMenu->getTemplateData(),
];
}
}