]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/field/Slider.js
2 * The slider is a way to allow the user to select a value from a given numerical range. You might use it for choosing
3 * a percentage, combine two of them to get min and max values, or use three of them to specify the hex values for a
4 * color. Each slider contains a single 'thumb' that can be dragged along the slider's length to change the value.
5 * Sliders are equally useful inside {@link Ext.form.Panel forms} and standalone. Here's how to quickly create a
6 * slider in form, in this case enabling a user to choose a percentage:
9 * Ext.create('Ext.form.Panel', {
13 * xtype: 'sliderfield',
14 * label: 'Percentage',
22 * In this case we set a starting value of 50%, and defined the min and max values to be 0 and 100 respectively, giving
23 * us a percentage slider. Because this is such a common use case, the defaults for {@link #minValue} and
24 * {@link #maxValue} are already set to 0 and 100 so in the example above they could be removed.
26 * It's often useful to render sliders outside the context of a form panel too. In this example we create a slider that
27 * allows a user to choose the waist measurement of a pair of jeans. Let's say the online store we're making this for
28 * sells jeans with waist sizes from 24 inches to 60 inches in 2 inch increments - here's how we might achieve that:
31 * Ext.create('Ext.form.Panel', {
35 * xtype: 'sliderfield',
36 * label: 'Waist Measurement',
45 * Now that we've got our slider, we can ask it what value it currently has and listen to events that it fires. For
46 * example, if we wanted our app to show different images for different sizes, we can listen to the {@link #change}
47 * event to be informed whenever the slider is moved:
49 * slider.on('change', function(field, newValue) {
50 * if (newValue[0] > 40) {
51 * imgComponent.setSrc('large.png');
53 * imgComponent.setSrc('small.png');
57 * Here we listened to the {@link #change} event on the slider and updated the background image of an
58 * {@link Ext.Img image component} based on what size the user selected. Of course, you can use any logic inside your
61 * For more information regarding forms and fields, please review [Using Forms in Sencha Touch Guide](../../../components/forms.html)
63 Ext
.define('Ext.field.Slider', {
64 extend
: 'Ext.field.Field',
65 xtype
: 'sliderfield',
66 requires
: ['Ext.slider.Slider'],
67 alternateClassName
: 'Ext.form.Slider',
71 * Fires when an option selection has changed.
72 * @param {Ext.field.Slider} me
73 * @param {Ext.slider.Slider} sl Slider Component.
74 * @param {Ext.slider.Thumb} thumb
75 * @param {Number} newValue The new value of this thumb.
76 * @param {Number} oldValue The old value of this thumb.
81 * Fires when the slider thumb starts a drag operation.
82 * @param {Ext.field.Slider} this
83 * @param {Ext.slider.Slider} sl Slider Component.
84 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
85 * @param {Array} value The start value.
86 * @param {Ext.EventObject} e
91 * Fires when the slider thumb starts a drag operation.
92 * @param {Ext.field.Slider} this
93 * @param {Ext.slider.Slider} sl Slider Component.
94 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
95 * @param {Ext.EventObject} e
100 * Fires when the slider thumb ends a drag operation.
101 * @param {Ext.field.Slider} this
102 * @param {Ext.slider.Slider} sl Slider Component.
103 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
104 * @param {Array} value The end value.
105 * @param {Ext.EventObject} e
113 cls
: Ext
.baseCSSPrefix
+ 'slider-field',
122 * Will make this field read only, meaning it cannot be changed with used interaction.
123 * @cfg {Boolean} readOnly
132 * @inheritdoc Ext.slider.Slider#increment
133 * @cfg {Number} increment
139 * @inheritdoc Ext.slider.Slider#value
140 * @cfg {Number/Number[]} value
146 * @inheritdoc Ext.slider.Slider#minValue
147 * @cfg {Number} minValue
153 * @inheritdoc Ext.slider.Slider#maxValue
154 * @cfg {Number} maxValue
161 * @inheritdoc Ext.slider.Slider#values
162 * @cfg {Number/Number[]} values
165 constructor: function(config
) {
166 config
= config
|| {};
168 if (config
.hasOwnProperty('values')) {
169 config
.value
= config
.values
;
172 this.callParent([config
]);
173 this.updateMultipleState();
177 initialize: function() {
180 this.getComponent().on({
183 change
: 'onSliderChange',
184 dragstart
: 'onSliderDragStart',
185 drag
: 'onSliderDrag',
186 dragend
: 'onSliderDragEnd'
191 applyComponent: function(config
) {
192 return Ext
.factory(config
, Ext
.slider
.Slider
);
196 updateComponent: function(component
) {
197 this.callSuper(arguments
);
199 component
.setMinValue(this.getMinValue());
200 component
.setMaxValue(this.getMaxValue());
203 onSliderChange: function() {
204 this.fireEvent
.apply(this, [].concat('change', this, Array
.prototype.slice
.call(arguments
)));
207 onSliderDragStart: function() {
208 this.fireEvent
.apply(this, [].concat('dragstart', this, Array
.prototype.slice
.call(arguments
)));
211 onSliderDrag: function() {
212 this.fireEvent
.apply(this, [].concat('drag', this, Array
.prototype.slice
.call(arguments
)));
215 onSliderDragEnd: function() {
216 this.fireEvent
.apply(this, [].concat('dragend', this, Array
.prototype.slice
.call(arguments
)));
220 * Convenience method. Calls {@link #setValue}.
221 * @param {Object} value
223 setValues: function(value
) {
224 this.setValue(value
);
225 this.updateMultipleState();
229 * Convenience method. Calls {@link #getValue}
232 getValues: function() {
233 return this.getValue();
237 var config
= this.config
,
238 initialValue
= (this.config
.hasOwnProperty('values')) ? config
.values
: config
.value
;
240 this.setValue(initialValue
);
243 doSetDisabled: function(disabled
) {
244 this.callParent(arguments
);
246 this.getComponent().setDisabled(disabled
);
249 updateReadOnly: function(newValue
) {
250 this.getComponent().setReadOnly(newValue
);
253 isDirty : function () {
254 if (this.getDisabled()) {
258 return this.getValue() !== this.originalValue
;
261 updateMultipleState: function() {
262 var value
= this.getValue();
263 if (value
&& value
.length
> 1) {
264 this.addCls(Ext
.baseCSSPrefix
+ 'slider-multiple');