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