-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathRangeInput.php
273 lines (240 loc) · 9.03 KB
/
RangeInput.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?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\form;
use macgyer\yii2materializecss\assets\NoUiSliderAsset;
use macgyer\yii2materializecss\lib\BaseInputWidget;
use macgyer\yii2materializecss\lib\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* RangeInput renders a slider input element. You can specify to use a simple HTML5 range `<input>` or make use of the versatile
* noUiSlider JS plugin.
*
* All noUiSlider settings provided as member variables are needed for out-of-the-box functionality. See the noUiSlider
* docs for more options and events.
*
* <strong>Please note:</strong> This widget provides functionality for sliders with one handle only. If you want to use
* noUiSlider with multiple handles or more complex functionality you need to create your own class and inherit from this class.
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
* @subpackage form
*
* @see https://materializecss.com/range.html
* @see https://refreshless.com/nouislider/
*/
class RangeInput extends BaseInputWidget
{
/**
* @var array the HTML attributes for the widget container tag. The following special options are recognized:
*
* - tag: string, defaults to "div", the name of the 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 slider element. The following special options are recognized:
*
* - tag: string, defaults to "div", the name of the slider element's 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 $sliderOptions = [];
/**
* @var boolean whether to use noUiSlider.
* @see https://materializecss.github.io/materialize/range.html
*/
public $useNoUiSlider = true;
/**
* @var boolean whether to render an hidden input field to hold the slider value.
* This options only takes effect, when [[useNoUiSlider]] has been set to `true`.
*/
public $renderHiddenInput = true;
/**
* @var array the HTML attributes for the actual input element.
*
* If you have set [[useNoUiSlider]] and [[renderHiddenInput]] both to `true`, the input element will be used as a hidden
* input field holding the value issued by the slider and these options applied.
*
* When setting [[useNoUiSlider]] to `false`, the input options will be applied to the `<input type="range">` element.
*
* @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 $inputOptions = [];
/**
* @var integer the start value of the slider.
* This options only takes effect, when [[useNoUiSlider]] has been set to `true`.
* @see https://refreshless.com/nouislider/slider-options/#section-start
*/
public $start = 0;
/**
* @var array the min and max values of the slider.
* This options only takes effect, when [[useNoUiSlider]] has been set to `true`.
*/
public $range = [
'min' => 0,
'max' => 100
];
/**
* @var integer the stepping value when moving the slider handle.
* This options only takes effect, when [[useNoUiSlider]] has been set to `true`.
* @see https://refreshless.com/nouislider/slider-options/#section-step
*/
public $step = 1;
/**
* @var boolean whether to use a vertical slider.
* This options only takes effect, when [[useNoUiSlider]] has been set to `true`.
* @see https://refreshless.com/nouislider/slider-options/#section-orientation
*/
public $vertical = false;
/**
* Initialize the widget.
*/
public function init()
{
parent::init();
if ($this->useNoUiSlider) {
if (!ArrayHelper::keyExists('start', $this->clientOptions)) {
$this->clientOptions['start'] = $this->start;
}
if (!ArrayHelper::keyExists('range', $this->clientOptions)) {
$this->clientOptions['range'] = $this->range;
}
if (!ArrayHelper::keyExists('step', $this->clientOptions)) {
$this->clientOptions['step'] = $this->step;
}
if ($this->vertical) {
$this->clientOptions['orientation'] = 'vertical';
}
if (!isset($this->sliderOptions['id'])) {
$this->sliderOptions['id'] = "{$this->getId()}-slider";
}
$this->addUpdateEvent();
$this->registerClientScripts();
}
}
/**
* Adds default noUiSlider update event to [[clientEvents]].
* @see https://refreshless.com/nouislider/events-callbacks/#section-update
*/
protected function addUpdateEvent()
{
$containerId = $this->options['id'];
$sliderId = $this->sliderOptions['id'];
$this->inputOptions['data-slider-target'] = $sliderId;
if (!isset($this->clientEvents['update'])) {
$this->clientEvents['update'] = new JsExpression(<<<JS
function (values, handle) {
let value = values[handle];
document.querySelector('#$containerId [data-slider-target="{$sliderId}"]').value = value;
}
JS
);
}
}
/**
* Execute the widget.
*
* @return string the rendered markup.
*/
public function run()
{
$tag = ArrayHelper::remove($this->options, 'tag', 'div');
$html[] = Html::beginTag($tag, $this->options);
if (!$this->useNoUiSlider) {
$html[] = $this->renderHtml5RangeInput();
} else {
$html[] = $this->renderNoUiSlider();
}
$html[] = Html::endTag($tag);
return implode("\n", $html);
}
/**
* Renders a standard HTML5 range input field.
* This is only being effective when [[$useNoUiSlider]] is set to 'false'.
*
* @return string the field markup.
*/
protected function renderHtml5RangeInput()
{
$html[] = Html::beginTag('div', ['class' => 'range-field']);
if ($this->hasModel()) {
$html[] = Html::activeInput('range', $this->model, $this->attribute, $this->inputOptions);
} else {
$html[] = Html::input('range', $this->name, $this->value, $this->inputOptions);
}
$html[] = Html::endTag('div');
return implode("\n", $html);
}
/**
* Renders the markup for the noUiSlider plugin.
* This is only being effective when [[$useNoUiSlider]] is set to 'true'.
*
* @return string the field markup.
*/
protected function renderNoUiSlider()
{
$html[] = Html::tag('div', '', $this->sliderOptions);
if ($this->renderHiddenInput) {
$html[] = $this->renderHiddenInput();
}
return implode("\n", $html);
}
/**
* Renders a hidden input field which holds the value issued by the slider.
* This is only being effective when [[$useNoUiSlider]] is set to 'true'.
*
* @return string the input field markup.
*/
protected function renderHiddenInput()
{
if ($this->hasModel()) {
return Html::activeHiddenInput($this->model, $this->attribute, $this->inputOptions);
} else {
return Html::hiddenInput($this->name, $this->value, $this->inputOptions);
}
}
/**
* Registers all scripts needed by the underlying JS plugin.
*/
protected function registerClientScripts()
{
$view = $this->getView();
NoUiSliderAsset::register($view);
$id = $this->sliderOptions['id'];
$varName = $this->getUniqueId('slider_');
if ($this->clientOptions !== false) {
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$view->registerJs(<<<JS
let $varName = document.getElementById('$id');
noUiSlider.create($varName, {$options});
JS
);
}
$this->registerClientEvents($varName);
}
/**
* Registers the events of the slider.
*
* @param string $sliderVarName the JS variable name of the slider element.
*/
protected function registerClientEvents($sliderVarName = null)
{
if (!empty($this->clientEvents)) {
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "$sliderVarName.noUiSlider.on('$event', $handler);";
}
$this->getView()->registerJs(implode("\n", $js));
}
}
}