1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bulma; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
use Yiisoft\Html\Tag\A; |
11
|
|
|
use Yiisoft\Html\Tag\I; |
12
|
|
|
use Yiisoft\Html\Tag\P; |
13
|
|
|
use Yiisoft\Html\Tag\Span; |
14
|
|
|
use Yiisoft\Widget\Widget; |
15
|
|
|
|
16
|
|
|
use function implode; |
17
|
|
|
use function in_array; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Panel renders a panel component. |
21
|
|
|
* |
22
|
|
|
* @see https://bulma.io/documentation/components/panel/ |
23
|
|
|
*/ |
24
|
|
|
final class Panel extends Widget |
25
|
|
|
{ |
26
|
|
|
public const COLOR_PRIMARY = 'is-primary'; |
27
|
|
|
public const COLOR_LINK = 'is-link'; |
28
|
|
|
public const COLOR_INFO = 'is-info'; |
29
|
|
|
public const COLOR_SUCCESS = 'is-success'; |
30
|
|
|
public const COLOR_WARNING = 'is-warning'; |
31
|
|
|
public const COLOR_DANGER = 'is-danger'; |
32
|
|
|
public const COLOR_DARK = 'is-dark'; |
33
|
|
|
private const COLOR_ALL = [ |
34
|
|
|
self::COLOR_PRIMARY, |
35
|
|
|
self::COLOR_LINK, |
36
|
|
|
self::COLOR_INFO, |
37
|
|
|
self::COLOR_SUCCESS, |
38
|
|
|
self::COLOR_WARNING, |
39
|
|
|
self::COLOR_DANGER, |
40
|
|
|
self::COLOR_DARK, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
private array $attributes = []; |
44
|
|
|
private string $autoIdPrefix = 'w'; |
45
|
|
|
private string $blockClass = 'panel-block'; |
46
|
|
|
private string $color = ''; |
47
|
|
|
private ?string $heading = null; |
48
|
|
|
private array $headingAttributes = []; |
49
|
|
|
private string $headingClass = 'panel-heading'; |
50
|
|
|
private string $iconClass = 'panel-icon'; |
51
|
|
|
private string $isActiveClass = 'is-active'; |
52
|
|
|
private string $panelClass = 'panel'; |
53
|
|
|
/** @psalm-var array<int, array> */ |
54
|
2 |
|
private array $tabs = []; |
55
|
|
|
private array $tabsAttributes = []; |
56
|
2 |
|
private string $tabClass = 'panel-tabs'; |
57
|
2 |
|
/** @psalm-var array<int, string> */ |
58
|
|
|
private array $tabItems = []; |
59
|
2 |
|
private string $template = '{panelBegin}{panelHeading}{panelTabs}{panelItems}{panelEnd}'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The HTML attributes. |
63
|
|
|
* |
64
|
|
|
* @param array $values Attribute values indexed by attribute names. |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
* |
68
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} For details on how attributes are being rendered. |
69
|
2 |
|
*/ |
70
|
|
|
public function attributes(array $values): self |
71
|
2 |
|
{ |
72
|
2 |
|
$new = clone $this; |
73
|
|
|
$new->attributes = $values; |
74
|
2 |
|
return $new; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a new instance with the specified prefix to the automatically generated widget IDs. |
79
|
|
|
* |
80
|
|
|
* @param string $value The prefix to the automatically generated widget IDs. |
81
|
|
|
* |
82
|
|
|
* @return self |
83
|
|
|
*/ |
84
|
4 |
|
public function autoIdPrefix(string $value): self |
85
|
|
|
{ |
86
|
4 |
|
$new = clone $this; |
87
|
4 |
|
$new->autoIdPrefix = $value; |
88
|
|
|
return $new; |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The panel block class. |
93
|
|
|
* |
94
|
|
|
* @param string $value The block class. |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function blockClass(string $value): self |
99
|
2 |
|
{ |
100
|
|
|
$new = clone $this; |
101
|
2 |
|
$new->blockClass = $value; |
102
|
2 |
|
return $new; |
103
|
|
|
} |
104
|
2 |
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a new instance with the specified panel color class. |
107
|
|
|
* |
108
|
|
|
* @param string $value The panel color class. Default any color. |
109
|
|
|
* Possible values are: Panel::COLOR_PRIMARY, Panel::COLOR_INFO, Panel::COLOR_SUCCESS, Panel::COLOR_WARNING, |
110
|
|
|
* Panel::COLOR_DANGER, Panel::COLOR_DARK |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
3 |
|
public function color(string $value): self |
115
|
|
|
{ |
116
|
3 |
|
if (!in_array($value, self::COLOR_ALL, true)) { |
117
|
1 |
|
$values = implode(' ', self::COLOR_ALL); |
118
|
1 |
|
throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\"."); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$new = clone $this; |
122
|
2 |
|
$new->color = $value; |
123
|
|
|
return $new; |
124
|
2 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* The panel class. |
128
|
|
|
* |
129
|
|
|
* @param string $value The panel class. |
130
|
|
|
* |
131
|
|
|
* @return self |
132
|
|
|
*/ |
133
|
|
|
public function cssClass(string $value): self |
134
|
9 |
|
{ |
135
|
|
|
$new = clone $this; |
136
|
9 |
|
$new->panelClass = $value; |
137
|
9 |
|
return $new; |
138
|
|
|
} |
139
|
9 |
|
|
140
|
|
|
/** |
141
|
|
|
* Returns a new instance with the specified panel heading. |
142
|
|
|
* |
143
|
|
|
* @param string $value The panel heading. |
144
|
|
|
* |
145
|
|
|
* @return self |
146
|
|
|
*/ |
147
|
|
|
public function heading(string $value): self |
148
|
|
|
{ |
149
|
2 |
|
$new = clone $this; |
150
|
|
|
$new->heading = $value; |
151
|
2 |
|
return $new; |
152
|
2 |
|
} |
153
|
|
|
|
154
|
2 |
|
/** |
155
|
|
|
* Returns a new instance with the specified panel heading attributes. |
156
|
|
|
* |
157
|
13 |
|
* @param array $values Attribute values indexed by attribute names. |
158
|
|
|
* |
159
|
13 |
|
* @return self |
160
|
|
|
*/ |
161
|
13 |
|
public function headingAttributes(array $values): self |
162
|
|
|
{ |
163
|
13 |
|
$new = clone $this; |
164
|
13 |
|
$new->headingAttributes = $values; |
165
|
13 |
|
return $new; |
166
|
13 |
|
} |
167
|
11 |
|
|
168
|
11 |
|
/** |
169
|
|
|
* The panel heading class. |
170
|
|
|
* |
171
|
|
|
* @param string $value The heading class. |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
public function headingClass(string $value): self |
176
|
|
|
{ |
177
|
13 |
|
$new = clone $this; |
178
|
|
|
$new->headingClass = $value; |
179
|
13 |
|
return $new; |
180
|
3 |
|
} |
181
|
3 |
|
|
182
|
|
|
/** |
183
|
|
|
* The panel icon class. |
184
|
10 |
|
* |
185
|
|
|
* @param string $value The icon class. |
186
|
|
|
* |
187
|
|
|
* @return self |
188
|
|
|
*/ |
189
|
|
|
public function iconClass(string $value): self |
190
|
|
|
{ |
191
|
|
|
$new = clone $this; |
192
|
13 |
|
$new->iconClass = $value; |
193
|
|
|
return $new; |
194
|
13 |
|
} |
195
|
8 |
|
|
196
|
8 |
|
/** |
197
|
8 |
|
* Returns a new instance with the specified ID of the widget. |
198
|
|
|
* |
199
|
|
|
* @param string $value The ID of the widget. |
200
|
6 |
|
* |
201
|
|
|
* @return self |
202
|
6 |
|
*/ |
203
|
|
|
public function id(string $value): self |
204
|
|
|
{ |
205
|
5 |
|
$new = clone $this; |
206
|
|
|
$new->attributes['id'] = $value; |
207
|
|
|
return $new; |
208
|
13 |
|
} |
209
|
|
|
|
210
|
13 |
|
/** |
211
|
|
|
* Returns a new instance with the specified active tab class. |
212
|
13 |
|
* |
213
|
|
|
* @param string $value The active tab class. |
214
|
13 |
|
* |
215
|
1 |
|
* @return self |
216
|
|
|
*/ |
217
|
13 |
|
public function isActiveClass(string $value): self |
218
|
|
|
{ |
219
|
|
|
$new = clone $this; |
220
|
|
|
$new->isActiveClass = $value; |
221
|
|
|
return $new; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns a new instance with the specified panel tabs. |
226
|
|
|
* |
227
|
8 |
|
* @param array $value The panel tabs. |
228
|
|
|
* |
229
|
8 |
|
* @return self |
230
|
8 |
|
* |
231
|
8 |
|
* @psalm-param array<int, array> $value |
232
|
8 |
|
*/ |
233
|
8 |
|
public function tabs(array $value): self |
234
|
8 |
|
{ |
235
|
8 |
|
$new = clone $this; |
236
|
|
|
$new->tabs = $value; |
237
|
8 |
|
return $new; |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
8 |
|
* The panel tab class. |
242
|
1 |
|
* |
243
|
|
|
* @param string $value The tab class. |
244
|
|
|
* |
245
|
7 |
|
* @return self |
246
|
7 |
|
*/ |
247
|
|
|
public function tabClass(string $value): self |
248
|
|
|
{ |
249
|
7 |
|
$new = clone $this; |
250
|
4 |
|
$new->tabClass = $value; |
251
|
|
|
return $new; |
252
|
|
|
} |
253
|
7 |
|
|
254
|
4 |
|
/** |
255
|
4 |
|
* Returns a new instance with the specified panel tabs attributes. |
256
|
|
|
* |
257
|
4 |
|
* @param array $values Attribute values indexed by attribute names. |
258
|
|
|
* |
259
|
4 |
|
* @return self |
260
|
4 |
|
*/ |
261
|
|
|
public function tabsAttributes(array $values): self |
262
|
|
|
{ |
263
|
3 |
|
$new = clone $this; |
264
|
|
|
$new->tabsAttributes = $values; |
265
|
3 |
|
return $new; |
266
|
|
|
} |
267
|
|
|
|
268
|
6 |
|
/** |
269
|
|
|
* Returns a new instance with the specified template. |
270
|
|
|
* |
271
|
4 |
|
* @param string $value The string the template for rendering panel: |
272
|
|
|
* |
273
|
4 |
|
* - `{panelBegin}`: _string_, which will render the panel container begin. |
274
|
4 |
|
* - `{panelHeading}`: _string_, which will render the panel heading. |
275
|
4 |
|
* - `{panelTabs}`: _string_, which will render the panel tabs. |
276
|
4 |
|
* - `{panelItems}`: _string_, which will render the panel items. |
277
|
|
|
* - `{panelEnd}`: _string_, which will render the panel container end. |
278
|
4 |
|
* |
279
|
1 |
|
* @return self |
280
|
|
|
*/ |
281
|
|
|
public function template(string $value): self |
282
|
3 |
|
{ |
283
|
3 |
|
$new = clone $this; |
284
|
|
|
$new->template = $value; |
285
|
|
|
return $new; |
286
|
3 |
|
} |
287
|
|
|
|
288
|
3 |
|
protected function run(): string |
289
|
2 |
|
{ |
290
|
|
|
$attributes = $this->attributes; |
291
|
|
|
|
292
|
3 |
|
/** @var string */ |
293
|
|
|
$attributes['id'] ??= (Html::generateId($this->autoIdPrefix) . '-panel'); |
294
|
3 |
|
|
295
|
3 |
|
$id = $attributes['id']; |
296
|
3 |
|
|
297
|
|
|
/** @var string */ |
298
|
|
|
$tag = $attributes['tag'] ?? 'nav'; |
299
|
3 |
|
|
300
|
|
|
Html::addCssClass($attributes, $this->panelClass); |
301
|
|
|
|
302
|
|
|
if ($this->color !== '') { |
303
|
|
|
Html::addCssClass($attributes, $this->color); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return strtr($this->template, [ |
307
|
|
|
'{panelBegin}' => Html::openTag($tag, $attributes) . PHP_EOL, |
308
|
|
|
'{panelHeading}' => $this->renderHeading(), |
309
|
7 |
|
'{panelTabs}' => $this->renderTabs($id), |
310
|
|
|
'{panelItems}' => implode('', $this->tabItems), |
311
|
7 |
|
'{panelEnd}' => Html::closeTag($tag), |
312
|
|
|
]); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
private function renderHeading(): string |
316
|
|
|
{ |
317
|
|
|
$headingAttributes = $this->headingAttributes; |
318
|
|
|
|
319
|
|
|
if (!empty($this->heading)) { |
320
|
|
|
Html::addCssClass($headingAttributes, $this->headingClass); |
321
|
|
|
|
322
|
|
|
return P::tag()->attributes($headingAttributes)->content($this->heading)->render() . PHP_EOL; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return ''; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
private function renderTabs(string $id): string |
329
|
|
|
{ |
330
|
|
|
$tabsAttributes = $this->tabsAttributes; |
331
|
|
|
|
332
|
|
|
if (!empty($this->tabs)) { |
333
|
|
|
$tabs = ''; |
334
|
|
|
|
335
|
|
|
foreach ($this->tabs as $index => $item) { |
336
|
|
|
$tabs .= $this->renderTab($index, $item, $id . '-' . $index) . PHP_EOL; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
Html::addCssClass($tabsAttributes, $this->tabClass); |
340
|
|
|
|
341
|
|
|
return P::tag()->attributes($tabsAttributes)->content(PHP_EOL . $tabs)->encode(false)->render() . PHP_EOL; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
return ''; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
private function renderTab(int $index, array $item, string $id): string |
348
|
|
|
{ |
349
|
|
|
/** @var string */ |
350
|
|
|
$url = $item['url'] ?? ''; |
351
|
|
|
|
352
|
|
|
/** @var string */ |
353
|
|
|
$label = $item['label'] ?? ''; |
354
|
|
|
|
355
|
|
|
/** @var bool */ |
356
|
|
|
$encode = $item['encode'] ?? true; |
357
|
|
|
|
358
|
|
|
/** @var array */ |
359
|
|
|
$urlAttributes = $item['urlAttributes'] ?? []; |
360
|
|
|
|
361
|
|
|
/** @var array */ |
362
|
|
|
$tabItems = $item['items'] ?? []; |
363
|
|
|
|
364
|
|
|
if ($url !== '') { |
365
|
|
|
$urlAttributes['href'] = $url; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
if ($label === '') { |
369
|
|
|
throw new InvalidArgumentException('The "label" option is required.'); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if ($encode === true) { |
373
|
|
|
$label = Html::encode($label); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if ($this->isActive($item)) { |
377
|
|
|
Html::addCssClass($urlAttributes, $this->isActiveClass); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if (!empty($tabItems)) { |
381
|
|
|
/** @var string */ |
382
|
|
|
$urlAttributes['href'] ??= '#' . $id; |
383
|
|
|
|
384
|
|
|
$tabsItems = ''; |
385
|
|
|
|
386
|
|
|
/** @psalm-var array[] */ |
387
|
|
|
foreach ($tabItems as $tabItem) { |
388
|
|
|
$tabsItems .= $this->renderItem($tabItem) . PHP_EOL; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$this->tabItems[$index] = $tabsItems; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return A::tag()->attributes($urlAttributes)->content($label)->encode(false)->render(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
private function renderItem(array $item): string |
398
|
|
|
{ |
399
|
|
|
/** @var array */ |
400
|
|
|
$urlAttributes = $item['urlAttributes'] ?? []; |
401
|
|
|
|
402
|
|
|
/** @var string */ |
403
|
|
|
$label = $item['label'] ?? ''; |
404
|
|
|
|
405
|
|
|
/** @var string */ |
406
|
|
|
$icon = $item['icon'] ?? ''; |
407
|
|
|
|
408
|
|
|
/** @var bool */ |
409
|
|
|
$encode = $item['encode'] ?? true; |
410
|
|
|
|
411
|
|
|
if ($label === '') { |
412
|
|
|
throw new InvalidArgumentException('The "label" option is required.'); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** @var array */ |
416
|
|
|
$labelAttributes = $item['labelAttributes'] ?? []; |
417
|
|
|
|
418
|
|
|
if ($encode) { |
419
|
|
|
$label = Html::encode($label); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
Html::addCssClass($urlAttributes, $this->blockClass); |
423
|
|
|
|
424
|
|
|
if ($this->isActive($item)) { |
425
|
|
|
Html::addCssClass($urlAttributes, $this->isActiveClass); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
Html::addCssClass($labelAttributes, $this->iconClass); |
429
|
|
|
|
430
|
|
|
if ($icon !== '') { |
431
|
|
|
$icon = PHP_EOL . I::tag()->attributes(['aria-hidden' => 'true'])->class($icon) . PHP_EOL; |
432
|
|
|
$label = PHP_EOL . Span::tag()->attributes($labelAttributes)->content($icon)->encode(false) . PHP_EOL . |
433
|
|
|
$label . PHP_EOL; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
return A::tag()->attributes($urlAttributes)->content($label)->encode(false)->render(); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Checking if active item. |
441
|
|
|
* |
442
|
|
|
* @param array $item |
443
|
|
|
* |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
|
|
private function isActive(array $item): bool |
447
|
|
|
{ |
448
|
|
|
return (bool) ArrayHelper::getValue($item, 'active', false); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|