|
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
|
|
|
* @param bool $value |
|
216
|
|
|
* |
|
217
|
|
|
* @return self |
|
218
|
|
|
*/ |
|
219
|
1 |
|
public function withEncodeTags(bool $value = true): self |
|
220
|
|
|
{ |
|
221
|
1 |
|
$new = clone $this; |
|
222
|
1 |
|
$new->encodeTags = $value; |
|
223
|
|
|
|
|
224
|
1 |
|
return $new; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Renders the header HTML markup of the toast. |
|
229
|
|
|
* |
|
230
|
|
|
* @throws JsonException |
|
231
|
|
|
* |
|
232
|
|
|
* @return string the rendering result |
|
233
|
|
|
*/ |
|
234
|
8 |
|
private function renderHeader(): string |
|
235
|
|
|
{ |
|
236
|
8 |
|
$button = $this->renderCloseButton(); |
|
237
|
8 |
|
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong'); |
|
238
|
8 |
|
Html::addCssClass($this->titleOptions, ['widget' => 'me-auto']); |
|
239
|
8 |
|
$title = Html::tag($tag, $this->title, $this->titleOptions); |
|
240
|
|
|
|
|
241
|
8 |
|
if ($this->dateTime !== '') { |
|
242
|
2 |
|
$tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small'); |
|
243
|
2 |
|
$title .= "\n" . Html::tag($tag, $this->dateTime, $this->dateTimeOptions); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
8 |
|
$title .= "\n" . $button; |
|
247
|
|
|
|
|
248
|
8 |
|
Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']); |
|
249
|
8 |
|
return Html::div("\n" . $title . "\n", $this->headerOptions); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Renders the opening tag of the toast body. |
|
254
|
|
|
* |
|
255
|
|
|
* @throws JsonException |
|
256
|
|
|
* |
|
257
|
|
|
* @return string the rendering result |
|
258
|
|
|
*/ |
|
259
|
8 |
|
private function renderBodyBegin(): string |
|
260
|
|
|
{ |
|
261
|
8 |
|
Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']); |
|
262
|
8 |
|
return Html::beginTag('div', $this->bodyOptions); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Renders the closing tag of the toast body. |
|
267
|
|
|
* |
|
268
|
|
|
* @return string the rendering result |
|
269
|
|
|
*/ |
|
270
|
8 |
|
private function renderBodyEnd(): string |
|
271
|
|
|
{ |
|
272
|
8 |
|
return $this->body . "\n" . Html::endTag('div'); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Renders the close button. |
|
277
|
|
|
* |
|
278
|
|
|
* @throws JsonException |
|
279
|
|
|
* |
|
280
|
|
|
* @return string the rendering result |
|
281
|
|
|
*/ |
|
282
|
8 |
|
private function renderCloseButton(): string |
|
283
|
|
|
{ |
|
284
|
8 |
|
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button'); |
|
285
|
8 |
|
$label = ArrayHelper::remove( |
|
286
|
8 |
|
$this->closeButton, |
|
287
|
8 |
|
'label', |
|
288
|
8 |
|
Html::tag('span', '×', ['aria-hidden' => 'true', 'encode' => false]) |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
8 |
|
return Html::tag($tag, "\n" . $label . "\n", $this->closeButton); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Initializes the widget options. |
|
296
|
|
|
* |
|
297
|
|
|
* This method sets the default values for various options. |
|
298
|
|
|
*/ |
|
299
|
8 |
|
private function initOptions(): void |
|
300
|
|
|
{ |
|
301
|
8 |
|
if ($this->encodeTags === false) { |
|
302
|
7 |
|
$this->bodyOptions = array_merge($this->bodyOptions, ['encode' => false]); |
|
303
|
7 |
|
$this->closeButton = array_merge($this->closeButton, ['encode' => false]); |
|
304
|
7 |
|
$this->dateTimeOptions = array_merge($this->dateTimeOptions, ['encode' => false]); |
|
305
|
7 |
|
$this->headerOptions = array_merge($this->headerOptions, ['encode' => false]); |
|
306
|
7 |
|
$this->options = array_merge($this->options, ['encode' => false]); |
|
307
|
7 |
|
$this->titleOptions = array_merge($this->titleOptions, ['encode' => false]); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
8 |
|
Html::addCssClass($this->options, ['widget' => 'toast']); |
|
311
|
|
|
|
|
312
|
8 |
|
$this->closeButton = array_merge([ |
|
313
|
8 |
|
'aria' => ['label' => 'Close'], |
|
314
|
|
|
'data' => ['bs-dismiss' => 'toast'], |
|
315
|
|
|
'class' => ['widget' => 'btn-close'], |
|
316
|
|
|
'type' => 'button', |
|
317
|
8 |
|
], $this->closeButton); |
|
318
|
|
|
|
|
319
|
8 |
|
if (!isset($this->options['role'])) { |
|
320
|
8 |
|
$this->options['role'] = 'alert'; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
8 |
|
if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) { |
|
324
|
8 |
|
$this->options['aria'] = [ |
|
325
|
|
|
'live' => 'assertive', |
|
326
|
|
|
'atomic' => 'true', |
|
327
|
|
|
]; |
|
328
|
|
|
} |
|
329
|
8 |
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|