Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
12.50% |
6 / 48 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PrimaryNavComponent | |
12.50% |
6 / 48 |
|
0.00% |
0 / 2 |
92.06 | |
0.00% |
0 / 1 |
__construct | |
13.04% |
6 / 46 |
|
0.00% |
0 / 1 |
62.26 | |||
isActiveLink | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
16 | * |
17 | * @file |
18 | */ |
19 | namespace MediaWiki\Skin\WikimediaApiPortal\Component; |
20 | |
21 | use MediaWiki\Html\Html; |
22 | use MediaWiki\Skin\WikimediaApiPortal\Skin; |
23 | use MediaWiki\Title\Title; |
24 | use OOUI\ButtonWidget; |
25 | |
26 | class PrimaryNavComponent extends Component { |
27 | /** |
28 | * @param Title $title |
29 | * @param string $id |
30 | * @param array $sidebar |
31 | * @param Skin $skin |
32 | */ |
33 | public function __construct( |
34 | Title $title, |
35 | string $id, |
36 | array $sidebar, |
37 | Skin $skin |
38 | ) { |
39 | parent::__construct( 'PrimaryNav' ); |
40 | $items = []; |
41 | foreach ( $sidebar as $menu ) { |
42 | /** @var array $menu See BaseTemplate::getSidebar */ |
43 | |
44 | // Dropdown or single link |
45 | if ( is_array( $menu['content'] ) && count( $menu['content'] ) > 1 ) { |
46 | $subitems = []; |
47 | $isActive = false; |
48 | foreach ( $menu['content'] as $key => $item ) { |
49 | if ( !isset( $item['href'] ) || !isset( $item['text'] ) ) { |
50 | // Skip any links without href or text set. |
51 | continue; |
52 | } |
53 | /** @var array $item See Skin::addToSidebarPlain */ |
54 | $isActive = $isActive || $this->isActiveLink( $title, $item['href'] ); |
55 | $subitems[] = Html::element( |
56 | 'a', |
57 | [ |
58 | 'href' => $item['href'], |
59 | 'class' => 'primary-nav-menu-item' |
60 | ], |
61 | $item['text'] |
62 | ); |
63 | } |
64 | |
65 | $header = new ButtonWidget( [ |
66 | 'label' => $menu['header'], |
67 | 'framed' => false, |
68 | 'indicator' => 'down', |
69 | 'classes' => [ 'primary-nav-menu-button' ] |
70 | ] ); |
71 | $items[] = [ |
72 | 'isDropdown' => true, |
73 | 'isActive' => $isActive, |
74 | 'header' => $header, |
75 | 'items' => $subitems, |
76 | ]; |
77 | } elseif ( isset( $menu['content'][0] ) ) { |
78 | $isActive = $this->isActiveLink( $title, $menu['content'][0]['href'] ); |
79 | $header = new ButtonWidget( [ |
80 | 'label' => $menu['header'], |
81 | 'framed' => false, |
82 | 'href' => $menu['content'][0]['href'], |
83 | 'classes' => [ 'primary-nav-menu-button' ] |
84 | ] ); |
85 | $items[] = [ |
86 | 'isDropdown' => false, |
87 | 'isActive' => $isActive, |
88 | 'header' => $header, |
89 | ]; |
90 | } |
91 | } |
92 | |
93 | $this->args = [ |
94 | 'items' => $items |
95 | ]; |
96 | } |
97 | |
98 | /** |
99 | * Whether the link points to the current title (or a subpage thereof). |
100 | * @param Title $title |
101 | * @param string $link |
102 | * @return bool |
103 | */ |
104 | private function isActiveLink( Title $title, string $link ): bool { |
105 | // Match logic in Skin::addToSidebarPlain |
106 | $currentLink = $title->fixSpecialName()->getLinkURL(); |
107 | |
108 | return $link === $currentLink || strpos( $currentLink, "$link/" ) === 0; |
109 | } |
110 | } |