| 1 |
<?php |
| 2 |
namespace MediaWiki\Skins\Vector\Components; |
| 3 |
|
| 4 |
use MediaWiki\Config\Config; |
| 5 |
use MediaWiki\Skins\Vector\Constants; |
| 6 |
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager; |
| 7 |
use MessageLocalizer; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class VectorComponentTableOfContents implements VectorComponent { |
| 13 |
|
| 14 |
|
| 15 |
private $tocData; |
| 16 |
|
| 17 |
|
| 18 |
private $localizer; |
| 19 |
|
| 20 |
|
| 21 |
private $isPinned; |
| 22 |
|
| 23 |
|
| 24 |
private $config; |
| 25 |
|
| 26 |
|
| 27 |
private $pinnableHeader; |
| 28 |
|
| 29 |
|
| 30 |
public const ID = 'vector-toc'; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct( |
| 39 |
array $tocData, |
| 40 |
MessageLocalizer $localizer, |
| 41 |
Config $config, |
| 42 |
FeatureManager $featureManager |
| 43 |
) { |
| 44 |
$this->tocData = $tocData; |
| 45 |
$this->localizer = $localizer; |
| 46 |
|
| 47 |
$this->isPinned = $featureManager->isFeatureEnabled( Constants::FEATURE_TOC_PINNED ); |
| 48 |
$this->config = $config; |
| 49 |
$this->pinnableHeader = new VectorComponentPinnableHeader( |
| 50 |
$this->localizer, |
| 51 |
$this->isPinned, |
| 52 |
self::ID, |
| 53 |
'toc-pinned', |
| 54 |
false, |
| 55 |
'h2' |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function isPinned(): bool { |
| 63 |
return $this->isPinned; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getTemplateData(): array { |
| 72 |
$sections = $this->tocData[ 'array-sections' ] ?? []; |
| 73 |
if ( !$sections ) { |
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
foreach ( $sections as &$section ) { |
| 78 |
if ( $section['is-top-level-section'] && $section['is-parent-section'] ) { |
| 79 |
$section['vector-button-label'] = |
| 80 |
$this->localizer->msg( 'vector-toc-toggle-button-label' ) |
| 81 |
->rawParams( $section['line'] ) |
| 82 |
->escaped(); |
| 83 |
} |
| 84 |
} |
| 85 |
$this->tocData[ 'array-sections' ] = $sections; |
| 86 |
|
| 87 |
$pinnableElement = new VectorComponentPinnableElement( self::ID ); |
| 88 |
|
| 89 |
return $pinnableElement->getTemplateData() + |
| 90 |
array_merge( $this->tocData, [ |
| 91 |
'vector-is-collapse-sections-enabled' => |
| 92 |
count( $this->tocData['array-sections'] ) > 3 && |
| 93 |
$this->tocData[ 'number-section-count'] >= $this->config->get( |
| 94 |
'VectorTableOfContentsCollapseAtCount' |
| 95 |
), |
| 96 |
'data-pinnable-header' => $this->pinnableHeader->getTemplateData(), |
| 97 |
] ); |
| 98 |
} |
| 99 |
} |