1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\bootstrap4;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Toasts renders an toast bootstrap component.
*
* For example,
*
* ```php
* echo Toast::widget([
* 'title' => 'Hello world!',
* 'dateTime' => 'now',
* 'body' => 'Say hello...',
* ]);
* ```
*
* The following example will show the content enclosed between the [[begin()]]
* and [[end()]] calls within the toast box:
*
* ```php
* Toast::begin([
* 'title' => 'Hello world!',
* 'dateTime' => 'now'
* ]);
*
* echo 'Say hello...';
*
* Toast::end();
* ```
*
* @see https://getbootstrap.com/docs/4.5/components/toasts/
* @author Simon Karlen <simi.albi@outlook.com>
*/
class Toast extends Widget
{
/**
* @var string|null the body content in the alert component. Note that anything between
* the [[begin()]] and [[end()]] calls of the Toast widget will also be treated
* as the body content, and will be rendered before this.
*/
public $body;
/**
* @var string|null The title content in the toast.
*/
public $title;
/**
* @var int|string|\DateTime|\DateTimeInterface|\DateInterval|false The date time the toast message references to.
* This will be formatted as relative time (via formatter component). It will be omitted if
* set to `false` (default).
*/
public $dateTime = false;
/**
* @var array the options for rendering the close button tag.
* The close button is displayed in the header of the toast. Clicking on the button will hide the toast.
*
* The following special options are supported:
*
* - tag: string, the tag name of the button. Defaults to 'button'.
* - label: string, the label of the button. Defaults to '×'.
*
* The rest of the options will be rendered as the HTML attributes of the button tag.
* Please refer to the [Toast documentation](https://getbootstrap.com/docs/4.5/components/toasts/)
* for the supported HTML attributes.
*/
public $closeButton = [];
/**
* @var array additional title options
*
* The following special options are supported:
*
* - tag: string, the tag name of the button. Defaults to 'strong'.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $titleOptions = [];
/**
* @var array additional date time part options
*
* The following special options are supported:
*
* - tag: string, the tag name of the button. Defaults to 'small'.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $dateTimeOptions = [];
/**
* @var array additional header options
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $headerOptions = [];
/**
* @var array body options
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $bodyOptions = [];
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->initOptions();
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderHeader() . "\n";
echo $this->renderBodyBegin() . "\n";
}
/**
* {@inheritdoc}
*/
public function run()
{
echo "\n" . $this->renderBodyEnd();
echo "\n" . Html::endTag('div');
$this->registerPlugin('toast');
}
/**
* Renders the header HTML markup of the modal
* @return string the rendering result
*/
protected function renderHeader()
{
$button = $this->renderCloseButton();
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong');
Html::addCssClass($this->titleOptions, ['widget' => 'mr-auto']);
$title = Html::tag($tag, $this->title === null ? '' : $this->title, $this->titleOptions);
if ($this->dateTime !== false) {
$tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small');
Html::addCssClass($this->dateTimeOptions, ['widget' => 'text-muted']);
$title .= "\n" . Html::tag($tag, Yii::$app->formatter->asRelativeTime($this->dateTime), $this->dateTimeOptions);
}
$title .= "\n" . $button;
Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']);
return Html::tag('div', "\n" . $title . "\n", $this->headerOptions);
}
/**
* Renders the opening tag of the toast body.
* @return string the rendering result
*/
protected function renderBodyBegin()
{
Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']);
return Html::beginTag('div', $this->bodyOptions);
}
/**
* Renders the toast body and the close button (if any).
* @return string the rendering result
*/
protected function renderBodyEnd()
{
return $this->body . "\n" . Html::endTag('div');
}
/**
* Renders the close button.
* @return string the rendering result
*/
protected function renderCloseButton()
{
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
$label = ArrayHelper::remove($this->closeButton, 'label', Html::tag('span', '×', [
'aria-hidden' => 'true'
]));
return Html::tag($tag, "\n" . $label . "\n", $this->closeButton);
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
Html::addCssClass($this->options, ['widget' => 'toast']);
$this->closeButton = array_merge([
'aria' => ['label' => 'Close'],
'data' => ['dismiss' => 'toast'],
'class' => ['widget' => 'ml-2 mb-1 close'],
'type' => 'button'
], $this->closeButton);
if (!isset($this->options['role'])) {
$this->options['role'] = 'alert';
}
if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) {
$this->options['aria'] = [
'live' => 'assertive',
'atomic' => 'true'
];
}
}
}