forked from yiisoft/yii2-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathButtonDropdown.php
144 lines (134 loc) · 4.02 KB
/
ButtonDropdown.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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* ButtonDropdown renders a group or split button dropdown bootstrap component.
*
* For example,
*
* ```php
* // a button group using Dropdown widget
* echo ButtonDropdown::widget([
* 'label' => 'Action',
* 'dropdown' => [
* 'items' => [
* ['label' => 'DropdownA', 'url' => '/'],
* ['label' => 'DropdownB', 'url' => '#'],
* ],
* ],
* ]);
* ```
* @see http://getbootstrap.com/javascript/#buttons
* @see http://getbootstrap.com/components/#btn-dropdowns
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @since 2.0
*/
class ButtonDropdown extends Widget
{
/**
* @var string the button label
*/
public $label = 'Button';
/**
* @var array the HTML attributes for the container tag. The following special options are recognized:
*
* - tag: string, defaults to "div", the name of the container tag.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
* @since 2.0.1
*/
public $containerOptions = [];
/**
* @var array the HTML attributes of the button.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array the configuration array for [[Dropdown]].
*/
public $dropdown = [];
/**
* @var boolean whether to display a group of split-styled button group.
*/
public $split = false;
/**
* @var string the tag to use to render the button
*/
public $tagName = 'button';
/**
* @var boolean whether the label should be HTML-encoded.
*/
public $encodeLabel = true;
/**
* Renders the widget.
*/
public function run()
{
Html::addCssClass($this->containerOptions, 'btn-group');
$options = $this->containerOptions;
$tag = ArrayHelper::remove($options, 'tag', 'div');
$this->registerPlugin('button');
return implode("\n", [
Html::beginTag($tag, $this->containerOptions),
$this->renderButton(),
$this->renderDropdown(),
Html::endTag($tag)
]);
}
/**
* Generates the button dropdown.
* @return string the rendering result.
*/
protected function renderButton()
{
Html::addCssClass($this->options, 'btn');
$label = $this->label;
if ($this->encodeLabel) {
$label = Html::encode($label);
}
if ($this->split) {
$options = $this->options;
$this->options['data-toggle'] = 'dropdown';
Html::addCssClass($this->options, 'dropdown-toggle');
$splitButton = Button::widget([
'label' => '<span class="caret"></span>',
'encodeLabel' => false,
'options' => $this->options,
'view' => $this->getView(),
]);
} else {
$label .= ' <span class="caret"></span>';
$options = $this->options;
if (!isset($options['href'])) {
$options['href'] = '#';
}
Html::addCssClass($options, 'dropdown-toggle');
$options['data-toggle'] = 'dropdown';
$splitButton = '';
}
return Button::widget([
'tagName' => $this->tagName,
'label' => $label,
'options' => $options,
'encodeLabel' => false,
'view' => $this->getView(),
]) . "\n" . $splitButton;
}
/**
* Generates the dropdown menu.
* @return string the rendering result.
*/
protected function renderDropdown()
{
$config = $this->dropdown;
$config['clientOptions'] = false;
$config['view'] = $this->getView();
return Dropdown::widget($config);
}
}