-
Notifications
You must be signed in to change notification settings - Fork 29
/
Button.php
executable file
·194 lines (169 loc) · 5.51 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets;
use macgyer\yii2materializecss\lib\BaseWidget;
use macgyer\yii2materializecss\lib\Html;
use yii\helpers\ArrayHelper;
/**
* Button renders an HTML button.
*
* There are three main button types described in Materialize.
* - the raised button is a standard button that signifies actions and seek to give depth to a mostly flat page
* - the floating circular action button is meant for very important functions
* - flat buttons are usually used within elements that already have depth like cards or modals
*
* The button can be displayed with an optional icon. This class uses the [[Icon|Icon]] widget to show icons.
*
* @see https://materializecss.com/buttons.html
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
*/
class Button extends BaseWidget
{
/**
* Sets the [[type]] of the button to "raised". This is the default.
*/
const TYPE_RAISED = 'raised';
/**
* Sets the [[type]] of the button to "floating".
*/
const TYPE_FLOATING = 'floating';
/**
* Sets the [[type]] of the button to "flat".
*/
const TYPE_FLAT = 'flat';
/**
* Sets the [[size]] of the button to the default size.
*/
const SIZE_DEFAULT = 'default';
/**
* Sets the [[size]] of the button to "small".
*/
const SIZE_SMALL = 'small';
/**
* Sets the [[size]] of the button to "large".
*/
const SIZE_LARGE = 'large';
/**
* @var string the tag used to render the button.
*/
public $tagName = 'button';
/**
* @var string the label on the button. If you do not want a label text to be rendered, set this options to "false".
*/
public $label = 'Button';
/**
* @var boolean whether the label should be HTML-encoded.
*/
public $encodeLabel = 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
* 'position' => 'position of the icon', // optional, 'left' or 'right', defaults to 'left'
* 'options' => 'the HTML attributes for the img', // optional
* ]
* ```
*
* @see Icon|Icon
*/
public $icon = [];
/**
* @var string the type of button to be rendered.
*
* The following options are supported:
* - raised
* - floating
* - flat
*
* This property defaults to "raised". To set the type, use the corresponding `TYPE_*` constant of this class.
* If no type from this range is given, the button will be of the "raised" type.
*/
public $type = self::TYPE_RAISED;
/**
* @var string the size of button to be rendered.
*
* The following options are supported:
* - default
* - small
* - large
*
* This property defaults to "default". To set the type, use the corresponding `TYPE_*` constant of this class.
* If no type from this range is given, the button will be of the "default" type.
*/
public $size = self::SIZE_DEFAULT;
/**
* @var boolean whether the button shall be disabled.
*/
public $disabled = false;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->clientOptions = false;
Html::addCssClass($this->options, ['widget' => 'btn']);
switch ($this->type) {
case self::TYPE_FLOATING:
case self::TYPE_FLAT:
Html::addCssClass($this->options, ['btn_type' => "btn-$this->type"]);
break;
}
switch ($this->size) {
case self::SIZE_SMALL:
case self::SIZE_LARGE:
Html::addCssClass($this->options, ['btn_size' => "btn-$this->size"]);
break;
}
// if ($this->large) {
// Html::addCssClass($this->options, ['btn_size' => 'btn-large']);
// }
if ($this->disabled) {
Html::addCssClass($this->options, ['btn_disabled' => 'disabled']);
}
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
* @uses [[renderIcon]]
*/
public function run()
{
if ($this->label !== false) {
$label = $this->encodeLabel ? Html::encode($this->label) : $this->label;
} else {
$label = '';
}
$content = $this->renderIcon() . $label;
return $this->tagName === 'button' ? Html::button($content, $this->options) : Html::tag($this->tagName, $content, $this->options);
}
/**
* Renders an icon.
*
* @return string the rendered icon
* @throws \yii\base\InvalidConfigException if icon name is not specified
*
* @uses http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#getValue()-detail
* @see Icon::run
*/
protected function renderIcon()
{
if (!$this->icon) {
return '';
}
return Icon::widget([
'name' => ArrayHelper::getValue($this->icon, 'name', null),
'position' => ArrayHelper::getValue($this->icon, 'position', null),
'options' => ArrayHelper::getValue($this->icon, 'options', [])
]);
}
}