-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAlert.php
137 lines (122 loc) · 3.07 KB
/
Alert.php
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
<?php
/**
* This file is part of the yii2-uikit project.
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
* @copyright yii2-uikit (c) 2018
* @author Eugene Zakirov (worstinme) <[email protected]>
*/
namespace worstinme\uikit;
use Yii;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Alert renders an alert uikit component.
*
* For example,
*
* ```php
* echo Alert::widget([
* 'type'=>'primary',
* 'body' => 'Say hello...',
* ]);
* ```
*
* The following example will show the content enclosed between the [[begin()]]
* and [[end()]] calls within the alert box:
*
* ```php
* Alert::begin([
* 'type' => 'warning',
* 'closeButton'=>false,
* ]);
*
* echo 'Say hello...';
*
* Alert::end();
* ```
*/
class Alert extends Widget
{
/**
* @var string the body content in the alert component. Note that anything between
* the [[begin()]] and [[end()]] calls of the Alert widget will also be treated
* as the body content, and will be rendered before this.
*/
public $body;
/**
* @var array|false the options for rendering the close button tag.
*/
public $closeButton = [];
/**
* @var string alert colored type
*/
public $type = 'primary';
/**
* {@inheritdoc}
*/
public $options = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->initOptions();
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderBodyBegin() . "\n";
}
/**
* Renders the widget.
*/
public function run()
{
echo "\n" . $this->renderBodyEnd();
echo "\n" . Html::endTag('div');
}
/**
* Renders the close button if any before rendering the content.
* @return string the rendering result
*/
protected function renderBodyBegin()
{
return $this->renderCloseButton();
}
/**
* Renders the alert body (if any).
* @return string the rendering result
*/
protected function renderBodyEnd()
{
return $this->body . "\n";
}
/**
* Renders the close button.
* @return string the rendering result
*/
protected function renderCloseButton()
{
if (($closeButton = $this->closeButton) !== false) {
$tag = ArrayHelper::remove($closeButton, 'tag', 'a');
$label = ArrayHelper::remove($closeButton, 'label', '');
if ($tag === 'a') {
$closeButton['class'] = 'uk-alert-close';
$closeButton['uk-close'] = true;
}
return Html::tag($tag, $label, $closeButton);
} else {
return null;
}
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
Html::addCssClass($this->options, 'uk-alert uk-alert-'.$this->type);
$this->options['uk-alert'] = true;
}
}