2022-10-20 21:32:07 +00:00
|
|
|
<?php
|
|
|
|
namespace MediaWiki\Skins\Vector\Components;
|
|
|
|
|
|
|
|
use MessageLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VectorComponentPinnableHeader component
|
|
|
|
*/
|
|
|
|
class VectorComponentPinnableHeader implements VectorComponent {
|
|
|
|
/** @var MessageLocalizer */
|
|
|
|
private $localizer;
|
|
|
|
/** @var bool */
|
|
|
|
private $pinned;
|
|
|
|
/** @var string */
|
2023-03-03 21:23:28 +00:00
|
|
|
private $id;
|
|
|
|
/** @var string */
|
2022-12-07 22:15:31 +00:00
|
|
|
private $featureName;
|
2022-11-14 18:24:43 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
* Flag controlling if the pinnable element should be automatically moved in the DOM when pinned/unpinned
|
|
|
|
*/
|
2022-10-20 21:32:07 +00:00
|
|
|
private $moveElement;
|
2022-11-14 18:24:43 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $labelTagName;
|
2022-10-20 21:32:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param MessageLocalizer $localizer
|
|
|
|
* @param bool $pinned
|
2023-03-03 21:23:28 +00:00
|
|
|
* @param string $id Pinnable element id, by convention this should include the `vector-`
|
2022-12-07 22:15:31 +00:00
|
|
|
* prefix e.g. `vector-page-tools` or `vector-toc`.
|
2023-03-03 21:23:28 +00:00
|
|
|
* @param string $featureName Pinned and unpinned states will
|
2022-12-07 22:15:31 +00:00
|
|
|
* persist for logged-in users by leveraging features.js to manage the user
|
|
|
|
* preference storage and the toggling of the body class. This name should NOT
|
|
|
|
* contain the "vector-" prefix.
|
2022-10-20 21:32:07 +00:00
|
|
|
* @param bool|null $moveElement
|
2022-11-14 18:24:43 +00:00
|
|
|
* @param string|null $labelTagName Element type of the label. Either a 'div' or a 'h2'
|
|
|
|
* in the case of the pinnable ToC.
|
2022-10-20 21:32:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
MessageLocalizer $localizer,
|
|
|
|
bool $pinned,
|
2023-03-03 21:23:28 +00:00
|
|
|
string $id,
|
|
|
|
string $featureName,
|
2022-11-14 18:24:43 +00:00
|
|
|
?bool $moveElement = true,
|
|
|
|
?string $labelTagName = 'div'
|
2022-10-20 21:32:07 +00:00
|
|
|
) {
|
|
|
|
$this->localizer = $localizer;
|
|
|
|
$this->pinned = $pinned;
|
2023-03-03 21:23:28 +00:00
|
|
|
$this->id = $id;
|
2022-12-07 22:15:31 +00:00
|
|
|
$this->featureName = $featureName;
|
2022-10-20 21:32:07 +00:00
|
|
|
$this->moveElement = $moveElement;
|
2022-11-14 18:24:43 +00:00
|
|
|
$this->labelTagName = $labelTagName;
|
2022-10-20 21:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTemplateData(): array {
|
|
|
|
$messageLocalizer = $this->localizer;
|
|
|
|
$data = [
|
|
|
|
'is-pinned' => $this->pinned,
|
2023-03-03 21:23:28 +00:00
|
|
|
'label' => $messageLocalizer->msg( $this->id . '-label' ),
|
2022-11-14 18:24:43 +00:00
|
|
|
'label-tag-name' => $this->labelTagName,
|
2022-10-20 21:32:07 +00:00
|
|
|
'pin-label' => $messageLocalizer->msg( 'vector-pin-element-label' ),
|
|
|
|
'unpin-label' => $messageLocalizer->msg( 'vector-unpin-element-label' ),
|
2023-03-03 21:23:28 +00:00
|
|
|
'data-pinnable-element-id' => $this->id,
|
2022-12-07 22:15:31 +00:00
|
|
|
'data-feature-name' => $this->featureName
|
2022-10-20 21:32:07 +00:00
|
|
|
];
|
|
|
|
if ( $this->moveElement ) {
|
|
|
|
// Assumes consistent naming standard for pinnable elements and their containers
|
|
|
|
$data = array_merge( $data, [
|
2023-03-03 21:23:28 +00:00
|
|
|
'data-unpinned-container-id' => $this->id . '-unpinned-container',
|
|
|
|
'data-pinned-container-id' => $this->id . '-pinned-container',
|
2022-10-20 21:32:07 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|