1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
use Yiisoft\Html\Html; |
10
|
|
|
|
11
|
|
|
use function array_merge; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Toasts renders an toast bootstrap component. |
15
|
|
|
* |
16
|
|
|
* For example, |
17
|
|
|
* |
18
|
|
|
* ```php |
19
|
|
|
* Toast::widget() |
20
|
|
|
* ->title('Hello world!') |
21
|
|
|
* ->dateTime('a minute ago') |
22
|
|
|
* ->body('Say hello...') |
23
|
|
|
* ]) |
24
|
|
|
* ->begin(); |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* The following example will show the content enclosed between the [[begin()]] |
28
|
|
|
* and [[end()]] calls within the toast box: |
29
|
|
|
* |
30
|
|
|
* ```php |
31
|
|
|
* Toast::widget() |
32
|
|
|
* ->title('Hello world!') |
33
|
|
|
* ->dateTime('a minute ago') |
34
|
|
|
* ]) |
35
|
|
|
* ->begin(); |
36
|
|
|
* |
37
|
|
|
* echo 'Say hello...'; |
38
|
|
|
* |
39
|
|
|
* Toast::end(); |
40
|
|
|
* ``` |
41
|
|
|
* |
42
|
|
|
* @see https://getbootstrap.com/docs/components/toasts/ |
43
|
|
|
* |
44
|
|
|
* @author Simon Karlen <[email protected]> |
45
|
|
|
*/ |
46
|
|
|
final class Toast extends Widget |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var string the body content in the alert component. Note that anything between |
50
|
|
|
* the [[begin()]] and [[end()]] calls of the Toast widget will also be treated |
51
|
|
|
* as the body content, and will be rendered before this. |
52
|
|
|
*/ |
53
|
|
|
private string $body = ''; |
54
|
|
|
/** |
55
|
|
|
* @var string The title content in the toast. |
56
|
|
|
*/ |
57
|
|
|
private string $title = ''; |
58
|
|
|
/** |
59
|
|
|
* @var string The footer content in the toast. |
60
|
|
|
*/ |
61
|
|
|
private string $footer = ''; |
62
|
|
|
/** |
63
|
|
|
* @var string The date time the toast message references to. |
64
|
|
|
* This will be formatted as relative time (via formatter component). It will be omitted if |
65
|
|
|
* set to `false` (default). |
66
|
|
|
*/ |
67
|
|
|
private string $dateTime = ''; |
68
|
|
|
/** |
69
|
|
|
* @var array the options for rendering the close button tag. |
70
|
|
|
* The close button is displayed in the header of the toast. Clicking on the button will hide the toast. |
71
|
|
|
* |
72
|
|
|
* The following special options are supported: |
73
|
|
|
* |
74
|
|
|
* - tag: string, the tag name of the button. Defaults to 'button'. |
75
|
|
|
* - label: string, the label of the button. Defaults to '×'. |
76
|
|
|
* |
77
|
|
|
* The rest of the options will be rendered as the HTML attributes of the button tag. |
78
|
|
|
* Please refer to the [Toast documentation](https://getbootstrap.com/docs/4.5/components/toasts/) |
79
|
|
|
* for the supported HTML attributes. |
80
|
|
|
*/ |
81
|
|
|
private array $closeButton = []; |
82
|
|
|
/** |
83
|
|
|
* @var array additional title options |
84
|
|
|
* |
85
|
|
|
* The following special options are supported: |
86
|
|
|
* |
87
|
|
|
* - tag: string, the tag name of the button. Defaults to 'strong'. |
88
|
|
|
* |
89
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
90
|
|
|
*/ |
91
|
|
|
public array $titleOptions = []; |
92
|
|
|
/** |
93
|
|
|
* @var array additional date time part options |
94
|
|
|
* |
95
|
|
|
* The following special options are supported: |
96
|
|
|
* |
97
|
|
|
* - tag: string, the tag name of the button. Defaults to 'small'. |
98
|
|
|
* |
99
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
100
|
|
|
*/ |
101
|
|
|
private array $dateTimeOptions = []; |
102
|
|
|
/** |
103
|
|
|
* @var array additional header options |
104
|
|
|
* |
105
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
106
|
|
|
*/ |
107
|
|
|
private array $headerOptions = []; |
108
|
|
|
/** |
109
|
|
|
* @var array body options |
110
|
|
|
* |
111
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
112
|
|
|
*/ |
113
|
|
|
private array $bodyOptions = []; |
114
|
|
|
/** |
115
|
|
|
* @var array options |
116
|
|
|
*/ |
117
|
|
|
private array $options = []; |
118
|
|
|
/** |
119
|
|
|
* @var array footer options |
120
|
|
|
* |
121
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
122
|
|
|
*/ |
123
|
|
|
private array $footerOptions = []; |
124
|
|
|
|
125
|
4 |
|
public function begin(): ?string |
126
|
|
|
{ |
127
|
4 |
|
parent::begin(); |
128
|
|
|
|
129
|
4 |
|
if (!isset($this->options['id'])) { |
130
|
4 |
|
$this->options['id'] = "{$this->getId()}-toast"; |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
$this->initOptions(); |
134
|
|
|
|
135
|
|
|
return |
136
|
4 |
|
Html::beginTag('div', $this->options) . "\n" . |
137
|
4 |
|
$this->renderHeader() . "\n" . |
138
|
4 |
|
$this->renderBodyBegin(); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
protected function run(): string |
142
|
|
|
{ |
143
|
4 |
|
$this->registerPlugin('toast', $this->options); |
144
|
|
|
|
145
|
4 |
|
return $this->renderBodyEnd() . Html::endTag('div'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Renders the header HTML markup of the toast. |
150
|
|
|
* |
151
|
|
|
* @throws JsonException |
152
|
|
|
* |
153
|
|
|
* @return string the rendering result |
154
|
|
|
*/ |
155
|
4 |
|
protected function renderHeader(): string |
156
|
|
|
{ |
157
|
4 |
|
$button = $this->renderCloseButton(); |
158
|
4 |
|
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong'); |
159
|
4 |
|
Html::addCssClass($this->titleOptions, ['widget' => 'me-auto']); |
160
|
4 |
|
$title = Html::tag($tag, $this->title, $this->titleOptions); |
161
|
|
|
|
162
|
4 |
|
if ($this->dateTime !== '') { |
163
|
2 |
|
$tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small'); |
164
|
2 |
|
$title .= "\n" . Html::tag($tag, $this->dateTime, $this->dateTimeOptions); |
165
|
|
|
} |
166
|
|
|
|
167
|
4 |
|
$title .= "\n" . $button; |
168
|
|
|
|
169
|
4 |
|
Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']); |
170
|
4 |
|
return Html::div("\n" . $title . "\n", $this->headerOptions); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Renders the opening tag of the toast body. |
175
|
|
|
* |
176
|
|
|
* @throws JsonException |
177
|
|
|
* |
178
|
|
|
* @return string the rendering result |
179
|
|
|
*/ |
180
|
4 |
|
protected function renderBodyBegin(): string |
181
|
|
|
{ |
182
|
4 |
|
Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']); |
183
|
4 |
|
return Html::beginTag('div', $this->bodyOptions); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Renders the closing tag of the toast body. |
188
|
|
|
* |
189
|
|
|
* @return string the rendering result |
190
|
|
|
*/ |
191
|
4 |
|
protected function renderBodyEnd(): string |
192
|
|
|
{ |
193
|
4 |
|
return $this->body . "\n" . Html::endTag('div'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Renders the close button. |
198
|
|
|
* |
199
|
|
|
* @throws JsonException |
200
|
|
|
* |
201
|
|
|
* @return string the rendering result |
202
|
|
|
*/ |
203
|
4 |
|
protected function renderCloseButton(): ?string |
204
|
|
|
{ |
205
|
4 |
|
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button'); |
206
|
4 |
|
$label = ArrayHelper::remove($this->closeButton, 'label', Html::tag('span', '×', [ |
207
|
4 |
|
'aria-hidden' => 'true', |
208
|
|
|
])); |
209
|
|
|
|
210
|
4 |
|
return Html::tag($tag, "\n" . $label . "\n", $this->closeButton); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Initializes the widget options. |
215
|
|
|
* |
216
|
|
|
* This method sets the default values for various options. |
217
|
|
|
*/ |
218
|
4 |
|
protected function initOptions(): void |
219
|
|
|
{ |
220
|
4 |
|
Html::addCssClass($this->options, ['widget' => 'toast']); |
221
|
|
|
|
222
|
4 |
|
$this->closeButton = array_merge([ |
223
|
4 |
|
'aria' => ['label' => 'Close'], |
224
|
|
|
'data' => ['bs-dismiss' => 'toast'], |
225
|
|
|
'class' => ['widget' => 'btn-close'], |
226
|
|
|
'type' => 'button', |
227
|
4 |
|
], $this->closeButton); |
228
|
|
|
|
229
|
4 |
|
if (!isset($this->options['role'])) { |
230
|
4 |
|
$this->options['role'] = 'alert'; |
231
|
|
|
} |
232
|
|
|
|
233
|
4 |
|
if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) { |
234
|
4 |
|
$this->options['aria'] = [ |
235
|
|
|
'live' => 'assertive', |
236
|
|
|
'atomic' => 'true', |
237
|
|
|
]; |
238
|
|
|
} |
239
|
4 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Body options. |
243
|
|
|
* |
244
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
245
|
|
|
* |
246
|
|
|
* @param array $value |
247
|
|
|
* |
248
|
|
|
* @return $this |
249
|
|
|
*/ |
250
|
1 |
|
public function bodyOptions(array $value): self |
251
|
|
|
{ |
252
|
1 |
|
$this->bodyOptions = $value; |
253
|
|
|
|
254
|
1 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* The options for rendering the close button tag. |
259
|
|
|
* |
260
|
|
|
* The close button is displayed in the header of the toast window. Clicking on the button will hide the toast |
261
|
|
|
* window. If {@see closeButtonEnabled} is false, no close button will be rendered. |
262
|
|
|
* |
263
|
|
|
* The following special options are supported: |
264
|
|
|
* |
265
|
|
|
* - tag: string, the tag name of the button. Defaults to 'button'. |
266
|
|
|
* - label: string, the label of the button. Defaults to '×'. |
267
|
|
|
* |
268
|
|
|
* The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the |
269
|
|
|
* [Toast plugin help](https://getbootstrap.com/docs/components/toasts/) for the supported HTML attributes. |
270
|
|
|
* |
271
|
|
|
* @param array $value |
272
|
|
|
* |
273
|
|
|
* @return $this |
274
|
|
|
*/ |
275
|
|
|
public function closeButton(array $value): self |
276
|
|
|
{ |
277
|
|
|
$this->closeButton = $value; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* The date/time content in the toast window. |
284
|
|
|
* |
285
|
|
|
* @param string $value |
286
|
|
|
* |
287
|
|
|
* @return $this |
288
|
|
|
*/ |
289
|
2 |
|
public function dateTime(string $value): self |
290
|
|
|
{ |
291
|
2 |
|
$this->dateTime = $value; |
292
|
|
|
|
293
|
2 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Additional DateTime options. |
298
|
|
|
* |
299
|
|
|
* @param array $value |
300
|
|
|
* |
301
|
|
|
* @return $this |
302
|
|
|
* |
303
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
304
|
|
|
*/ |
305
|
1 |
|
public function dateTimeOptions(array $value): self |
306
|
|
|
{ |
307
|
1 |
|
$this->dateTimeOptions = $value; |
308
|
|
|
|
309
|
1 |
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* The footer content in the toast window. |
314
|
|
|
* |
315
|
|
|
* @param string|null $value |
316
|
|
|
* |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
|
|
public function footer(?string $value): self |
320
|
|
|
{ |
321
|
|
|
$this->footer = $value; |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Additional footer options. |
328
|
|
|
* |
329
|
|
|
* @param array $value |
330
|
|
|
* |
331
|
|
|
* @return $this |
332
|
|
|
* |
333
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
334
|
|
|
*/ |
335
|
|
|
public function footerOptions(array $value): self |
336
|
|
|
{ |
337
|
|
|
$this->footerOptions = $value; |
338
|
|
|
|
339
|
|
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Additional header options. |
344
|
|
|
* |
345
|
|
|
* @param array $value |
346
|
|
|
* |
347
|
|
|
* @return $this |
348
|
|
|
* |
349
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
350
|
|
|
*/ |
351
|
|
|
public function headerOptions(array $value): self |
352
|
|
|
{ |
353
|
|
|
$this->headerOptions = $value; |
354
|
|
|
|
355
|
|
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param array $value the HTML attributes for the widget container tag. The following special options are |
360
|
|
|
* recognized. |
361
|
|
|
* |
362
|
|
|
* @return $this |
363
|
|
|
* |
364
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
365
|
|
|
*/ |
366
|
|
|
public function options(array $value): self |
367
|
|
|
{ |
368
|
|
|
$this->options = $value; |
369
|
|
|
|
370
|
|
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* The title content in the toast window. |
375
|
|
|
* |
376
|
|
|
* @param string|null $value |
377
|
|
|
* |
378
|
|
|
* @return $this |
379
|
|
|
*/ |
380
|
3 |
|
public function title(?string $value): self |
381
|
|
|
{ |
382
|
3 |
|
$this->title = $value; |
383
|
|
|
|
384
|
3 |
|
return $this; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Additional title options. |
389
|
|
|
* |
390
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
391
|
|
|
* |
392
|
|
|
* @param array $value |
393
|
|
|
* |
394
|
|
|
* @return $this |
395
|
|
|
*/ |
396
|
1 |
|
public function titleOptions(array $value): self |
397
|
|
|
{ |
398
|
1 |
|
$this->titleOptions = $value; |
399
|
|
|
|
400
|
1 |
|
return $this; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|