-
Notifications
You must be signed in to change notification settings - Fork 1
/
Button.php
167 lines (140 loc) · 3.96 KB
/
Button.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
<?php
namespace iutbay\yii2bootstrap;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use iutbay\yii2fontawesome\FontAwesome as FA;
/**
* Button widget
* @author Kevin LEVRON <[email protected]>
*/
class Button extends \yii\bootstrap\Button
{
/**
* The URL for the hyperlink tag. This parameter will be processed by [[Url::to()]]
* @var array|string|null
*/
public $url;
/**
* @var string the button label
*/
public $label = '';
/**
* Button type
* @var string
*/
public $type = 'default';
/**
* Button size
* @var string
*/
public $size = '';
/**
* FontAwesome Icon
* @var string
*/
public $icon;
/**
* @var boolean
*/
public $visible = true;
/**
* @var boolean
*/
public $disabled = false;
/**
* Tooltip
* @var string
*/
public $tooltip = '';
/**
* Dropdown items
* @var string
*/
public $items;
/**
* Split button dropdowns
* @var boolean
*/
public $split = true;
/**
* Renders the widget.
*/
public function run()
{
if (!$this->visible)
return '';
if ($this->url !== null) {
$this->options['href'] = Url::to($this->url);
$this->tagName = 'a';
}
if ($this->disabled) {
$this->options['disabled'] = 'disabled';
$this->tagName = 'button';
$this->items = null;
unset($this->options['href'], $this->options['target']);
}
if (!empty($this->type)) {
if ($this->type == 'submit') {
$this->options['type'] = $this->type;
Html::addCssClass($this->options, 'btn-primary');
} else if ($this->type == 'button') {
$this->options['type'] = $this->type;
Html::addCssClass($this->options, 'btn-default');
} else {
Html::addCssClass($this->options, 'btn-' . $this->type);
}
}
if (!empty($this->size)) {
Html::addCssClass($this->options, 'btn-' . $this->size);
}
if (!empty($this->tooltip)) {
$this->options['title'] = $this->tooltip;
$this->options['data']['toggle'] = 'tooltip';
$this->options['data']['container'] = 'body';
}
if (!empty($this->icon)) {
$this->label = FA::icon($this->icon) . ' ' . $this->label;
$this->encodeLabel = false;
}
if (is_array($this->items)) {
return $this->renderDropdown();
}
return parent::run();
}
/**
* Render a dropdown button
* @return string
*/
protected function renderDropdown()
{
// build button(s)
$caret = '<span class="caret"></span>';
if ($this->split) {
$button = parent::run();
$button.= self::widget([
'label' => $caret,
'encodeLabel' => false,
'type' => $this->type,
'options' => [
'class' => 'dropdown-toggle',
'data-toggle' => 'dropdown',
],
]);
} else {
$this->label.= " {$caret}";
$this->encodeLabel = false;
$this->tooltip = '';
$this->options['data-toggle'] = 'dropdown';
Html::addCssClass($this->options, 'dropdown-toggle');
$button = parent::run();
}
// build dropdown
$dropdown = Dropdown::widget([
'items' => $this->items,
]);
// return button group
$buttonGroup = Html::tag('div', "{$button}\n{$dropdown}", ['class' => 'btn-group']);
return $buttonGroup;
}
}