mediawiki-skins-Vector/includes/Components/VectorComponentTableOfContents.php
bwang 0032b7b970 Remove VectorComponentTableOfContentsContainer in favor of directly using VectorComponentPinnableContainer,
- Remove all TOC components from markup when TOC isnt available
- Ensure TOC components have a consistent naming convention

Follow-up: Icd871e1bdf4ab0c7aa5b906b913416f4b1747750
Change-Id: I63519b5e5392b9eb546876e7eea060b9a6c147dc
2023-01-16 17:20:03 -06:00

95 lines
2.2 KiB
PHP

<?php
namespace MediaWiki\Skins\Vector\Components;
use Config;
use MessageLocalizer;
/**
* VectorComponentTableOfContents component
*/
class VectorComponentTableOfContents implements VectorComponent {
/** @var array */
private $tocData;
/** @var MessageLocalizer */
private $localizer;
/** @var bool */
private $isPinned;
/** @var Config */
private $config;
/** @var VectorComponentPinnableHeader */
private $pinnableHeader;
/** @var string */
public const ID = 'vector-toc';
/**
* @param array $tocData
* @param MessageLocalizer $localizer
* @param Config $config
*/
public function __construct(
array $tocData,
MessageLocalizer $localizer,
Config $config
) {
$this->tocData = $tocData;
$this->localizer = $localizer;
// ToC is pinned by default, hardcoded for now
$this->isPinned = true;
$this->config = $config;
$this->pinnableHeader = new VectorComponentPinnableHeader(
$this->localizer,
$this->isPinned,
self::ID,
null,
false,
'h2'
);
}
/**
* @return bool
*/
public function isPinned(): bool {
return $this->isPinned;
}
/**
* In tableOfContents.js we have tableOfContents::getTableOfContentsSectionsData(),
* that yields the same result as this function, please make sure to keep them in sync.
* @inheritDoc
*/
public function getTemplateData(): array {
$sections = $this->tocData[ 'array-sections' ] ?? [];
if ( empty( $sections ) ) {
return [];
}
// Populate button labels for collapsible TOC sections
foreach ( $sections as &$section ) {
if ( $section['is-top-level-section'] && $section['is-parent-section'] ) {
$section['vector-button-label'] =
$this->localizer->msg( 'vector-toc-toggle-button-label', $section['line'] )->text();
}
}
$pinnableElement = new VectorComponentPinnableElement( self::ID );
return $pinnableElement->getTemplateData() +
array_merge( $this->tocData, [
'is-vector-toc-beginning-enabled' => $this->config->get(
'VectorTableOfContentsBeginning'
),
'vector-is-collapse-sections-enabled' =>
$this->tocData[ 'number-section-count'] >= $this->config->get(
'VectorTableOfContentsCollapseAtCount'
),
'data-pinnable-header' => $this->pinnableHeader->getTemplateData(),
] );
}
}