]>
git.proxmox.com Git - extjs.git/blob - extjs/modern/modern/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 Ext
.define('Ext.field.Slider', {
62 extend
: 'Ext.field.Field',
63 xtype
: 'sliderfield',
64 requires
: ['Ext.slider.Slider'],
65 alternateClassName
: 'Ext.form.Slider',
69 * Fires when the value changes.
70 * @param {Ext.field.Slider} me
71 * @param {Number[]} newValue The new value.
72 * @param {Number[]} oldValue The old value.
77 * Fires when a thumb value changes via drag.
78 * @param {Ext.field.Slider} me
79 * @param {Ext.slider.Slider} sl Slider Component.
80 * @param {Ext.slider.Thumb} thumb
81 * @param {Number[]} newValue The new value of this thumb.
82 * @param {Number[]} oldValue The old value of this thumb.
87 * Fires when the slider thumb starts a drag operation.
88 * @param {Ext.field.Slider} this
89 * @param {Ext.slider.Slider} sl Slider Component.
90 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
91 * @param {Array} value The start value.
92 * @param {Ext.event.Event} e
97 * Fires when the slider thumb starts a drag operation.
98 * @param {Ext.field.Slider} this
99 * @param {Ext.slider.Slider} sl Slider Component.
100 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
101 * @param {Ext.event.Event} e
106 * Fires when the slider thumb ends a drag operation.
107 * @param {Ext.field.Slider} this
108 * @param {Ext.slider.Slider} sl Slider Component.
109 * @param {Ext.slider.Thumb} thumb The thumb being dragged.
110 * @param {Array} value The end value.
111 * @param {Ext.event.Event} e
119 cls
: Ext
.baseCSSPrefix
+ 'slider-field',
122 * @cfg {Boolean} liveUpdate
123 * `true` to fire change events while the slider is dragging. `false` will
124 * only fire a change once the drag is complete.
135 * Will make this field read only, meaning it cannot be changed with used interaction.
136 * @cfg {Boolean} readOnly
142 * @inheritdoc Ext.slider.Slider#value
143 * @cfg {Number/Number[]} value
152 * @inheritdoc Ext.slider.Slider#increment
153 * @cfg {Number} increment
159 * @inheritdoc Ext.slider.Slider#minValue
160 * @cfg {Number} minValue
166 * @inheritdoc Ext.slider.Slider#maxValue
167 * @cfg {Number} maxValue
173 defaultBindProperty
: 'values',
180 * @inheritdoc Ext.slider.Slider#values
181 * @cfg {Number/Number[]} values
184 constructor: function(config
) {
185 config
= config
|| {};
187 if (config
.hasOwnProperty('values')) {
188 config
.value
= config
.values
;
191 this.callParent([config
]);
192 this.updateMultipleState();
198 initialize: function() {
201 this.getComponent().on({
204 change
: 'onSliderChange',
205 dragstart
: 'onSliderDragStart',
206 drag
: 'onSliderDrag',
207 dragend
: 'onSliderDragEnd'
214 applyComponent: function(config
) {
215 return Ext
.factory(config
, Ext
.slider
.Slider
);
221 updateComponent: function(component
, oldComponent
) {
222 this.callParent([component
, oldComponent
]);
224 component
.setMinValue(this.getMinValue());
225 component
.setMaxValue(this.getMaxValue());
228 applyValue: function(value
) {
230 // If we are currently dragging, don't allow the binding
231 // to push a value over the top of what the user is doing.
232 if (this.dragging
&& this.isSyncing('value')) {
238 updateValue: function(value
, oldValue
) {
242 me
.setComponentValue(value
);
244 if (me
.initialized
) {
245 me
.fireEvent('change', me
, value
, oldValue
);
249 setComponentValue: function(value
) {
250 this.getComponent().setValue(value
);
253 onSliderChange: function(slider
, thumb
, newValue
, oldValue
) {
254 this.setValue(slider
.getValue());
255 this.fireEvent('dragchange', this, slider
, thumb
, newValue
, oldValue
);
258 onSliderDragStart: function(slider
, thumb
, startValue
, e
) {
259 this.dragging
= true;
260 this.fireEvent('dragstart', this, slider
, thumb
, startValue
, e
);
263 onSliderDrag: function(slider
, thumb
, value
, e
) {
265 if (me
.getLiveUpdate()) {
266 me
.setValue(slider
.getValue());
268 me
.fireEvent('drag', me
, slider
, thumb
, value
, e
);
271 onSliderDragEnd: function(slider
, thumb
, startValue
, e
) {
272 this.dragging
= false;
273 this.fireEvent('dragend', this, slider
, thumb
, startValue
, e
);
277 * Convenience method. Calls {@link #setValue}.
278 * @param {Object} value
280 setValues: function(value
) {
281 this.setValue(value
);
282 this.updateMultipleState();
286 * Convenience method. Calls {@link #getValue}
289 getValues: function() {
290 return this.getValue();
294 var config
= this.config
,
295 initialValue
= (this.config
.hasOwnProperty('values')) ? config
.values
: config
.value
;
297 this.setValue(initialValue
);
300 updateDisabled: function(disabled
) {
301 this.callParent(arguments
);
303 this.getComponent().setDisabled(disabled
);
306 updateReadOnly: function(newValue
) {
307 this.getComponent().setReadOnly(newValue
);
310 isDirty : function () {
311 if (this.getDisabled()) {
315 return this.getValue() !== this.originalValue
;
318 updateMultipleState: function() {
319 var value
= this.getValue();
320 if (value
&& value
.length
> 1) {
321 this.addCls(Ext
.baseCSSPrefix
+ 'slider-multiple');