1+ <?php
2+
3+ namespace yii2mod \slider ;
4+
5+ use yii \base \InvalidConfigException ;
6+ use yii \helpers \Html ;
7+ use yii \helpers \Json ;
8+ use yii \widgets \InputWidget ;
9+
10+ /**
11+ * Class IonSlider
12+ * @package yii2mod\slider
13+ */
14+ class IonSlider extends InputWidget
15+ {
16+ /**
17+ * @var array the HTML attributes for the input tag.
18+ * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
19+ */
20+ public $ options = [];
21+
22+ /**
23+ * @var array plugin options
24+ */
25+ public $ pluginOptions = [];
26+
27+ /**
28+ * Slider type - single, double
29+ * @var string
30+ */
31+ public $ type = 'single ' ;
32+
33+ /**
34+ * Initializes the object.
35+ * This method is invoked at the end of the constructor after the object is initialized with the
36+ * given configuration.
37+ */
38+ public function init ()
39+ {
40+ parent ::init ();
41+ }
42+
43+ /**
44+ * Render range slider
45+ * @return string|void
46+ */
47+ public function run ()
48+ {
49+ if ($ this ->hasModel ()) {
50+ echo Html::activeTextInput ($ this ->model , $ this ->attribute , $ this ->options );
51+ }
52+ $ this ->registerAssets ();
53+ }
54+
55+ /**
56+ * Register client assets
57+ */
58+ protected function registerAssets ()
59+ {
60+ $ view = $ this ->getView ();
61+ IonSliderAsset::register ($ view );
62+ $ js = '$("# ' . $ this ->getInputId () . '").ionRangeSlider( ' . $ this ->getPluginOptions () . '); ' ;
63+ $ view ->registerJs ($ js , $ view ::POS_END );
64+ }
65+
66+ /**
67+ * Return plugin options in json format
68+ * @return string
69+ */
70+ public function getPluginOptions ()
71+ {
72+ $ this ->pluginOptions ['type ' ] = $ this ->type ;
73+
74+ return Json::encode ($ this ->pluginOptions );
75+ }
76+
77+ /**
78+ * Return select id
79+ * @return mixed
80+ * @throws InvalidConfigException
81+ */
82+ public function getInputId ()
83+ {
84+ return $ this ->options ['id ' ];
85+ }
86+ }
0 commit comments