1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bulma; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Html\Html; |
9
|
|
|
|
10
|
|
|
final class NavBar extends Widget |
11
|
|
|
{ |
12
|
|
|
private string $brand = ''; |
13
|
|
|
private string $brandLabel = ''; |
14
|
|
|
private string $brandImage = ''; |
15
|
|
|
private string $brandUrl = '/'; |
16
|
|
|
private string $toggleIcon = ''; |
17
|
|
|
private array $options = []; |
18
|
|
|
private array $brandOptions = []; |
19
|
|
|
private array $brandLabelOptions = []; |
20
|
|
|
private array $brandImageOptions = []; |
21
|
|
|
private array $itemsOptions = []; |
22
|
|
|
private array $menuOptions = []; |
23
|
|
|
private array $toggleOptions = [ |
24
|
|
|
'aria-expanded' => 'false', |
25
|
|
|
'aria-label' => 'menu', |
26
|
|
|
'class' => 'navbar-burger', |
27
|
|
|
'role' => 'button' |
28
|
|
|
]; |
29
|
|
|
|
30
|
14 |
|
public function start(): string |
31
|
|
|
{ |
32
|
14 |
|
$this->buildOptions(); |
33
|
14 |
|
$this->renderBrand(); |
34
|
|
|
|
35
|
14 |
|
$navOptions = $this->options; |
36
|
14 |
|
$navTag = ArrayHelper::remove($navOptions, 'tag', 'nav'); |
37
|
|
|
|
38
|
|
|
return |
39
|
14 |
|
Html::beginTag($navTag, $navOptions) . "\n" . |
40
|
14 |
|
$this->brand . "\n" . |
41
|
14 |
|
Html::beginTag('div', $this->menuOptions) . |
42
|
14 |
|
Html::beginTag('div', $this->itemsOptions); |
43
|
|
|
} |
44
|
|
|
|
45
|
14 |
|
protected function run(): string |
46
|
|
|
{ |
47
|
14 |
|
$tag = ArrayHelper::remove($this->options, 'tag', 'nav'); |
48
|
|
|
|
49
|
|
|
return |
50
|
14 |
|
Html::endTag('div') . "\n" . |
51
|
14 |
|
Html::endTag('div') . "\n" . |
52
|
14 |
|
Html::endTag($tag); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set render brand custom, {@see brandLabel} and {@see brandImage} are not generated. |
57
|
|
|
* |
58
|
|
|
* @param string $value |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
1 |
|
public function brand(string $value): self |
63
|
|
|
{ |
64
|
1 |
|
$this->brand = $value; |
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The text of the brand label or empty if it's not used. Note that this is not HTML-encoded. |
70
|
|
|
* |
71
|
|
|
* @param string $value |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
13 |
|
public function brandLabel(string $value): self |
76
|
|
|
{ |
77
|
13 |
|
$this->brandLabel = $value; |
78
|
13 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The image of the brand or empty if it's not used. |
83
|
|
|
* |
84
|
|
|
* @param string $value |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
12 |
|
public function brandImage(string $value): self |
89
|
|
|
{ |
90
|
12 |
|
$this->brandImage = $value; |
91
|
12 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The URL for the brand's hyperlink tag and will be used for the "href" attribute of the brand link. Default value |
96
|
|
|
* is '/' will be used. You may set it to `null` if you want to have no link at all. |
97
|
|
|
* |
98
|
|
|
* @param string $value |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
11 |
|
public function brandUrl(string $value): self |
103
|
|
|
{ |
104
|
11 |
|
$this->brandUrl = $value; |
105
|
11 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set toggle icon. |
110
|
|
|
* |
111
|
|
|
* @param string $value. |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
1 |
|
public function toggleIcon(string $value): self |
116
|
|
|
{ |
117
|
1 |
|
$this->toggleIcon = $value; |
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Options HTML attributes for the tag nav. |
123
|
|
|
* |
124
|
|
|
* @param array $value |
125
|
|
|
* |
126
|
|
|
* @return self |
127
|
|
|
* |
128
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
129
|
|
|
*/ |
130
|
2 |
|
public function options(array $value): self |
131
|
|
|
{ |
132
|
2 |
|
$this->options = $value; |
133
|
2 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Options HTML attributes of the tag div brand. |
138
|
|
|
* |
139
|
|
|
* @param array $value default value `navbar-item`. |
140
|
|
|
* |
141
|
|
|
* @return self |
142
|
|
|
* |
143
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
144
|
|
|
*/ |
145
|
1 |
|
public function brandOptions(array $value): self |
146
|
|
|
{ |
147
|
1 |
|
$this->brandOptions = $value; |
148
|
1 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Options HTML attributes of the tag div brand label. |
153
|
|
|
* |
154
|
|
|
* @param array $value default value `navbar-item`. |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
* |
158
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
159
|
|
|
*/ |
160
|
1 |
|
public function brandLabelOptions(array $value): self |
161
|
|
|
{ |
162
|
1 |
|
$this->brandLabelOptions = $value; |
163
|
1 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Options HTML attributes of the tag div brand link. |
168
|
|
|
* |
169
|
|
|
* @param array $value default value `navbar-item`. |
170
|
|
|
* |
171
|
|
|
* @return self |
172
|
|
|
* |
173
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
174
|
|
|
*/ |
175
|
1 |
|
public function brandImageOptions(array $value): self |
176
|
|
|
{ |
177
|
1 |
|
$this->brandImageOptions = $value; |
178
|
1 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Options HTML attributes of the tag div items nav, values `navbar-start`, `navbar-end`. |
183
|
|
|
* |
184
|
|
|
* @param array $value default value `navbar-start`. |
185
|
|
|
* |
186
|
|
|
* @return self |
187
|
|
|
* |
188
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
189
|
|
|
*/ |
190
|
2 |
|
public function itemsOptions(array $value): self |
191
|
|
|
{ |
192
|
2 |
|
$this->itemsOptions = $value; |
193
|
2 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Options HTML attributes of the tag div nav menu. |
198
|
|
|
* |
199
|
|
|
* @param array $value default value `navbar-menu`. |
200
|
|
|
* |
201
|
|
|
* @return self |
202
|
|
|
* |
203
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
204
|
|
|
*/ |
205
|
1 |
|
public function menuOptions(array $value): self |
206
|
|
|
{ |
207
|
1 |
|
$this->menuOptions = $value; |
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* The HTML attributes of the navbar toggler button. |
213
|
|
|
* |
214
|
|
|
* @param array $value |
215
|
|
|
* |
216
|
|
|
* @return self |
217
|
|
|
* |
218
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
219
|
|
|
*/ |
220
|
1 |
|
public function toggleOptions(array $value): self |
221
|
|
|
{ |
222
|
1 |
|
$this->toggleOptions = $value; |
223
|
1 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
14 |
|
private function buildOptions(): void |
227
|
|
|
{ |
228
|
14 |
|
$id = ''; |
229
|
|
|
|
230
|
14 |
|
if (!isset($this->options['id'])) { |
231
|
14 |
|
$id = $this->getId(); |
232
|
14 |
|
$this->options['id'] = "{$id}-navbar"; |
233
|
|
|
} |
234
|
|
|
|
235
|
14 |
|
$this->options = $this->addOptions($this->options, 'navbar'); |
236
|
14 |
|
$this->brandOptions = $this->addOptions($this->brandOptions, 'navbar-brand'); |
237
|
14 |
|
$this->brandLabelOptions = $this->addOptions($this->brandLabelOptions, 'navbar-item'); |
238
|
14 |
|
$this->brandImageOptions = $this->addOptions($this->brandImageOptions, 'navbar-item'); |
239
|
14 |
|
$this->menuOptions = $this->addOptions($this->menuOptions, 'navbar-menu'); |
240
|
|
|
|
241
|
14 |
|
$this->menuOptions['id'] = "{$id}-navbar-Menu"; |
242
|
|
|
|
243
|
14 |
|
$this->initItemsOptions(); |
244
|
14 |
|
} |
245
|
|
|
|
246
|
14 |
|
private function initItemsOptions(): void |
247
|
|
|
{ |
248
|
14 |
|
$optionsItems = ''; |
249
|
|
|
|
250
|
14 |
|
if (isset($this->itemsOptions['class'])) { |
251
|
2 |
|
$optionsItems = $this->itemsOptions['class']; |
252
|
|
|
|
253
|
2 |
|
unset($this->itemsOptions['class']); |
254
|
|
|
} |
255
|
|
|
|
256
|
14 |
|
if (strpos($optionsItems, 'navbar-end') === false) { |
257
|
13 |
|
Html::addCssClass($this->itemsOptions, 'navbar-start'); |
258
|
|
|
} |
259
|
|
|
|
260
|
14 |
|
if (!empty($optionsItems)) { |
261
|
2 |
|
Html::addCssClass($this->itemsOptions, $optionsItems); |
262
|
|
|
} |
263
|
14 |
|
} |
264
|
|
|
|
265
|
14 |
|
private function renderBrand(): void |
266
|
|
|
{ |
267
|
14 |
|
if ($this->brand === '') { |
268
|
13 |
|
$this->brand = Html::beginTag('div', $this->brandOptions); |
269
|
|
|
|
270
|
13 |
|
if ($this->brandImage !== '' && $this->brandLabel !== '') { |
271
|
11 |
|
$this->brand .= Html::tag('span', Html::img($this->brandImage), $this->brandImageOptions); |
272
|
|
|
} |
273
|
|
|
|
274
|
13 |
|
if ($this->brandImage !== '' && $this->brandLabel === '') { |
275
|
1 |
|
$this->brand .= Html::a(Html::img($this->brandImage), $this->brandUrl, $this->brandImageOptions); |
276
|
|
|
} |
277
|
|
|
|
278
|
13 |
|
if ($this->brandLabel !== '') { |
279
|
12 |
|
$this->brand .= Html::a($this->brandLabel, $this->brandUrl, $this->brandLabelOptions); |
280
|
|
|
} |
281
|
|
|
|
282
|
13 |
|
$this->brand .= $this->renderToggleButton(); |
283
|
13 |
|
$this->brand .= Html::endTag('div'); |
284
|
|
|
} |
285
|
14 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Renders collapsible toggle button. |
289
|
|
|
* |
290
|
|
|
* @return string the rendering toggle button. |
291
|
|
|
*/ |
292
|
13 |
|
private function renderToggleButton(): string |
293
|
|
|
{ |
294
|
|
|
return |
295
|
13 |
|
Html::beginTag('a', $this->toggleOptions) . |
296
|
13 |
|
$this->renderToggleIcon() . |
297
|
|
|
|
298
|
13 |
|
Html::endTag('a'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Render icon toggle. |
303
|
|
|
* |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
13 |
|
private function renderToggleIcon(): string |
307
|
|
|
{ |
308
|
13 |
|
if ($this->toggleIcon === '') { |
309
|
12 |
|
$this->toggleIcon = Html::tag('span', '', ['aria-hidden' => 'true']) . |
310
|
12 |
|
Html::tag('span', '', ['aria-hidden' => 'true']) . |
311
|
12 |
|
Html::tag('span', '', ['aria-hidden' => 'true']); |
312
|
|
|
} |
313
|
|
|
|
314
|
13 |
|
return $this->toggleIcon; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|