1: <?php
2: /**
3: * CJuiSliderInput class file.
4: *
5: * @author Sebastian Thierer <sebathi@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: Yii::import('zii.widgets.jui.CJuiInputWidget');
12:
13: /**
14: * CJuiSliderInput displays a slider. It can be used in forms and post its value.
15: *
16: * CJuiSlider encapsulates the {@link http://jqueryui.com/slider/ JUI
17: * slider} plugin.
18: *
19: * To use this widget, you may insert the following code in a view:
20: * <pre>
21: * $this->widget('zii.widgets.jui.CJuiSliderInput',array(
22: * 'name'=>'rate',
23: * 'value'=>37,
24: * // additional javascript options for the slider plugin
25: * 'options'=>array(
26: * 'min'=>10,
27: * 'max'=>50,
28: * ),
29: * 'htmlOptions'=>array(
30: * 'style'=>'height:20px;',
31: * ),
32: * ));
33: * </pre>
34: *
35: * The widget can also be used in range mode which uses 2 sliders to set a range.
36: * In this mode, {@link attribute} and {@link maxAttribute} will define the attribute
37: * names for the minimum and maximum range values, respectively. For example:
38: *
39: * <pre>
40: * $this->widget('zii.widgets.jui.CJuiSliderInput',array(
41: * 'model'=>$model,
42: * 'attribute'=>'timeMin',
43: * 'maxAttribute'=>'timeMax',
44: * // additional javascript options for the slider plugin
45: * 'options'=>array(
46: * 'range'=>true,
47: * 'min'=>0,
48: * 'max'=>24,
49: * ),
50: * ));
51: * </pre>
52: *
53: * If you need to use the slider event, please change the event value for 'stop' or 'change'.
54: *
55: * By configuring the {@link options} property, you may specify the options
56: * that need to be passed to the JUI slider plugin. Please refer to
57: * the {@link http://api.jqueryui.com/slider/ JUI Slider API} documentation
58: * for possible options (name-value pairs) and
59: * {@link http://jqueryui.com/slider/ JUI Slider page} for general
60: * description and demo.
61: *
62: * @author Sebastian Thierer <sebathi@gmail.com>
63: * @package zii.widgets.jui
64: * @since 1.1
65: */
66: class CJuiSliderInput extends CJuiInputWidget
67: {
68: /**
69: * @var string the name of the container element that contains the slider. Defaults to 'div'.
70: */
71: public $tagName='div';
72: /**
73: * @var integer determines the value of the slider, if there's only one handle. If there is more than one handle,
74: * determines the value of the first handle.
75: */
76: public $value;
77: /**
78: * @var string the name of the event where the input will be attached to the slider. It
79: * can be 'slide', 'stop' or 'change'. If you want to use 'slide' event change $event property to 'change'.
80: */
81: public $event='slide';
82: /**
83: * @var string name of attribute for max value if slider is used in range mode.
84: */
85: public $maxAttribute;
86: /**
87: * @var string the input name to be used for max value attribute when using slider in range mode.
88: * This must be set in case {@link model} isn't used.
89: * @since 1.1.14
90: */
91: public $maxName;
92: /**
93: * @var integer determines the max value of the slider, if there's two handles (range mode). Ignored if there's
94: * only one handle.
95: * @since 1.1.14
96: */
97: public $maxValue;
98: /**
99: * @var string the suffix to be appended to the ID of the max value input element
100: * when slider used in range mode.
101: * @since 1.1.14
102: */
103: public $maxIdSuffix='_end';
104:
105: /**
106: * Run this widget.
107: * This method registers necessary javascript and renders the needed HTML code.
108: */
109: public function run()
110: {
111: list($name,$id)=$this->resolveNameID();
112: if(isset($this->htmlOptions['id']))
113: $id=$this->htmlOptions['id'];
114: else
115: $this->htmlOptions['id']=$id;
116:
117: $isRange=isset($this->options['range']) && $this->options['range'] &&
118: $this->options['range']!=='max' && $this->options['range']!=='min';
119:
120: if($this->hasModel())
121: {
122: $attribute=$this->attribute;
123: if($isRange)
124: {
125: $options=$this->htmlOptions;
126: echo CHtml::activeHiddenField($this->model,$this->attribute,$options);
127: $options['id'].=$this->maxIdSuffix;
128: echo CHtml::activeHiddenField($this->model,$this->maxAttribute,$options);
129: $maxAttribute=$this->maxAttribute;
130: $this->options['values']=array($this->model->$attribute,$this->model->$maxAttribute);
131: }
132: else
133: {
134: echo CHtml::activeHiddenField($this->model,$this->attribute,$this->htmlOptions);
135: $this->options['value']=$this->model->$attribute;
136: }
137: }
138: else
139: {
140: if($isRange)
141: {
142: list($maxName,$maxId)=$this->resolveNameID('maxName','maxAttribute');
143: $options=$this->htmlOptions;
144: echo CHtml::hiddenField($name,$this->value,$options);
145: $options['id'].=$this->maxIdSuffix;
146: echo CHtml::hiddenField($maxName,$this->maxValue,$options);
147: $this->options['values']=array($this->value,$this->maxValue);
148: }
149: else
150: {
151: echo CHtml::hiddenField($name,$this->value,$this->htmlOptions);
152: if($this->value!==null)
153: $this->options['value']=$this->value;
154: }
155: }
156:
157: $idHidden=$this->htmlOptions['id'];
158: $this->htmlOptions['id']=$idHidden.'_slider';
159: echo CHtml::tag($this->tagName,$this->htmlOptions,'');
160:
161: $this->options[$this->event]=$isRange
162: ? new CJavaScriptExpression("function(e,ui){ v=ui.values; jQuery('#{$idHidden}').val(v[0]); jQuery('#{$idHidden}{$this->maxIdSuffix}').val(v[1]); }")
163: : new CJavaScriptExpression("function(event, ui) { jQuery('#{$idHidden}').val(ui.value); }");
164:
165: $options=CJavaScript::encode($this->options);
166: Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}_slider').slider($options);");
167: }
168: }
169: