mediawiki-skins-Vector/includes/Components/VectorComponentPageTools.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

96 lines
2.2 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Components;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
use MessageLocalizer;
use User;
/**
* VectorComponentMainMenu component
*/
class VectorComponentPageTools implements VectorComponent {
/** @var array */
private $menus;
/** @var MessageLocalizer */
private $localizer;
/** @var bool */
private $isPinned;
/** @var VectorComponentPinnableHeader|null */
private $pinnableHeader;
/** @var string */
public const ID = 'vector-page-tools';
/** @var string */
public const TOOLBOX_ID = 'p-tb';
/** @var string */
private const ACTIONS_ID = 'p-cactions';
/**
* @param array $menus
* @param MessageLocalizer $localizer
* @param User $user
* @param FeatureManager $featureManager
*/
public function __construct(
array $menus,
MessageLocalizer $localizer,
User $user,
FeatureManager $featureManager
) {
$this->menus = $menus;
$this->localizer = $localizer;
$this->isPinned = $featureManager->isFeatureEnabled( Constants::FEATURE_PAGE_TOOLS_PINNED );
$this->pinnableHeader = $user->isRegistered() ? new VectorComponentPinnableHeader(
$localizer,
$this->isPinned,
// Name
self::ID,
// Feature name
'page-tools-pinned'
) : null;
}
/**
* Revises the labels of the p-tb and p-cactions menus.
*
* @return array
*/
private function getMenus(): array {
return array_map( function ( $menu ) {
switch ( $menu['id'] ?? '' ) {
case self::TOOLBOX_ID:
$menu['label'] = $this->localizer->msg( 'vector-page-tools-general-label' )->text();
break;
case self::ACTIONS_ID:
$menu['label'] = $this->localizer->msg( 'vector-page-tools-actions-label' )->text();
break;
}
return $menu;
}, $this->menus );
}
/**
* @inheritDoc
*/
public function getTemplateData(): array {
$pinnedContainer = new VectorComponentPinnedContainer( self::ID, $this->isPinned );
$pinnableElement = new VectorComponentPinnableElement( self::ID );
$data = $pinnableElement->getTemplateData() +
$pinnedContainer->getTemplateData();
return $data + [
'data-pinnable-header' => $this->pinnableHeader ? $this->pinnableHeader->getTemplateData() : null,
'data-menus' => $this->getMenus()
];
}
}