2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
11 * @extends Ext.BoxComponent
\r
12 * Slider which supports vertical or horizontal orientation, keyboard adjustments,
\r
13 * configurable snapping, axis clicking and animation. Can be added as an item to
\r
14 * any container. Example usage:
\r
17 renderTo: Ext.getBody(),
\r
26 Ext.Slider = Ext.extend(Ext.BoxComponent, {
\r
28 * @cfg {Number} value The value to initialize the slider with. Defaults to minValue.
\r
31 * @cfg {Boolean} vertical Orient the Slider vertically rather than horizontally, defaults to false.
\r
35 * @cfg {Number} minValue The minimum value for the Slider. Defaults to 0.
\r
39 * @cfg {Number} maxValue The maximum value for the Slider. Defaults to 100.
\r
43 * @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
47 * @cfg {Number} increment How many units to change the slider when adjusting by drag and drop. Use this option to enable 'snapping'.
\r
53 * @cfg {Boolean} clickToChange Determines whether or not clicking on the Slider axis will change the slider. Defaults to true
\r
55 clickToChange : true,
\r
57 * @cfg {Boolean} animate Turn on or off animation. Defaults to true
\r
62 * True while the thumb is in a drag operation
\r
68 initComponent : function(){
\r
69 if(this.value === undefined){
\r
70 this.value = this.minValue;
\r
72 Ext.Slider.superclass.initComponent.call(this);
\r
73 this.keyIncrement = Math.max(this.increment, this.keyIncrement);
\r
76 * @event beforechange
\r
77 * Fires before the slider value is changed. By returning false from an event handler,
\r
78 * you can cancel the event and prevent the slider from changing.
\r
79 * @param {Ext.Slider} slider The slider
\r
80 * @param {Number} newValue The new value which the slider is being changed to.
\r
81 * @param {Number} oldValue The old value which the slider was previously.
\r
86 * Fires when the slider value is changed.
\r
87 * @param {Ext.Slider} slider The slider
\r
88 * @param {Number} newValue The new value which the slider has been changed to.
\r
92 * @event changecomplete
\r
93 * Fires when the slider value is changed by the user and any drag operations have completed.
\r
94 * @param {Ext.Slider} slider The slider
\r
95 * @param {Number} newValue The new value which the slider has been changed to.
\r
100 * Fires after a drag operation has started.
\r
101 * @param {Ext.Slider} slider The slider
\r
102 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
107 * Fires continuously during the drag operation while the mouse is moving.
\r
108 * @param {Ext.Slider} slider The slider
\r
109 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
114 * Fires after the drag operation has completed.
\r
115 * @param {Ext.Slider} slider The slider
\r
116 * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker
\r
122 Ext.apply(this, Ext.Slider.Vertical);
\r
126 // private override
\r
127 onRender : function(){
\r
129 cls: 'x-slider ' + (this.vertical ? 'x-slider-vert' : 'x-slider-horz'),
\r
130 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
132 Ext.Slider.superclass.onRender.apply(this, arguments);
\r
133 this.endEl = this.el.first();
\r
134 this.innerEl = this.endEl.first();
\r
135 this.thumb = this.innerEl.first();
\r
136 this.halfThumb = (this.vertical ? this.thumb.getHeight() : this.thumb.getWidth())/2;
\r
137 this.focusEl = this.thumb.next();
\r
141 // private override
\r
142 initEvents : function(){
\r
143 this.thumb.addClassOnOver('x-slider-thumb-over');
\r
144 this.mon(this.el, 'mousedown', this.onMouseDown, this);
\r
145 this.mon(this.el, 'keydown', this.onKeyDown, this);
\r
147 this.focusEl.swallowEvent("click", true);
\r
149 this.tracker = new Ext.dd.DragTracker({
\r
150 onBeforeStart: this.onBeforeDragStart.createDelegate(this),
\r
151 onStart: this.onDragStart.createDelegate(this),
\r
152 onDrag: this.onDrag.createDelegate(this),
\r
153 onEnd: this.onDragEnd.createDelegate(this),
\r
157 this.tracker.initEl(this.thumb);
\r
158 this.on('beforedestroy', this.tracker.destroy, this.tracker);
\r
161 // private override
\r
162 onMouseDown : function(e){
\r
163 if(this.disabled) {return;}
\r
164 if(this.clickToChange && e.target != this.thumb.dom){
\r
165 var local = this.innerEl.translatePoints(e.getXY());
\r
166 this.onClickChange(local);
\r
172 onClickChange : function(local){
\r
173 if(local.top > this.clickRange[0] && local.top < this.clickRange[1]){
\r
174 this.setValue(Math.round(this.reverseValue(local.left)), undefined, true);
\r
179 onKeyDown : function(e){
\r
180 if(this.disabled){e.preventDefault();return;}
\r
181 var k = e.getKey();
\r
187 this.setValue(this.maxValue, undefined, true);
\r
189 this.setValue(this.value+this.keyIncrement, undefined, true);
\r
196 this.setValue(this.minValue, undefined, true);
\r
198 this.setValue(this.value-this.keyIncrement, undefined, true);
\r
202 e.preventDefault();
\r
207 doSnap : function(value){
\r
208 if(!this.increment || this.increment == 1 || !value) {
\r
211 var newValue = value, inc = this.increment;
\r
212 var m = value % inc;
\r
215 newValue = value + (inc-m);
\r
217 newValue = value - m;
\r
220 return newValue.constrain(this.minValue, this.maxValue);
\r
224 afterRender : function(){
\r
225 Ext.Slider.superclass.afterRender.apply(this, arguments);
\r
226 if(this.value !== undefined){
\r
227 var v = this.normalizeValue(this.value);
\r
228 if(v !== this.value){
\r
230 this.setValue(v, false);
\r
232 this.moveThumb(this.translateValue(v), false);
\r
238 getRatio : function(){
\r
239 var w = this.innerEl.getWidth();
\r
240 var v = this.maxValue - this.minValue;
\r
241 return v == 0 ? w : (w/v);
\r
245 normalizeValue : function(v){
\r
246 if(typeof v != 'number'){
\r
250 v = this.doSnap(v);
\r
251 v = v.constrain(this.minValue, this.maxValue);
\r
256 * Programmatically sets the value of the Slider. Ensures that the value is constrained within
\r
257 * the minValue and maxValue.
\r
258 * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
\r
259 * @param {Boolean} animate Turn on or off animation, defaults to true
\r
261 setValue : function(v, animate, changeComplete){
\r
262 v = this.normalizeValue(v);
\r
263 if(v !== this.value && this.fireEvent('beforechange', this, v, this.value) !== false){
\r
265 this.moveThumb(this.translateValue(v), animate !== false);
\r
266 this.fireEvent('change', this, v);
\r
267 if(changeComplete){
\r
268 this.fireEvent('changecomplete', this, v);
\r
274 translateValue : function(v){
\r
275 var ratio = this.getRatio();
\r
276 return (v * ratio)-(this.minValue * ratio)-this.halfThumb;
\r
279 reverseValue : function(pos){
\r
280 var ratio = this.getRatio();
\r
281 return (pos+this.halfThumb+(this.minValue * ratio))/ratio;
\r
285 moveThumb: function(v, animate){
\r
286 if(!animate || this.animate === false){
\r
287 this.thumb.setLeft(v);
\r
289 this.thumb.shift({left: v, stopFx: true, duration:.35});
\r
294 focus : function(){
\r
295 this.focusEl.focus(10);
\r
299 onBeforeDragStart : function(e){
\r
300 return !this.disabled;
\r
304 onDragStart: function(e){
\r
305 this.thumb.addClass('x-slider-thumb-drag');
\r
306 this.dragging = true;
\r
307 this.dragStartValue = this.value;
\r
308 this.fireEvent('dragstart', this, e);
\r
312 onDrag: function(e){
\r
313 var pos = this.innerEl.translatePoints(this.tracker.getXY());
\r
314 this.setValue(Math.round(this.reverseValue(pos.left)), false);
\r
315 this.fireEvent('drag', this, e);
\r
319 onDragEnd: function(e){
\r
320 this.thumb.removeClass('x-slider-thumb-drag');
\r
321 this.dragging = false;
\r
322 this.fireEvent('dragend', this, e);
\r
323 if(this.dragStartValue != this.value){
\r
324 this.fireEvent('changecomplete', this, this.value);
\r
329 onDisable: function(){
\r
330 Ext.Slider.superclass.onDisable.call(this);
\r
331 this.thumb.addClass(this.disabledClass);
\r
333 //IE breaks when using overflow visible and opacity other than 1.
\r
334 //Create a place holder for the thumb and display it.
\r
335 var xy = this.thumb.getXY();
\r
337 this.innerEl.addClass(this.disabledClass).dom.disabled = true;
\r
338 if (!this.thumbHolder){
\r
339 this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});
\r
341 this.thumbHolder.show().setXY(xy);
\r
346 onEnable: function(){
\r
347 Ext.Slider.superclass.onEnable.call(this);
\r
348 this.thumb.removeClass(this.disabledClass);
\r
350 this.innerEl.removeClass(this.disabledClass).dom.disabled = false;
\r
351 if (this.thumbHolder){
\r
352 this.thumbHolder.hide();
\r
360 onResize : function(w, h){
\r
361 this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));
\r
366 * Synchronizes the thumb position to the proper proportion of the total component width based
\r
367 * on the current slider {@link #value}. This will be called automatically when the Slider
\r
368 * is resized by a layout, but if it is rendered auto width, this method can be called from
\r
369 * another resize handler to sync the Slider if necessary.
\r
371 syncThumb : function(){
\r
373 this.moveThumb(this.translateValue(this.value));
\r
378 * Returns the current value of the slider
\r
379 * @return {Number} The current value of the slider
\r
381 getValue : function(){
\r
385 Ext.reg('slider', Ext.Slider);
\r
387 // private class to support vertical sliders
\r
388 Ext.Slider.Vertical = {
\r
389 onResize : function(w, h){
\r
390 this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));
\r
394 getRatio : function(){
\r
395 var h = this.innerEl.getHeight();
\r
396 var v = this.maxValue - this.minValue;
\r
400 moveThumb: function(v, animate){
\r
401 if(!animate || this.animate === false){
\r
402 this.thumb.setBottom(v);
\r
404 this.thumb.shift({bottom: v, stopFx: true, duration:.35});
\r
408 onDrag: function(e){
\r
409 var pos = this.innerEl.translatePoints(this.tracker.getXY());
\r
410 var bottom = this.innerEl.getHeight()-pos.top;
\r
411 this.setValue(this.minValue + Math.round(bottom/this.getRatio()), false);
\r
412 this.fireEvent('drag', this, e);
\r
415 onClickChange : function(local){
\r
416 if(local.left > this.clickRange[0] && local.left < this.clickRange[1]){
\r
417 var bottom = this.innerEl.getHeight()-local.top;
\r
418 this.setValue(this.minValue + Math.round(bottom/this.getRatio()), undefined, true);
\r