-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropdownButton.php
115 lines (97 loc) · 2.72 KB
/
DropdownButton.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
<?php
/**
* @link http://foundationize.com
* @package shqear/yii2-foundation6
* @version 1.0.0
*/
namespace shqear\foundation6;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Description of ButtonDropdown
*/
class DropdownButton extends Widget
{
/**
* @var string the button label
*/
public $label = 'Button';
/**
* @var array the HTML attributes of the button.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'button'];
/**
* The button url, used only for split button
* @var string Button url
*/
public $url = '#';
/**
* @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()
{
$dropdown = Dropdown::begin($this->getDropdownConfig());
echo "\n" . $this->renderButton($dropdown);
Dropdown::end();
$this->registerPlugin('button');
}
/**
* Generates the button dropdown.
* @return string the rendering result.
*/
protected function renderButton($dropdown)
{
$dropdownId = $dropdown->getId();
$label = $this->label;
if ($this->encodeLabel) {
$label = Html::encode($label);
}
if ($this->split) {
$this->tagName = 'a';
Html::addCssClass($this->options, 'button');
Html::addCssClass($this->options, 'split');
$options = $this->options;
$label .= Html::tag('span', '', ['data-dropdown' => $dropdownId]);
} else {
Html::addCssClass($this->options, 'dropdown');
$options = $this->options;
$options['data-dropdown'] = $dropdownId;
}
return Button::widget([
'tagName' => $this->tagName,
'label' => $label,
'options' => $options,
'url' => $this->url,
'encodeLabel' => false,
]) . "<br />\n";
}
/**
* Get config for [[Dropdown]] widget
* @return array config options
*/
protected function getDropdownConfig()
{
$config = $this->dropdown;
$config['id'] = ArrayHelper::getValue($config, 'id', null);
$config['clientOptions'] = false;
$config['view'] = $this->getView();
return $config;
}
}