/
Slider.php
157 lines (137 loc) · 4.97 KB
/
Slider.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
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-slider
* @version 1.3.1
*/
namespace kartik\slider;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* An extended slider input for Bootstrap 3 based
* on bootstrap-slider plugin.
*
* @see https://github.com/seiyria/bootstrap-slider
* @see http://www.eyecon.ro/bootstrap-slider/
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class Slider extends \kartik\base\InputWidget
{
const TYPE_GREY = '#bababa';
const TYPE_PRIMARY = '#428bca';
const TYPE_INFO = '#5bc0de';
const TYPE_SUCCESS = '#5cb85c';
const TYPE_DANGER = '#d9534f';
const TYPE_WARNING = '#f0ad4e';
/**
* @inherit doc
*/
protected $_pluginName = 'slider';
/**
* @var bool whether another conflicting Slider plugin like JUI slider exists on the
* same page. If set to `true` the plugin will use a different namespace. If you have
* the full jQuery UI assets loaded on the page you should set this to `true`.
*/
public $pluginConflict = false;
/**
* @var string background color for the slider handle
*/
public $handleColor;
/**
* @var string background color for the slider selection
*/
public $sliderColor;
/**
* @var bool whether input is disabled
*/
private $_isDisabled = false;
/**
* Initializes the widget
*/
public function init()
{
parent::init();
$this->_pluginName = $this->pluginConflict ? 'bootstrapSlider' : 'slider';
if (!empty($this->value) || $this->value === 0) {
if (is_array($this->value)) {
throw new InvalidConfigException("Value cannot be passed as an array. If you wish to setup a range slider, pass the two values together as strings separated with a ',' sign.");
}
if (strpos($this->value, ',') > 0) {
$values = explode(',', $this->value);
static::validateValue($values[0]);
static::validateValue($values[1]);
$this->pluginOptions['value'] = [(float)$values[0], (float)$values[1]];
$this->pluginOptions['range'] = true;
} else {
static::validateValue($this->value);
$this->pluginOptions['value'] = (float)$this->value;
}
} else {
// initialize value
$this->pluginOptions['value'] = null;
}
Html::addCssClass($this->options, 'form-control');
// initialize if disabled
$this->_isDisabled = ((!empty($this->options['disabled']) && $this->options['disabled']) ||
(!empty($this->options['readonly']) && $this->options['readonly']));
if ($this->_isDisabled) {
$this->pluginOptions['enabled'] = false;
}
$this->registerAssets();
echo $this->getInput('textInput');
}
/**
* Validates the input value
*
* @param $value
* @throws \yii\base\InvalidConfigException
*/
protected static function validateValue($value)
{
if (!is_numeric($value)) {
throw new InvalidConfigException("Invalid value '{$value}' passed. Only numeric values allowed.");
}
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
SliderAsset::register($view);
// register plugin
$id = "$('#" . $this->options['id'] . "')";
$this->pluginOptions['id'] = $this->options['id'] . '-slider';
$this->registerPlugin($this->_pluginName);
// register CSS styles
$cssStyle = null;
if (!empty($this->handleColor) && !$this->_isDisabled) {
$isTriangle = (!empty($this->pluginOptions['handle']) && $this->pluginOptions['handle'] == 'triangle');
$cssStyle = $this->getCssColor('handle', $this->handleColor, $isTriangle);
}
if (!empty($this->sliderColor) && !$this->_isDisabled) {
$cssStyle .= $this->getCssColor('selection', $this->sliderColor);
}
if ($cssStyle != null && !$this->_isDisabled) {
$view->registerCss($cssStyle);
}
// trigger change event on slider stop, so that client validation
// is triggered for yii active fields
$view->registerJs("{$id}.on('slideStop', function(){{$id}.trigger('change')});");
}
/**
* Gets the css background style for a slider element type
*
* @param string $type the element type ('handle' or 'selection')
* @param string $color the hex color string
* @param bool $isTriangle whether the handle displayed is of triangle shape.
* @return string
*/
protected function getCssColor($type, $color, $isTriangle = false)
{
$feature = $isTriangle ? 'border-bottom-color' : 'background';
return "#" . $this->pluginOptions['id'] . " .slider-{$type}{{$feature}:{$color}}";
}
}