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