forked from yii2mod/yii2-ion-slider
/
IonSlider.php
89 lines (79 loc) · 1.99 KB
/
IonSlider.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
<?php
namespace yii2mod\slider;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* Class IonSlider
* @package yii2mod\slider
*/
class IonSlider extends InputWidget
{
/**
* @var array the HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array plugin options
*/
public $pluginOptions = [];
/**
* Slider type - single, double
* @var string
*/
public $type = 'single';
/**
* Initializes the object.
* This method is invoked at the end of the constructor after the object is initialized with the
* given configuration.
*/
public function init()
{
parent::init();
}
/**
* Render range slider
* @return string|void
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
$this->registerAssets();
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
IonSliderAsset::register($view);
$js = '$("#' . $this->getInputId() . '").ionRangeSlider(' . $this->getPluginOptions() . ');';
$view->registerJs($js, $view::POS_END);
}
/**
* Return plugin options in json format
* @return string
*/
public function getPluginOptions()
{
if (!isset($this->pluginOptions['type'])) {
$this->pluginOptions['type'] = $this->type;
}
return Json::encode($this->pluginOptions);
}
/**
* Return select id
* @return mixed
* @throws InvalidConfigException
*/
public function getInputId()
{
return $this->options['id'];
}
}