forked from kartik-v/yii2-widget-growl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Growl.php
197 lines (175 loc) · 5.48 KB
/
Growl.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
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
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-widgets
* @subpackage yii2-widget-growl
* @version 1.1.0
*/
namespace kartik\growl;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use kartik\base\AnimateAsset;
/**
* Widget that wraps the Bootstrap Growl plugin by remabledesigns.
*
* @http://bootstrap-growl.remabledesigns.com/
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class Growl extends \kartik\base\Widget
{
const TYPE_INFO = 'info';
const TYPE_DANGER = 'danger';
const TYPE_SUCCESS = 'success';
const TYPE_WARNING = 'warning';
const TYPE_GROWL = 'growl';
const TYPE_CUSTOM = '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 class name for the icon
*/
public $icon = '';
/**
* @var string the title for the alert
*/
public $title = '';
/**
* @var string the url to redirect to on clicking the alert. If this is <code>null</code> or not set,
* the alert will not be clickable.
*/
public $linkUrl = '';
/**
* @var bool show title separator. Only applicable if `title` is set.
*/
public $showSeparator = false;
/**
* @var string the alert message body
*/
public $body = '';
/**
* @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;
/**
* @var array the options for rendering the close button tag.
*/
public $closeButton = [];
/**
* @var use animations
*/
public $useAnimation = true;
/**
* @var array the HTML attributes for the growl icon container.
*/
public $iconOptions = [];
/**
* @var array the HTML attributes for the growl title container.
*/
public $titleOptions = [];
/**
* @var array the HTML attributes for the growl message body.
*/
public $bodyOptions = [];
/**
* @var array the HTML attributes for the growl url link
*/
public $linkOptions = [];
/**
* @var array the bootstrap growl plugin configuration options
* @see http://bootstrap-growl.remabledesigns.com/
*/
public $pluginOptions = [];
/**
* @var array the first part of growl plugin settings/options
*/
private $_settings;
/**
* Initializes the widget
*/
public function init()
{
parent::init();
$this->initOptions();
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
if (empty($this->options['id'])) {
$this->options['id'] = $this->getId();
}
$this->_settings = [
'message' => $this->body,
'icon' => $this->icon,
'title' => $this->title,
'url' => $this->linkUrl
];
$this->pluginOptions['type'] = $this->type;
if (empty($this->options['class'])) {
$this->options['class'] = 'alert col-xs-10 col-sm-10 col-md-3';
} else {
Html::addCssClass($this->options, 'alert');
}
$divider = !empty($this->showSeparator) && !empty($this->title) ? '<hr class="kv-alert-separator">' . "\n" : '';
$this->iconOptions['data-growl'] = 'icon';
$this->titleOptions['data-growl'] = 'title';
$this->bodyOptions['data-growl'] = 'message';
$this->linkOptions['data-growl'] = 'url';
$content = $this->renderCloseButton() . "\n" .
Html::tag('span', '', $this->iconOptions) . "\n" .
Html::tag('span', '', $this->titleOptions) . "\n" .
$divider .
Html::tag('span', '', $this->bodyOptions) . "\n" .
Html::a('', '#', $this->linkOptions);
$this->pluginOptions['template'] = Html::tag('div', $content, $this->options);
$this->registerAssets();
}
/**
* Renders the close button.
*
* @return string the rendering result
*/
protected function renderCloseButton()
{
if ($this->closeButton !== null) {
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
$label = ArrayHelper::remove($this->closeButton, 'label', '×');
$label = '<span aria-hidden="true">' . $label . '</span>';
Html::addCssClass($this->closeButton, 'close');
if ($tag === 'button' && !isset($this->closeButton['type'])) {
$this->closeButton['type'] = 'button';
}
$this->closeButton['data-growl'] = 'dismiss';
return Html::tag($tag, $label, $this->closeButton);
} else {
return '';
}
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
GrowlAsset::register($view);
if ($this->useAnimation) {
AnimateAsset::register($view);
}
$this->registerPluginOptions('growl');
$js = '$.growl(' . Json::encode($this->_settings) . ', ' . $this->_hashVar . ');';
if (!empty($this->delay) && $this->delay > 0) {
$js = 'setTimeout(function () {' . $js . '}, ' . $this->delay . ');';
}
$view->registerJs($js);
}
}