-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedActionButton.php
executable file
·150 lines (144 loc) · 5.07 KB
/
FixedActionButton.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
<?php
namespace makroxyz\materializecss;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Class FixedActionButton
* @package makroxyz\materializecss
*/
class FixedActionButton extends Widget
{
/**
* @var array list of button items in the fixed action button. Each element can be either an HTML string
* or an array representing a single item with the following specification:
*
* - label: string, required, the label of the item link
* - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]].
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item link.
* - options: array, optional, the HTML attributes of the item.
* */
public $items = [];
/**
* @var boolean whether the labels for header items should be HTML-encoded.
*/
public $encodeLabels = true;
/**
* @var array the HTML attributes for the widget container tag
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'fixed-action-btn'];
/**
* @var array the HTML attributes for the container around the button items
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $itemsContainerOptions = [];
/**
* @var bool whether the button items are only visible after click
*/
public $clickToToggle = false;
/**
* @var bool whether to display a horizontal FAB
*/
public $horizontal = false;
/**
* @var string the tag used to render the button
*
* Should be a for correct functionality of click to toggle FAB
*/
public $buttonTagName = 'a';
/**
* @var string the label on the button
*/
public $buttonLabel = 'Button';
/**
* @var bool whether the label should be HTML-encoded.
*/
public $buttonEncodeLabel = true;
/**
* @var array the options for the optional icon
*
* To specify an icon you can use the following parameters
*
* ```php
* [
* 'name' => 'name of the icon', // required
* 'options' => 'the HTML attributes for the img', // optional
* ]
* ```
*/
public $buttonIcon;
/**
* @var array the HTML attributes for the visible button
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $buttonOptions = ['class' => 'btn-large red'];
/**
* Initialize the widget.
*/
public function init()
{
parent::init();
Html::addCssClass($this->buttonOptions, ['widget' => 'btn-floating']);
if ($this->clickToToggle) {
Html::addCssClass($this->options, ['container' => 'click-to-toggle']);
}
if ($this->horizontal) {
Html::addCssClass($this->options, ['containerLayout' => 'horizontal']);
}
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
*/
public function run()
{
$html = Html::beginTag('div', $this->options);
// Visible button
$html .= Button::widget([
'tagName' => $this->buttonTagName,
'label' => $this->buttonIcon . $this->buttonLabel,
'encodeLabel' => $this->buttonEncodeLabel,
'options' => $this->buttonOptions,
]);
$html .= $this->renderItems();
$html .= Html::endTag('div');
return $html;
}
/**
* @return string
* @throws InvalidConfigException
*/
protected function renderItems()
{
$elements = [];
$items = $this->items;
foreach ($items as $item) {
if (isset($item['visible']) && !$item['visible']) {
continue;
}
if (is_string($item)) {
$elements[] = $item;
continue;
}
if (!array_key_exists('label', $item)) {
throw new InvalidConfigException("The 'label' option is required.");
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$itemOptions = ArrayHelper::getValue($item, 'options', []);
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$url = array_key_exists('url', $item) ? $item['url'] : null;
if ($url === null) {
$content = $label;
} else {
$content = Html::a($label, $url, $linkOptions);
}
$elements[] = Html::tag('li', $content, $itemOptions);
}
return Html::tag('ul', implode("\n", $elements), $this->itemsContainerOptions);
}
}