forked from kartik-v/yii2-widget-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlert.php
More file actions
143 lines (126 loc) · 3.83 KB
/
Copy pathAlert.php
File metadata and controls
143 lines (126 loc) · 3.83 KB
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
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-widgets
* @subpackage yii2-widget-alert
* @version 1.1.0
*/
namespace kartik\alert;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Extends the \yii\bootstrap\Alert widget with additional styling and auto fade out options.
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class Alert extends \yii\bootstrap\Alert
{
const TYPE_INFO = 'alert-info';
const TYPE_DANGER = 'alert-danger';
const TYPE_SUCCESS = 'alert-success';
const TYPE_WARNING = 'alert-warning';
const TYPE_PRIMARY = 'bg-primary';
const TYPE_DEFAULT = 'well';
const TYPE_CUSTOM = 'alert-custom';
/**
* @var string the type of the alert to be displayed. One of the `TYPE_` constants.
* Defaults to `TYPE_INFO`
*/
public $type = self::TYPE_INFO;
/**
* @var string the icon type. Can be either 'class' or 'image'. Defaults to 'class'.
*/
public $iconType = 'class';
/**
* @var string the class name for the icon to be displayed. If set to empty or null, will not be
* displayed.
*/
public $icon = '';
/**
* @var array the HTML attributes for the icon.
*/
public $iconOptions = [];
/**
* @var string the title for the alert. If set to empty or null, will not be
* displayed.
*/
public $title = '';
/**
* @var array the HTML attributes for the title. The following options are additionally recognized:
* - tag: the tag to display the title. Defaults to 'span'.
*/
public $titleOptions = ['class' => 'kv-alert-title'];
/**
* @var bool show title separator. Only applicable if `title` is set.
*/
public $showSeparator = false;
/**
* @var integer the delay in microseconds after which the alert will be displayed.
* Will be useful when multiple alerts are to be shown.
*/
public $delay;
/**
* Runs the widget
*/
public function run()
{
echo $this->getTitle();
parent::run();
}
/**
* Gets the title section
*
* @return string
*/
protected function getTitle()
{
$icon = '';
$title = '';
$separator = '';
if (!empty($this->icon) && $this->iconType == 'image') {
$icon = Html::img($this->icon, $this->iconOptions);
} elseif (!empty($this->icon)) {
$this->iconOptions['class'] = $this->icon . ' ' . (empty($this->iconOptions['class']) ? 'kv-alert-title' : $this->iconOptions['class']);
$icon = Html::tag('span', '', $this->iconOptions) . ' ';
}
if (!empty($this->title)) {
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'span');
$title = Html::tag($tag, $this->title, $this->titleOptions);
if ($this->showSeparator) {
$separator = '<hr class="kv-alert-separator">' . "\n";
}
}
return $icon . $title . $separator;
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
parent::initOptions();
if (empty($this->options['id'])) {
$this->options['id'] = $this->getId();
}
$this->registerAssets();
Html::addCssClass($this->options, 'kv-alert ' . $this->type);
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
AlertAsset::register($view);
if ($this->delay > 0) {
$js = 'jQuery("#' . $this->options['id'] . '").fadeTo(' . $this->delay . ', 0.00, function() {
$(this).slideUp("slow", function() {
$(this).remove();
});
});';
$view->registerJs($js);
}
}
}