22
33namespace yii2mod \slider ;
44
5- use yii \base \ InvalidConfigException ;
5+ use yii \helpers \ ArrayHelper ;
66use yii \helpers \Html ;
77use yii \helpers \Json ;
88use yii \widgets \InputWidget ;
1414class IonSlider extends InputWidget
1515{
1616 /**
17- * @var array the HTML attributes for the input tag.
18- * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
17+ * Single type of the slider
1918 */
20- public $ options = [] ;
19+ const TYPE_SINGLE = ' single ' ;
2120
2221 /**
23- * @var array plugin options
22+ * Double type of the slider
2423 */
25- public $ pluginOptions = [] ;
24+ const TYPE_DOUBLE = ' double ' ;
2625
2726 /**
28- * Slider type - single, double
29- * @var string
27+ * @var string the type of the slider. Defaults to `TYPE_SINGLE`
3028 */
31- public $ type = ' single ' ;
29+ public $ type = self :: TYPE_SINGLE ;
3230
3331 /**
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.
32+ * @var array plugin options
3733 */
38- public function init ()
39- {
40- parent ::init ();
41- }
34+ public $ pluginOptions = [];
4235
4336 /**
4437 * Render range slider
38+ *
4539 * @return string|void
4640 */
4741 public function run ()
@@ -51,6 +45,7 @@ public function run()
5145 } else {
5246 echo Html::textInput ($ this ->name , $ this ->value , $ this ->options );
5347 }
48+
5449 $ this ->registerAssets ();
5550 }
5651
@@ -61,29 +56,19 @@ protected function registerAssets()
6156 {
6257 $ view = $ this ->getView ();
6358 IonSliderAsset::register ($ view );
64- $ js = '$("# ' . $ this ->getInputId () . '").ionRangeSlider( ' . $ this ->getPluginOptions () . '); ' ;
59+ $ js = '$("# ' . $ this ->options [ ' id ' ] . '").ionRangeSlider( ' . $ this ->getPluginOptions () . '); ' ;
6560 $ view ->registerJs ($ js , $ view ::POS_END );
6661 }
6762
6863 /**
6964 * Return plugin options in json format
65+ *
7066 * @return string
7167 */
7268 public function getPluginOptions ()
7369 {
74- if (!isset ($ this ->pluginOptions ['type ' ])) {
75- $ this ->pluginOptions ['type ' ] = $ this ->type ;
76- }
77- return Json::encode ($ this ->pluginOptions );
78- }
70+ $ this ->pluginOptions ['type ' ] = ArrayHelper::getValue ($ this ->pluginOptions , 'type ' , $ this ->type );
7971
80- /**
81- * Return select id
82- * @return mixed
83- * @throws InvalidConfigException
84- */
85- public function getInputId ()
86- {
87- return $ this ->options ['id ' ];
72+ return Json::encode ($ this ->pluginOptions );
8873 }
89- }
74+ }
0 commit comments