2022-12-07 00:42:31 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
2024-01-02 19:54:08 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2023-02-23 21:23:46 +00:00
|
|
|
use MediaWiki\Skins\Vector\Constants;
|
|
|
|
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
|
2022-12-13 20:28:22 +00:00
|
|
|
use MessageLocalizer;
|
|
|
|
|
2022-12-07 00:42:31 +00:00
|
|
|
/**
|
|
|
|
* VectorComponentTableOfContents component
|
|
|
|
*/
|
|
|
|
class VectorComponentTableOfContents implements VectorComponent {
|
|
|
|
|
2022-12-13 20:28:22 +00:00
|
|
|
/** @var array */
|
|
|
|
private $tocData;
|
|
|
|
|
|
|
|
/** @var MessageLocalizer */
|
|
|
|
private $localizer;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $isPinned;
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/** @var VectorComponentPinnableHeader */
|
|
|
|
private $pinnableHeader;
|
|
|
|
|
2023-01-12 21:40:14 +00:00
|
|
|
/** @var string */
|
2022-12-13 20:28:22 +00:00
|
|
|
public const ID = 'vector-toc';
|
|
|
|
|
2022-12-07 00:42:31 +00:00
|
|
|
/**
|
2022-12-13 20:28:22 +00:00
|
|
|
* @param array $tocData
|
|
|
|
* @param MessageLocalizer $localizer
|
|
|
|
* @param Config $config
|
2023-02-23 21:23:46 +00:00
|
|
|
* @param FeatureManager $featureManager
|
2022-12-13 20:28:22 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
array $tocData,
|
|
|
|
MessageLocalizer $localizer,
|
2023-02-23 21:23:46 +00:00
|
|
|
Config $config,
|
|
|
|
FeatureManager $featureManager
|
2022-12-13 20:28:22 +00:00
|
|
|
) {
|
|
|
|
$this->tocData = $tocData;
|
|
|
|
$this->localizer = $localizer;
|
2024-04-10 21:08:22 +00:00
|
|
|
// FIXME: isPinned is no longer accurate because the appearance menu uses client preferences
|
2023-02-23 21:23:46 +00:00
|
|
|
$this->isPinned = $featureManager->isFeatureEnabled( Constants::FEATURE_TOC_PINNED );
|
2022-12-13 20:28:22 +00:00
|
|
|
$this->config = $config;
|
2023-01-12 21:40:14 +00:00
|
|
|
$this->pinnableHeader = new VectorComponentPinnableHeader(
|
|
|
|
$this->localizer,
|
|
|
|
$this->isPinned,
|
|
|
|
self::ID,
|
2023-02-23 21:23:46 +00:00
|
|
|
'toc-pinned',
|
2023-01-12 21:40:14 +00:00
|
|
|
false,
|
|
|
|
'h2'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPinned(): bool {
|
|
|
|
return $this->isPinned;
|
2022-12-13 20:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In tableOfContents.js we have tableOfContents::getTableOfContentsSectionsData(),
|
|
|
|
* that yields the same result as this function, please make sure to keep them in sync.
|
2022-12-07 00:42:31 +00:00
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
2023-01-12 21:44:54 +00:00
|
|
|
$sections = $this->tocData[ 'array-sections' ] ?? [];
|
2023-10-22 09:45:42 +00:00
|
|
|
if ( !$sections ) {
|
2022-12-13 20:28:22 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// Populate button labels for collapsible TOC sections
|
2023-01-12 21:44:54 +00:00
|
|
|
foreach ( $sections as &$section ) {
|
2022-12-13 20:28:22 +00:00
|
|
|
if ( $section['is-top-level-section'] && $section['is-parent-section'] ) {
|
|
|
|
$section['vector-button-label'] =
|
2023-07-05 12:47:27 +00:00
|
|
|
$this->localizer->msg( 'vector-toc-toggle-button-label' )
|
|
|
|
->rawParams( $section['line'] )
|
|
|
|
->escaped();
|
2022-12-13 20:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 16:47:17 +00:00
|
|
|
$this->tocData[ 'array-sections' ] = $sections;
|
2022-12-13 20:28:22 +00:00
|
|
|
|
|
|
|
$pinnableElement = new VectorComponentPinnableElement( self::ID );
|
|
|
|
|
|
|
|
return $pinnableElement->getTemplateData() +
|
|
|
|
array_merge( $this->tocData, [
|
|
|
|
'vector-is-collapse-sections-enabled' =>
|
2023-04-04 07:54:13 +00:00
|
|
|
count( $this->tocData['array-sections'] ) > 3 &&
|
2022-12-13 20:28:22 +00:00
|
|
|
$this->tocData[ 'number-section-count'] >= $this->config->get(
|
|
|
|
'VectorTableOfContentsCollapseAtCount'
|
|
|
|
),
|
2023-01-12 21:40:14 +00:00
|
|
|
'data-pinnable-header' => $this->pinnableHeader->getTemplateData(),
|
2022-12-13 20:28:22 +00:00
|
|
|
] );
|
2022-12-07 00:42:31 +00:00
|
|
|
}
|
|
|
|
}
|