3 * Copyright(c) 2006-2009 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
165 this.on('beforedestroy', this.tracker.destroy, this.tracker);
\r
168 // private override
\r
169 onMouseDown : function(e){
\r
170 if(this.disabled) {return;}
\r
171 if(this.clickToChange && e.target != this.thumb.dom){
\r
172 var local = this.innerEl.translatePoints(e.getXY());
\r
173 this.onClickChange(local);
\r
179 onClickChange : function(local){
\r
180 if(local.top > this.clickRange[0] && local.top < this.clickRange[1]){
\r
181 this.setValue(Ext.util.Format.round(this.reverseValue(local.left), this.decimalPrecision), undefined, true);
\r
186 onKeyDown : function(e){
\r
187 if(this.disabled){e.preventDefault();return;}
\r
188 var k = e.getKey();
\r
194 this.setValue(this.maxValue, undefined, true);
\r
196 this.setValue(this.value+this.keyIncrement, undefined, true);
\r
203 this.setValue(this.minValue, undefined, true);
\r
205 this.setValue(this.value-this.keyIncrement, undefined, true);
\r
209 e.preventDefault();
\r
214 doSnap : function(value){
\r
215 if(!this.increment || this.increment == 1 || !value) {
\r
218 var newValue = value, inc = this.increment;
\r
219 var m = value % inc;
\r
224 }else if(m * 2 < -inc){
\r
228 return newValue.constrain(this.minValue, this.maxValue);
\r
232 afterRender : function(){
\r
233 Ext.Slider.superclass.afterRender.apply(this, arguments);
\r
234 if(this.value !== undefined){
\r
235 var v = this.normalizeValue(this.value);
\r
236 if(v !== this.value){
\r
238 this.setValue(v, false);
\r
240 this.moveThumb(this.translateValue(v), false);
\r
246 getRatio : function(){
\r
247 var w = this.innerEl.getWidth();
\r
248 var v = this.maxValue - this.minValue;
\r
249 return v == 0 ? w : (w/v);
\r
253 normalizeValue : function(v){
\r
254 v = this.doSnap(v);
\r
255 v = Ext.util.Format.round(v, this.decimalPrecision);
\r
256 v = v.constrain(this.minValue, this.maxValue);
\r
261 * Programmatically sets the value of the Slider. Ensures that the value is constrained within
\r
262 * the minValue and maxValue.
\r
263 * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
\r
264 * @param {Boolean} animate Turn on or off animation, defaults to true
\r
266 setValue : function(v, animate, changeComplete){
\r
267 v = this.normalizeValue(v);
\r
268 if(v !== this.value && this.fireEvent('beforechange', this, v, this.value) !== false){
\r
270 this.moveThumb(this.translateValue(v), animate !== false);
\r
271 this.fireEvent('change', this, v);
\r
272 if(changeComplete){
\r
273 this.fireEvent('changecomplete', this, v);
\r
279 translateValue : function(v){
\r
280 var ratio = this.getRatio();
\r
281 return (v * ratio)-(this.minValue * ratio)-this.halfThumb;
\r
284 reverseValue : function(pos){
\r
285 var ratio = this.getRatio();
\r
286 return (pos+this.halfThumb+(this.minValue * ratio))/ratio;
\r
290 moveThumb: function(v, animate){
\r
291 if(!animate || this.animate === false){
\r
292 this.thumb.setLeft(v);
\r
294 this.thumb.shift({left: v, stopFx: true, duration:.35});
\r
299 focus : function(){
\r
300 this.focusEl.focus(10);
\r
304 onBeforeDragStart : function(e){
\r
305 return !this.disabled;
\r
309 onDragStart: function(e){
\r
310 this.thumb.addClass('x-slider-thumb-drag');
\r
311 this.dragging = true;
\r
312 this.dragStartValue = this.value;
\r
313 this.fireEvent('dragstart', this, e);
\r
317 onDrag: function(e){
\r
318 var pos = this.innerEl.translatePoints(this.tracker.getXY());
\r
319 this.setValue(Ext.util.Format.round(this.reverseValue(pos.left), this.decimalPrecision), false);
\r
320 this.fireEvent('drag', this, e);
\r
324 onDragEnd: function(e){
\r
325 this.thumb.removeClass('x-slider-thumb-drag');
\r
326 this.dragging = false;
\r
327 this.fireEvent('dragend', this, e);
\r
328 if(this.dragStartValue != this.value){
\r
329 this.fireEvent('changecomplete', this, this.value);
\r
334 onResize : function(w, h){
\r
335 this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));
\r
340 onDisable: function(){
\r
341 Ext.Slider.superclass.onDisable.call(this);
\r
342 this.thumb.addClass(this.disabledClass);
\r
344 //IE breaks when using overflow visible and opacity other than 1.
\r
345 //Create a place holder for the thumb and display it.
\r
346 var xy = this.thumb.getXY();
\r
348 this.innerEl.addClass(this.disabledClass).dom.disabled = true;
\r
349 if (!this.thumbHolder){
\r
350 this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});
\r
352 this.thumbHolder.show().setXY(xy);
\r
357 onEnable: function(){
\r
358 Ext.Slider.superclass.onEnable.call(this);
\r
359 this.thumb.removeClass(this.disabledClass);
\r
361 this.innerEl.removeClass(this.disabledClass).dom.disabled = false;
\r
362 if (this.thumbHolder){
\r
363 this.thumbHolder.hide();
\r
371 * Synchronizes the thumb position to the proper proportion of the total component width based
\r
372 * on the current slider {@link #value}. This will be called automatically when the Slider
\r
373 * is resized by a layout, but if it is rendered auto width, this method can be called from
\r
374 * another resize handler to sync the Slider if necessary.
\r
376 syncThumb : function(){
\r
378 this.moveThumb(this.translateValue(this.value));
\r
383 * Returns the current value of the slider
\r
384 * @return {Number} The current value of the slider
\r
386 getValue : function(){
\r
390 Ext.reg('slider', Ext.Slider);
\r
392 // private class to support vertical sliders
\r
393 Ext.Slider.Vertical = {
\r
394 onResize : function(w, h){
\r
395 this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));
\r
399 getRatio : function(){
\r
400 var h = this.innerEl.getHeight();
\r
401 var v = this.maxValue - this.minValue;
\r
405 moveThumb: function(v, animate){
\r
406 if(!animate || this.animate === false){
\r
407 this.thumb.setBottom(v);
\r
409 this.thumb.shift({bottom: v, stopFx: true, duration:.35});
\r
413 onDrag: function(e){
\r
414 var pos = this.innerEl.translatePoints(this.tracker.getXY());
\r
415 var bottom = this.innerEl.getHeight()-pos.top;
\r
416 this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), false);
\r
417 this.fireEvent('drag', this, e);
\r
420 onClickChange : function(local){
\r
421 if(local.left > this.clickRange[0] && local.left < this.clickRange[1]){
\r
422 var bottom = this.innerEl.getHeight()-local.top;
\r
423 this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), undefined, true);
\r