-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSpinner.php
executable file
·201 lines (173 loc) · 5.63 KB
/
Spinner.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
195
196
197
198
199
200
201
<?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;
/**
* Spinner renders a circular loading animation.
*
* When displaying a spinner you can choose to let the colors change with every rotation.
*
* @see Progress|Progress
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @since 1.0.2
* @package widgets
*/
class Spinner extends BaseWidget
{
/**
* Sets the [[size]] of the spinner to "small".
*/
const SIZE_SMALL = 10;
/**
* Sets the [[size]] of the spinner to "medium". This is the default setting.
*/
const SIZE_MEDIUM = 20;
/**
* Sets the [[size]] of the spinner to "big".
*/
const SIZE_BIG = 30;
/**
* Sets the [[color]] of the spinner to "red".
*/
const COLOR_RED = 10;
/**
* Sets the [[color]] of the spinner to "blue".
*/
const COLOR_BLUE = 20;
/**
* Sets the [[color]] of the spinner to "yellow".
*/
const COLOR_YELLOW = 30;
/**
* Sets the [[color]] of the spinner to "green".
*/
const COLOR_GREEN = 40;
/**
* @var array the HTML attributes for the widget container tag.
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail)
* for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array the HTML attributes for the spinner.
*
* If [[flashColors]] is set to "true" these options will be applied to all spinner simultaneously.
*
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail)
* for details on how attributes are being rendered.
*/
public $spinnerOptions = [];
/**
* @var boolean whether to show alternating colors in spinner.
*
* If this is set to "true" the spinner will continously alternate its colors between blue, red, yellow and green.
*
* @see https://materializecss.com/preloader.html
*/
public $flashColors = false;
/**
* @var string the size of the spinner.
*
* The following options are supported:
* - small
* - medium
* - big
*
* Defaults to "medium".
* To set the size, use the corresponding `SIZE_*` constant of this class.
*/
public $size = self::SIZE_MEDIUM;
/**
* @var integer the color of the spinner.
*
* The following options are supported:
* - blue
* - red
* - yellow
* - green
*
* To set the color, use the corresponding `COLOR_*` constant of this class.
* If no color from this range is given, the slider color will be the Materialize default "petrol" color (#26a69a).
*/
public $color;
/**
* @var array the colors alternating when $flashColors equals 'true'
*/
private $colors = [
self::COLOR_BLUE => 'blue',
self::COLOR_RED => 'red',
self::COLOR_YELLOW => 'yellow',
self::COLOR_GREEN => 'green',
];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, ['widget' => 'preloader-wrapper active']);
Html::addCssClass($this->spinnerOptions, ['spinner' => 'spinner-layer']);
switch ($this->size) {
case self::SIZE_SMALL:
Html::addCssClass($this->options, ['spinner-size' => 'small']);
break;
case self::SIZE_BIG:
Html::addCssClass($this->options, ['spinner-size' => 'big']);
break;
}
if ($this->flashColors === false && $this->color) {
$color = ArrayHelper::getValue($this->colors, $this->color);
Html::addCssClass($this->spinnerOptions, ['spinner-color' => "spinner-$color-only"]);
}
}
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
* @uses [[renderSpinner()]]
*/
public function run()
{
$html[] = Html::beginTag('div', $this->options);
if ($this->flashColors !== false) {
foreach ($this->colors as $color) {
Html::addCssClass($this->spinnerOptions, ['color' => 'spinner-' . $color]);
$html[] = Html::beginTag('div', $this->spinnerOptions);
$html[] = $this->renderSpinner();
$html[] = Html::endTag('div');
Html::removeCssClass($this->spinnerOptions, ['color' => 'spinner-' . $color]);
}
} else {
$html[] = Html::beginTag('div', $this->spinnerOptions);
$html[] = $this->renderSpinner();
$html[] = Html::endTag('div');
}
$html[] = Html::endTag('div');
return implode("\n", $html);
}
/**
* Renders a single spinner.
* @return string
*/
protected function renderSpinner()
{
$html = [
'<div class="circle-clipper left">',
'<div class="circle"></div>',
'</div>',
'<div class="gap-patch">',
'<div class="circle"></div>',
'</div>',
'<div class="circle-clipper right">',
'<div class="circle"></div>',
'</div>'
];
return implode("\n", $html);
}
}