1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
|
12
|
|
|
use function array_key_exists; |
13
|
|
|
use function array_merge; |
14
|
|
|
use function array_merge_recursive; |
15
|
|
|
use function is_string; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Dropdown renders a Bootstrap dropdown menu component. |
19
|
|
|
* |
20
|
|
|
* For example, |
21
|
|
|
* |
22
|
|
|
* ```php |
23
|
|
|
* <div class="dropdown"> |
24
|
|
|
* <?php |
25
|
|
|
* echo Dropdown::widget() |
26
|
|
|
* ->withItems([ |
27
|
|
|
* ['label' => 'DropdownA', 'url' => '/'], |
28
|
|
|
* ['label' => 'DropdownB', 'url' => '#'], |
29
|
|
|
* ]); |
30
|
|
|
* ?> |
31
|
|
|
* </div> |
32
|
|
|
* ``` |
33
|
|
|
*/ |
34
|
|
|
final class Dropdown extends Widget |
35
|
|
|
{ |
36
|
|
|
private array $items = []; |
37
|
|
|
private bool $encodeLabels = true; |
38
|
|
|
private bool $encodeTags = false; |
39
|
|
|
private array $submenuOptions = []; |
40
|
|
|
private array $options = []; |
41
|
16 |
|
|
42
|
|
|
public function run(): string |
43
|
16 |
|
{ |
44
|
15 |
|
if (!isset($this->options['id'])) { |
45
|
|
|
$this->options['id'] = "{$this->getId()}-dropdown"; |
46
|
|
|
} |
47
|
|
|
|
48
|
16 |
|
/** @psalm-suppress InvalidArgument */ |
49
|
|
|
Html::addCssClass($this->options, ['widget' => 'dropdown-menu']); |
50
|
16 |
|
|
51
|
|
|
if ($this->encodeTags === false) { |
52
|
16 |
|
$this->options = array_merge($this->options, ['itemOptions' => ['encode' =>false], 'encode' => false]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->renderItems($this->items, $this->options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* List of menu items in the dropdown. Each array element can be either an HTML string, or an array representing a |
60
|
|
|
* single menu with the following structure: |
61
|
|
|
* |
62
|
|
|
* - label: string, required, the label of the item link. |
63
|
|
|
* - encode: bool, optional, whether to HTML-encode item label. |
64
|
|
|
* - url: string|array, optional, the URL of the item link. This will be processed by {@see currentPath}. |
65
|
16 |
|
* If not set, the item will be treated as a menu header when the item has no sub-menu. |
66
|
|
|
* - visible: bool, optional, whether this menu item is visible. Defaults to true. |
67
|
16 |
|
* - linkOptions: array, optional, the HTML attributes of the item link. |
68
|
|
|
* - options: array, optional, the HTML attributes of the item. |
69
|
16 |
|
* - items: array, optional, the submenu items. The structure is the same as this property. |
70
|
16 |
|
* Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it. |
71
|
3 |
|
* - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be |
72
|
|
|
* merged with {@see submenuOptions}. |
73
|
|
|
* |
74
|
16 |
|
* To insert divider use `-`. |
75
|
3 |
|
* |
76
|
|
|
* @param array $value |
77
|
|
|
* |
78
|
16 |
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function withItems(array $value): self |
81
|
|
|
{ |
82
|
16 |
|
$new = clone $this; |
83
|
16 |
|
$new->items = $value; |
84
|
16 |
|
|
85
|
16 |
|
return $new; |
86
|
16 |
|
} |
87
|
16 |
|
|
88
|
16 |
|
/** |
89
|
|
|
* Whether the labels for header items should be HTML-encoded. |
90
|
16 |
|
* |
91
|
|
|
* @param bool $value |
92
|
16 |
|
* |
93
|
1 |
|
* @return $this |
94
|
1 |
|
*/ |
95
|
1 |
|
public function withoutEncodeLabels(bool $value = false): self |
96
|
16 |
|
{ |
97
|
3 |
|
$new = clone $this; |
98
|
|
|
$new->encodeLabels = $value; |
99
|
|
|
|
100
|
16 |
|
return $new; |
101
|
|
|
} |
102
|
16 |
|
|
103
|
16 |
|
/** |
104
|
3 |
|
* The HTML attributes for sub-menu container tags. |
105
|
16 |
|
* |
106
|
1 |
|
* @param array $value |
107
|
16 |
|
* |
108
|
8 |
|
* @return $this |
109
|
|
|
*/ |
110
|
10 |
|
public function withSubmenuOptions(array $value): self |
111
|
|
|
{ |
112
|
|
|
$new = clone $this; |
113
|
16 |
|
$new->submenuOptions = $value; |
114
|
|
|
|
115
|
3 |
|
return $new; |
116
|
|
|
} |
117
|
3 |
|
|
118
|
1 |
|
/** |
119
|
|
|
* @param array $value the HTML attributes for the widget container tag. The following special options are |
120
|
|
|
* recognized. |
121
|
3 |
|
* |
122
|
3 |
|
* @return $this |
123
|
|
|
* |
124
|
3 |
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
125
|
3 |
|
*/ |
126
|
|
|
public function withOptions(array $value): self |
127
|
|
|
{ |
128
|
|
|
$new = clone $this; |
129
|
3 |
|
$new->options = $value; |
130
|
|
|
|
131
|
3 |
|
return $new; |
132
|
3 |
|
} |
133
|
3 |
|
|
134
|
3 |
|
/** |
135
|
3 |
|
* Allows you to enable or disable the encoding tags html. |
136
|
3 |
|
* |
137
|
3 |
|
* @param bool $value |
138
|
3 |
|
* |
139
|
3 |
|
* @return self |
140
|
3 |
|
*/ |
141
|
3 |
|
public function withEncodeTags(bool $value = true): self |
142
|
|
|
{ |
143
|
|
|
$new = clone $this; |
144
|
|
|
$new->encodeTags = $value; |
145
|
|
|
|
146
|
|
|
return $new; |
147
|
16 |
|
} |
148
|
16 |
|
|
149
|
16 |
|
/** |
150
|
|
|
* Renders menu items. |
151
|
|
|
* |
152
|
|
|
* @param array $items the menu items to be rendered |
153
|
|
|
* @param array $options the container HTML attributes |
154
|
|
|
* |
155
|
|
|
* @throws JsonException|RuntimeException if the label option is not specified in one of the items. |
156
|
|
|
* |
157
|
|
|
* @return string the rendering result. |
158
|
|
|
*/ |
159
|
|
|
private function renderItems(array $items, array $options = []): string |
160
|
|
|
{ |
161
|
|
|
$lines = []; |
162
|
|
|
|
163
|
|
|
foreach ($items as $item) { |
164
|
|
|
if (is_string($item)) { |
165
|
|
|
$item = ['label' => $item, 'encode' => false, 'enclose' => false]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (isset($item['visible']) && !$item['visible']) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!array_key_exists('label', $item)) { |
173
|
|
|
throw new RuntimeException("The 'label' option is required."); |
174
|
|
|
} |
175
|
16 |
|
|
176
|
|
|
$encodeLabel = $item['encode'] ?? $this->encodeLabels; |
177
|
16 |
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
178
|
|
|
$itemOptions = ArrayHelper::getValue($item, 'options', []); |
179
|
16 |
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
180
|
|
|
$active = ArrayHelper::getValue($item, 'active', false); |
181
|
|
|
$disabled = ArrayHelper::getValue($item, 'disabled', false); |
182
|
|
|
$enclose = ArrayHelper::getValue($item, 'enclose', true); |
183
|
|
|
|
184
|
|
|
Html::addCssClass($linkOptions, ['widget' => 'dropdown-item']); |
185
|
|
|
|
186
|
|
|
if ($this->encodeTags === false) { |
187
|
|
|
$linkOptions = array_merge($linkOptions, ['encode' => false]); |
188
|
|
|
$itemOptions = array_merge($itemOptions, ['encode' => false]); |
189
|
12 |
|
} |
190
|
|
|
|
191
|
12 |
|
if ($disabled) { |
192
|
|
|
ArrayHelper::setValue($linkOptions, 'tabindex', '-1'); |
193
|
12 |
|
ArrayHelper::setValue($linkOptions, 'aria-disabled', 'true'); |
194
|
|
|
Html::addCssClass($linkOptions, ['disable' => 'disabled']); |
195
|
|
|
} elseif ($active) { |
196
|
|
|
Html::addCssClass($linkOptions, ['active' => 'active']); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$url = $item['url'] ?? null; |
200
|
|
|
|
201
|
|
|
if (empty($item['items'])) { |
202
|
|
|
if ($label === '-') { |
203
|
3 |
|
$content = Html::div('', ['class' => 'dropdown-divider']); |
204
|
|
|
} elseif ($enclose === false) { |
205
|
3 |
|
$content = $label; |
206
|
|
|
} elseif ($url === null) { |
207
|
3 |
|
$content = Html::tag('h6', $label, ['class' => 'dropdown-header']); |
208
|
|
|
} else { |
209
|
|
|
$content = Html::a($label, $url, $linkOptions); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$lines[] = $content; |
213
|
|
|
} else { |
214
|
|
|
$submenuOptions = $this->submenuOptions; |
215
|
|
|
|
216
|
|
|
if (isset($item['submenuOptions'])) { |
217
|
|
|
$submenuOptions = array_merge($submenuOptions, $item['submenuOptions']); |
218
|
12 |
|
} |
219
|
|
|
|
220
|
12 |
|
Html::addCssClass($submenuOptions, ['submenu' => 'dropdown-menu']); |
221
|
|
|
Html::addCssClass($linkOptions, ['toggle' => 'dropdown-toggle']); |
222
|
12 |
|
|
223
|
|
|
$itemOptions = array_merge_recursive(['class' => ['dropdown'], 'aria-expanded' => 'false'], $itemOptions); |
224
|
|
|
|
225
|
|
|
$lines[] = Html::a($label, $url, array_merge( |
226
|
|
|
[ |
227
|
|
|
'data-bs-toggle' => 'dropdown', |
228
|
|
|
'aria-haspopup' => 'true', |
229
|
|
|
'aria-expanded' => 'false', |
230
|
|
|
'role' => 'button', |
231
|
|
|
], |
232
|
|
|
$linkOptions) |
233
|
|
|
) |
234
|
|
|
|
235
|
|
|
. Html::tag( |
236
|
|
|
'ul', |
237
|
|
|
self::widget() |
238
|
|
|
->withItems($item['items']) |
|
|
|
|
239
|
|
|
->withOptions($submenuOptions) |
240
|
|
|
->withSubmenuOptions($submenuOptions) |
241
|
|
|
->withoutEncodeLabels($this->encodeLabels) |
242
|
|
|
->render(), |
243
|
|
|
$itemOptions, |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$options = array_merge(['aria-expanded' => 'false'], $options); |
249
|
|
|
|
250
|
|
|
return Html::ul($lines, $options); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|