generated from yiisoft/package-template
/
Nav.php
308 lines (264 loc) · 9.13 KB
/
Nav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Bulma;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
use Yiisoft\Factory\Exceptions\InvalidConfigException;
use function array_key_exists;
use function implode;
use function is_array;
final class Nav extends Widget
{
private bool $activateItems = true;
private bool $activateParents = false;
private string $currentPath = '';
private bool $dropdown = false;
private bool $encodeLabels = true;
private array $items = [];
protected function run(): string
{
$html = $this->renderItems();
if ($this->dropdown) {
$html .= Html::endTag('div');
}
return $html;
}
/**
* Whether to automatically activate items according to whether their currentPath matches the currently requested.
*
* @param bool $value
*
* @return $this
*
* {@see isItemActive}
*/
public function activateItems(bool $value): self
{
$this->activateItems = $value;
return $this;
}
/**
* Whether to activate parent menu items when one of the corresponding child menu items is active.
*
* @param bool $value
*
* @return $this
*/
public function activateParents(bool $value): self
{
$this->activateParents = $value;
return $this;
}
/**
* Allows you to assign the current path of the url from request controller.
*
* @param string $value
*
* @return $this
*/
public function currentPath(string $value): self
{
$this->currentPath = $value;
return $this;
}
/**
* Whether the nav items labels should be HTML-encoded.
*
* @param bool $value
*
* @return $this
*/
public function encodeLabels(bool $value): self
{
$this->encodeLabels = $value;
return $this;
}
/**
* List of items in the nav widget. Each array element represents a single menu item which can be either a string
* or an array with the following structure:
*
* - label: string, required, the nav item label.
* - url: optional, the item's URL. Defaults to "#".
* - visible: bool, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item's link.
* - options: array, optional, the HTML attributes of the item container (LI).
* - active: bool, optional, whether the item should be on active state or not.
* - dropdownOptions: array, optional, the HTML options that will passed to the {@see Dropdown} widget.
* - items: array|string, optional, the configuration array for creating a {@see Dropdown} widget, or a string
* representing the dropdown menu.
* - encode: bool, optional, whether the label will be HTML-encoded. If set, supersedes the $encodeLabels option for
* only this item.
*
* If a menu item is a string, it will be rendered directly without HTML encoding.
*
* @param array $value
*
* @return $this
*/
public function items(array $value): self
{
$this->items = $value;
return $this;
}
/**
* Renders the given items as a dropdown.
*
* This method is called to create sub-menus.
*
* @param array $items the given items. Please refer to {@see Dropdown::items} for the array structure.
* @param array $parentItem the parent item information. Please refer to {@see items} for the structure of this
* array.
*
* @throws InvalidConfigException
*
* @return string the rendering result.
*/
private function renderDropdown(array $items, array $parentItem): string
{
return Dropdown::widget()
->dividerClass('navbar-divider')
->itemClass('navbar-item')
->itemsClass('navbar-dropdown')
->encloseByContainer(false)
->encodeLabels($this->encodeLabels)
->items($items)
->itemsOptions(ArrayHelper::getValue($parentItem, 'dropdownOptions', []))
->render() . "\n";
}
/**
* Check to see if a child item is active optionally activating the parent.
*
* @param array $items
* @param bool $active should the parent be active too
*
* @return array
*
* {@see items}
*/
private function isChildActive(array $items, bool &$active): array
{
foreach ($items as $i => $child) {
if ($this->isItemActive($child)) {
ArrayHelper::setValue($items[$i], 'active', true);
if ($this->activateParents) {
$active = $this->activateParents;
}
}
if (is_array($child) && ($childItems = ArrayHelper::getValue($child, 'items')) && is_array($childItems)) {
$activeParent = false;
$items[$i]['items'] = $this->isChildActive($childItems, $activeParent);
if ($activeParent) {
Html::addCssClass($items[$i]['options'], 'active');
$active = $activeParent;
}
}
}
return $items;
}
/**
* Checks whether a menu item is active.
*
* This is done by checking if {@see currentPath} match that specified in the `url` option of the menu item. When
* the `url` option of a menu item is specified in terms of an array, its first element is treated as the
* currentPath for the item and the rest of the elements are the associated parameters. Only when its currentPath
* and parameters match {@see currentPath}, respectively, will a menu item be considered active.
*
* @param array|string|object $item the menu item to be checked
*
* @return bool whether the menu item is active
*/
private function isItemActive($item): bool
{
if (isset($item['active'])) {
return ArrayHelper::getValue($item, 'active');
}
if (isset($item['url']) && $this->currentPath !== '/' && $item['url'] === $this->currentPath && $this->activateItems) {
return true;
}
return false;
}
private function renderIcon(string $label, string $icon, array $iconOptions): string
{
if ($icon !== '') {
$label = Html::beginTag('span', $iconOptions) .
Html::tag('i', '', ['class' => $icon]) .
Html::endTag('span') .
Html::tag('span', $label);
}
return $label;
}
/**
* Renders widget items.
*
* @throws InvalidConfigException|JsonException
*
* @return string
*/
private function renderItems(): string
{
$items = [];
foreach ($this->items as $item) {
if (!isset($item['visible']) || $item['visible']) {
$items[] = $this->renderItem($item);
}
}
return implode("\n", $items);
}
/**
* Renders a widget's item.
*
* @param array $item the item to render.
*
* @throws InvalidConfigException|JsonException
*
* @return string the rendering result.
*/
private function renderItem(array $item): string
{
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$this->encodeLabels = $item['encode'] ?? $this->encodeLabels;
if ($this->encodeLabels) {
$label = Html::encode($item['label']);
} else {
$label = $item['label'];
}
$iconOptions = [];
$icon = $item['icon'] ?? '';
if (array_key_exists('iconOptions', $item) && is_array($item['iconOptions'])) {
$iconOptions = $this->addOptions($iconOptions, 'icon');
}
$label = $this->renderIcon($label, $icon, $iconOptions);
$options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items');
$url = ArrayHelper::getValue($item, 'url', '#');
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$disabled = ArrayHelper::getValue($item, 'disabled', false);
$active = $this->isItemActive($item);
if (isset($items)) {
$this->dropdown = true;
Html::addCssClass($options, 'navbar-item has-dropdown is-hoverable');
if (is_array($items)) {
$items = $this->isChildActive($items, $active);
$items = $this->renderDropdown($items, $item);
}
}
Html::addCssClass($linkOptions, 'navbar-item');
if ($disabled) {
Html::addCssStyle($linkOptions, 'opacity:.65; pointer-events:none;');
}
/** @psalm-suppress ConflictingReferenceConstraint */
if ($this->activateItems && $active) {
Html::addCssClass($linkOptions, 'is-active');
}
if ($this->dropdown) {
return
html::beginTag('div', $options) . "\n" .
Html::a($label, $url, ['class' => 'navbar-link']) . "\n" .
$items;
}
return Html::a($label, $url, $linkOptions);
}
}