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