/
ButtonFixedAction.php
115 lines (109 loc) · 3.3 KB
/
ButtonFixedAction.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 namespace altiore\materialize;
use yii\helpers\Html;
use altiore\materialize\Button;
/**
* Button renders a materialize button.
*
* For example,
*
* ```php
* echo ButtonFixedAction::widget([
* 'label' => 'Action',
* 'options' => ['class' => 'btn-large'],
* 'wrapOptions' => ['style' => 'bottom: 45px; right: 24px;'],
* 'buttons' => [
* Button::widget(['label' => 'A']),
* ['label' => 'B'],
* ],
* ]);
* ```
* @see http://materializecss.com/buttons.html
* @author altiore <altiore@altiore.name>
* @since 2.0
*/
class ButtonFixedAction extends Widget
{
/**
* @var string the tag to use to render the button
*/
public $tagName = 'button';
/**
* @var string the button label
*/
public $label = 'Button';
/**
* @var boolean whether the label should be HTML-encoded.
*/
public $encodeLabel = true;
/**
* @var string default css class for button
*/
public $defaultClass = 'btn';
/**
* @var array the HTML attributes for the widget container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $wrapOptions = [];
/**
* @var string default css class for button wrapper
*/
public $defaultWrapClass = 'fixed-action-btn';
/**
* @var array list of buttons. Each array element represents a single button
* which can be specified as a string or an array of the following structure:
*
* - label: string, required, the button label.
* - options: array, optional, the HTML attributes of the button.
*/
public $buttons = [];
/**
* @var boolean whether to HTML-encode the button labels.
*/
public $encodeLabels = true;
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
$this->clientOptions = false;
if ($this->defaultWrapClass)
Html::addCssClass($this->wrapOptions, $this->defaultWrapClass);
if ($this->defaultClass)
Html::addCssClass($this->options, $this->defaultClass);
}
/**
* Renders the widget.
*/
public function run()
{
MaterializeAsset::register($this->getView());
return Html::tag(
'div',
Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options) . Html::tag('ul', $this->renderButtons()),
$this->wrapOptions
);
}
/**
* Generates the buttons that compound the group as specified on [[buttons]].
* @return string the rendering result.
*/
protected function renderButtons()
{
$buttons = [];
foreach ($this->buttons as $button) {
if (is_array($button)) {
$button['view'] = $this->getView();
$button['defaultClass'] = false;
if (!isset($button['encodeLabel'])) {
$button['encodeLabel'] = $this->encodeLabels;
}
$buttons[] = Html::tag('li', Button::widget($button));
} else {
$buttons[] = Html::tag('li', $button);
}
}
return implode("\n", $buttons);
}
}