3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
9 * @extends Ext.BoxComponent
\r
10 * Slider which supports vertical or horizontal orientation, keyboard adjustments,
\r
11 * configurable snapping, axis clicking and animation. Can be added as an item to
\r
12 * any container. Example usage:
\r
15 renderTo: Ext.getBody(),
\r
24 Ext.Slider = Ext.extend(Ext.BoxComponent, {
\r
26 * @cfg {Number} value The value to initialize the slider with. Defaults to minValue.
\r
29 * @cfg {Boolean} vertical Orient the Slider vertically rather than horizontally, defaults to false.
\r
33 * @cfg {Number} minValue The minimum value for the Slider. Defaults to 0.
\r
37 * @cfg {Number} maxValue The maximum value for the Slider. Defaults to 100.
\r
41 * @cfg {Number/Boolean} decimalPrecision.
\r
42 * <p>The number of decimal places to which to round the Slider's value. Defaults to 0.</p>
\r
43 * <p>To disable rounding, configure as <tt><b>false</b></tt>.</p>
\r
45 decimalPrecision: 0,
\r
47 * @cfg {Number} keyIncrement How many units to change the Slider when adjusting with keyboard navigation. Defaults to 1. If the increment config is larger, it will be used instead.
\r
51 * @cfg {Number} increment How many units to change the slider when adjusting by drag and drop. Use this option to enable 'snapping'.
\r
57 * @cfg {Boolean} clickToChange Determines whether or not clicking on the Slider axis will change the slider. Defaults to true
\r
59 clickToChange : true,
\r
61 * @cfg {Boolean} animate Turn on or off animation. Defaults to true
\r
66 * True while the thumb is in a drag operation
\r
72 initComponent : function(){
\r
73 if(!Ext.isDefined(this.value)){
\r
74 this.value = this.minValue;
\r
76 Ext.Slider.superclass.initComponent.call(this);
\r
77 this.keyIncrement = Math.max(this.increment, this.keyIncrement);
\r
80 * @event beforechange
\r
81 * Fires before the slider value is changed. By returning false from an event handler,
\r
82 * you can cancel the event and prevent the slider from changing.
\r
83 * @param {Ext.Slider} slider The slider
\r
84 * @param {Number} newValue The new value which the slider is being changed to.
\r
85 * @param {Number} oldValue The old value which the slider was previously.
\r
90 * Fires when the slider value is changed.
\r
91 * @param {Ext.Slider} slider The slider
\r
92 * @param {Number} newValue The new value which the slider has been changed to.
\r
96 * @event changecomplete
\r
97 * Fires when the slider value is changed by the user and any drag operations have completed.
\r
98 * @param {Ext.Slider} slider The slider
\r
99 * @param {Number} newValue The new value which the slider has been changed to.
\r
104 * Fires after a drag operation has started.
\r
105 * @param {Ext.Slider} slider The slider
\r
106 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
111 * Fires continuously during the drag operation while the mouse is moving.
\r
112 * @param {Ext.Slider} slider The slider
\r
113 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
118 * Fires after the drag operation has completed.
\r
119 * @param {Ext.Slider} slider The slider
\r
120 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
126 Ext.apply(this, Ext.Slider.Vertical);
\r
130 // private override
\r
131 onRender : function(){
\r
133 cls: 'x-slider ' + (this.vertical ? 'x-slider-vert' : 'x-slider-horz'),
\r
134 cn:{cls:'x-slider-end',cn:{cls:'x-slider-inner',cn:[{cls:'x-slider-thumb'},{tag:'a', cls:'x-slider-focus', href:"#", tabIndex: '-1', hidefocus:'on'}]}}
\r
136 Ext.Slider.superclass.onRender.apply(this, arguments);
\r
137 this.endEl = this.el.first();
\r
138 this.innerEl = this.endEl.first();
\r
139 this.thumb = this.innerEl.first();
\r
140 this.halfThumb = (this.vertical ? this.thumb.getHeight() : this.thumb.getWidth())/2;
\r
141 this.focusEl = this.thumb.next();
\r
145 // private override
\r
146 initEvents : function(){
\r
147 this.thumb.addClassOnOver('x-slider-thumb-over');
\r
148 this.mon(this.el, {
\r
150 mousedown: this.onMouseDown,
\r
151 keydown: this.onKeyDown
\r
154 this.focusEl.swallowEvent("click", true);
\r
156 this.tracker = new Ext.dd.DragTracker({
\r
157 onBeforeStart: this.onBeforeDragStart.createDelegate(this),
\r
158 onStart: this.onDragStart.createDelegate(this),
\r
159 onDrag: this.onDrag.createDelegate(this),
\r
160 onEnd: this.onDragEnd.createDelegate(this),
\r
164 this.tracker.initEl(this.thumb);
\r
167 // private override
\r
168 onMouseDown : function(e){
\r
172 if(this.clickToChange && e.target != this.thumb.dom){
\r
173 var local = this.innerEl.translatePoints(e.getXY());
\r
174 this.onClickChange(local);
\r
180 onClickChange : function(local){
\r
181 if(local.top > this.clickRange[0] && local.top < this.clickRange[1]){
\r
182 this.setValue(Ext.util.Format.round(this.reverseValue(local.left), this.decimalPrecision), undefined, true);
\r
187 onKeyDown : function(e){
\r
188 if(this.disabled){e.preventDefault();return;}
\r
189 var k = e.getKey();
\r
195 this.setValue(this.maxValue, undefined, true);
\r
197 this.setValue(this.value+this.keyIncrement, undefined, true);
\r
204 this.setValue(this.minValue, undefined, true);
\r
206 this.setValue(this.value-this.keyIncrement, undefined, true);
\r
210 e.preventDefault();
\r
215 doSnap : function(value){
\r
216 if(!(this.increment && value)){
\r
219 var newValue = value,
\r
220 inc = this.increment,
\r
226 }else if(m * 2 < -inc){
\r
230 return newValue.constrain(this.minValue, this.maxValue);
\r
234 afterRender : function(){
\r
235 Ext.Slider.superclass.afterRender.apply(this, arguments);
\r
236 if(this.value !== undefined){
\r
237 var v = this.normalizeValue(this.value);
\r
238 if(v !== this.value){
\r
240 this.setValue(v, false);
\r
242 this.moveThumb(this.translateValue(v), false);
\r
248 getRatio : function(){
\r
249 var w = this.innerEl.getWidth(),
\r
250 v = this.maxValue - this.minValue;
\r
251 return v == 0 ? w : (w/v);
\r
255 normalizeValue : function(v){
\r
256 v = this.doSnap(v);
\r
257 v = Ext.util.Format.round(v, this.decimalPrecision);
\r
258 v = v.constrain(this.minValue, this.maxValue);
\r
263 * Sets the minimum value for the slider instance. If the current value is less than the
\r
264 * minimum value, the current value will be changed.
\r
265 * @param {Number} val The new minimum value
\r
267 setMinValue : function(val){
\r
268 this.minValue = val;
\r
270 if(this.value < val){
\r
271 this.setValue(val);
\r
276 * Sets the maximum value for the slider instance. If the current value is more than the
\r
277 * maximum value, the current value will be changed.
\r
278 * @param {Number} val The new maximum value
\r
280 setMaxValue : function(val){
\r
281 this.maxValue = val;
\r
283 if(this.value > val){
\r
284 this.setValue(val);
\r
289 * Programmatically sets the value of the Slider. Ensures that the value is constrained within
\r
290 * the minValue and maxValue.
\r
291 * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
\r
292 * @param {Boolean} animate Turn on or off animation, defaults to true
\r
294 setValue : function(v, animate, changeComplete){
\r
295 v = this.normalizeValue(v);
\r
296 if(v !== this.value && this.fireEvent('beforechange', this, v, this.value) !== false){
\r
298 this.moveThumb(this.translateValue(v), animate !== false);
\r
299 this.fireEvent('change', this, v);
\r
300 if(changeComplete){
\r
301 this.fireEvent('changecomplete', this, v);
\r
307 translateValue : function(v){
\r
308 var ratio = this.getRatio();
\r
309 return (v * ratio) - (this.minValue * ratio) - this.halfThumb;
\r
312 reverseValue : function(pos){
\r
313 var ratio = this.getRatio();
\r
314 return (pos + (this.minValue * ratio)) / ratio;
\r
318 moveThumb: function(v, animate){
\r
319 if(!animate || this.animate === false){
\r
320 this.thumb.setLeft(v);
\r
322 this.thumb.shift({left: v, stopFx: true, duration:.35});
\r
327 focus : function(){
\r
328 this.focusEl.focus(10);
\r
332 onBeforeDragStart : function(e){
\r
333 return !this.disabled;
\r
337 onDragStart: function(e){
\r
338 this.thumb.addClass('x-slider-thumb-drag');
\r
339 this.dragging = true;
\r
340 this.dragStartValue = this.value;
\r
341 this.fireEvent('dragstart', this, e);
\r
345 onDrag: function(e){
\r
346 var pos = this.innerEl.translatePoints(this.tracker.getXY());
\r
347 this.setValue(Ext.util.Format.round(this.reverseValue(pos.left), this.decimalPrecision), false);
\r
348 this.fireEvent('drag', this, e);
\r
352 onDragEnd: function(e){
\r
353 this.thumb.removeClass('x-slider-thumb-drag');
\r
354 this.dragging = false;
\r
355 this.fireEvent('dragend', this, e);
\r
356 if(this.dragStartValue != this.value){
\r
357 this.fireEvent('changecomplete', this, this.value);
\r
362 onResize : function(w, h){
\r
363 this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));
\r
365 Ext.Slider.superclass.onResize.apply(this, arguments);
\r
369 onDisable: function(){
\r
370 Ext.Slider.superclass.onDisable.call(this);
\r
371 this.thumb.addClass(this.disabledClass);
\r
373 //IE breaks when using overflow visible and opacity other than 1.
\r
374 //Create a place holder for the thumb and display it.
\r
375 var xy = this.thumb.getXY();
\r
377 this.innerEl.addClass(this.disabledClass).dom.disabled = true;
\r
378 if (!this.thumbHolder){
\r
379 this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});
\r
381 this.thumbHolder.show().setXY(xy);
\r
386 onEnable: function(){
\r
387 Ext.Slider.superclass.onEnable.call(this);
\r
388 this.thumb.removeClass(this.disabledClass);
\r
390 this.innerEl.removeClass(this.disabledClass).dom.disabled = false;
\r
391 if(this.thumbHolder){
\r
392 this.thumbHolder.hide();
\r
400 * Synchronizes the thumb position to the proper proportion of the total component width based
\r
401 * on the current slider {@link #value}. This will be called automatically when the Slider
\r
402 * is resized by a layout, but if it is rendered auto width, this method can be called from
\r
403 * another resize handler to sync the Slider if necessary.
\r
405 syncThumb : function(){
\r
407 this.moveThumb(this.translateValue(this.value));
\r
412 * Returns the current value of the slider
\r
413 * @return {Number} The current value of the slider
\r
415 getValue : function(){
\r
420 beforeDestroy : function(){
\r
421 Ext.destroyMembers(this, 'endEl', 'innerEl', 'thumb', 'halfThumb', 'focusEl', 'tracker', 'thumbHolder');
\r
422 Ext.Slider.superclass.beforeDestroy.call(this);
\r
425 Ext.reg('slider', Ext.Slider);
\r
427 // private class to support vertical sliders
\r
428 Ext.Slider.Vertical = {
\r
429 onResize : function(w, h){
\r
430 this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));
\r
434 getRatio : function(){
\r
435 var h = this.innerEl.getHeight(),
\r
436 v = this.maxValue - this.minValue;
\r
440 moveThumb: function(v, animate){
\r
441 if(!animate || this.animate === false){
\r
442 this.thumb.setBottom(v);
\r
444 this.thumb.shift({bottom: v, stopFx: true, duration:.35});
\r
448 onDrag: function(e){
\r
449 var pos = this.innerEl.translatePoints(this.tracker.getXY()),
\r
450 bottom = this.innerEl.getHeight()-pos.top;
\r
451 this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), false);
\r
452 this.fireEvent('drag', this, e);
\r
455 onClickChange : function(local){
\r
456 if(local.left > this.clickRange[0] && local.left < this.clickRange[1]){
\r
457 var bottom = this.innerEl.getHeight() - local.top;
\r
458 this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), undefined, true);
\r