Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
72.73% |
16 / 22 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
VectorComponentPinnableHeader | |
72.73% |
16 / 22 |
|
50.00% |
1 / 2 |
3.18 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getTemplateData | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace MediaWiki\Skins\Vector\Components; |
3 | |
4 | use MessageLocalizer; |
5 | |
6 | /** |
7 | * VectorComponentPinnableHeader component |
8 | */ |
9 | class VectorComponentPinnableHeader implements VectorComponent { |
10 | /** @var MessageLocalizer */ |
11 | private $localizer; |
12 | /** @var bool */ |
13 | private $pinned; |
14 | /** @var string */ |
15 | private $id; |
16 | /** @var string */ |
17 | private $featureName; |
18 | /** |
19 | * @var bool |
20 | * Flag controlling if the pinnable element should be automatically moved in the DOM when pinned/unpinned |
21 | */ |
22 | private $moveElement; |
23 | /** |
24 | * @var string |
25 | */ |
26 | private $labelTagName; |
27 | |
28 | /** |
29 | * @param MessageLocalizer $localizer |
30 | * @param bool $pinned |
31 | * @param string $id Pinnable element id, by convention this should include the `vector-` |
32 | * prefix e.g. `vector-page-tools` or `vector-toc`. |
33 | * @param string $featureName Pinned and unpinned states will |
34 | * persist for logged-in users by leveraging features.js to manage the user |
35 | * preference storage and the toggling of the body class. This name should NOT |
36 | * contain the "vector-" prefix. |
37 | * @param bool|null $moveElement |
38 | * @param string|null $labelTagName Element type of the label. Either a 'div' or a 'h2' |
39 | * in the case of the pinnable ToC. |
40 | */ |
41 | public function __construct( |
42 | MessageLocalizer $localizer, |
43 | bool $pinned, |
44 | string $id, |
45 | string $featureName, |
46 | ?bool $moveElement = true, |
47 | ?string $labelTagName = 'div' |
48 | ) { |
49 | $this->localizer = $localizer; |
50 | $this->pinned = $pinned; |
51 | $this->id = $id; |
52 | $this->featureName = $featureName; |
53 | $this->moveElement = $moveElement; |
54 | $this->labelTagName = $labelTagName; |
55 | } |
56 | |
57 | /** |
58 | * @inheritDoc |
59 | */ |
60 | public function getTemplateData(): array { |
61 | $messageLocalizer = $this->localizer; |
62 | $data = [ |
63 | 'is-pinned' => $this->pinned, |
64 | 'label' => $messageLocalizer->msg( $this->id . '-label' ), |
65 | 'label-tag-name' => $this->labelTagName, |
66 | 'pin-label' => $messageLocalizer->msg( 'vector-pin-element-label' ), |
67 | 'unpin-label' => $messageLocalizer->msg( 'vector-unpin-element-label' ), |
68 | 'data-pinnable-element-id' => $this->id, |
69 | 'data-feature-name' => $this->featureName |
70 | ]; |
71 | if ( $this->moveElement ) { |
72 | // Assumes consistent naming standard for pinnable elements and their containers |
73 | $data = array_merge( $data, [ |
74 | 'data-unpinned-container-id' => $this->id . '-unpinned-container', |
75 | 'data-pinned-container-id' => $this->id . '-pinned-container', |
76 | ] ); |
77 | } |
78 | return $data; |
79 | } |
80 | } |