Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / Slider.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.Slider\r
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
13 <pre><code>\r
14 new Ext.Slider({\r
15     renderTo: Ext.getBody(),\r
16     width: 200,\r
17     value: 50,\r
18     increment: 10,\r
19     minValue: 0,\r
20     maxValue: 100\r
21 });\r
22 </code></pre>\r
23  */\r
24 Ext.Slider = Ext.extend(Ext.BoxComponent, {\r
25         /**\r
26          * @cfg {Number} value The value to initialize the slider with. Defaults to minValue.\r
27          */\r
28         /**\r
29          * @cfg {Boolean} vertical Orient the Slider vertically rather than horizontally, defaults to false.\r
30          */\r
31     vertical: false,\r
32         /**\r
33          * @cfg {Number} minValue The minimum value for the Slider. Defaults to 0.\r
34          */\r
35     minValue: 0,\r
36         /**\r
37          * @cfg {Number} maxValue The maximum value for the Slider. Defaults to 100.\r
38          */\r
39     maxValue: 100,\r
40     /**\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
44      */\r
45     decimalPrecision: 0,\r
46         /**\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
48          */\r
49     keyIncrement: 1,\r
50         /**\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
52          */\r
53     increment: 0,\r
54         // private\r
55     clickRange: [5,15],\r
56         /**\r
57          * @cfg {Boolean} clickToChange Determines whether or not clicking on the Slider axis will change the slider. Defaults to true\r
58          */\r
59     clickToChange : true,\r
60         /**\r
61          * @cfg {Boolean} animate Turn on or off animation. Defaults to true\r
62          */\r
63     animate: true,\r
64 \r
65     /**\r
66      * True while the thumb is in a drag operation\r
67      * @type boolean\r
68      */\r
69     dragging: false,\r
70 \r
71     // private override\r
72     initComponent : function(){\r
73         if(!Ext.isDefined(this.value)){\r
74             this.value = this.minValue;\r
75         }\r
76         Ext.Slider.superclass.initComponent.call(this);\r
77         this.keyIncrement = Math.max(this.increment, this.keyIncrement);\r
78         this.addEvents(\r
79             /**\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
86              */\r
87                         'beforechange',\r
88                         /**\r
89                          * @event change\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
93                          */\r
94                         'change',\r
95                         /**\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
100                          */\r
101                         'changecomplete',\r
102                         /**\r
103                          * @event dragstart\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
107                          */\r
108                         'dragstart',\r
109                         /**\r
110                          * @event drag\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
114                          */\r
115                         'drag',\r
116                         /**\r
117                          * @event dragend\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
121                          */\r
122                         'dragend'\r
123                 );\r
124 \r
125         if(this.vertical){\r
126             Ext.apply(this, Ext.Slider.Vertical);\r
127         }\r
128     },\r
129 \r
130         // private override\r
131     onRender : function(){\r
132         this.autoEl = {\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
135         };\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
142         this.initEvents();\r
143     },\r
144 \r
145         // private override\r
146     initEvents : function(){\r
147         this.thumb.addClassOnOver('x-slider-thumb-over');\r
148         this.mon(this.el, {\r
149             scope: this,\r
150             mousedown: this.onMouseDown,\r
151             keydown: this.onKeyDown\r
152         });\r
153 \r
154         this.focusEl.swallowEvent("click", true);\r
155 \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
161             tolerance: 3,\r
162             autoStart: 300\r
163         });\r
164         this.tracker.initEl(this.thumb);\r
165         this.on('beforedestroy', this.tracker.destroy, this.tracker);\r
166     },\r
167 \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
174         }\r
175         this.focus();\r
176     },\r
177 \r
178         // private\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
182         }\r
183     },\r
184 \r
185         // private\r
186     onKeyDown : function(e){\r
187         if(this.disabled){e.preventDefault();return;}\r
188         var k = e.getKey();\r
189         switch(k){\r
190             case e.UP:\r
191             case e.RIGHT:\r
192                 e.stopEvent();\r
193                 if(e.ctrlKey){\r
194                     this.setValue(this.maxValue, undefined, true);\r
195                 }else{\r
196                     this.setValue(this.value+this.keyIncrement, undefined, true);\r
197                 }\r
198             break;\r
199             case e.DOWN:\r
200             case e.LEFT:\r
201                 e.stopEvent();\r
202                 if(e.ctrlKey){\r
203                     this.setValue(this.minValue, undefined, true);\r
204                 }else{\r
205                     this.setValue(this.value-this.keyIncrement, undefined, true);\r
206                 }\r
207             break;\r
208             default:\r
209                 e.preventDefault();\r
210         }\r
211     },\r
212 \r
213         // private\r
214     doSnap : function(value){\r
215         if(!this.increment || this.increment == 1 || !value) {\r
216             return value;\r
217         }\r
218         var newValue = value, inc = this.increment;\r
219         var m = value % inc;\r
220         if(m != 0){\r
221             newValue -= m;\r
222             if(m * 2 > inc){\r
223                 newValue += inc;\r
224             }else if(m * 2 < -inc){\r
225                 newValue -= inc;\r
226             }\r
227         }\r
228         return newValue.constrain(this.minValue,  this.maxValue);\r
229     },\r
230 \r
231         // private\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
237                 delete this.value;\r
238                 this.setValue(v, false);\r
239             }else{\r
240                 this.moveThumb(this.translateValue(v), false);\r
241             }\r
242         }\r
243     },\r
244 \r
245         // private\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
250     },\r
251 \r
252         // private\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
257         return v;\r
258     },\r
259 \r
260         /**\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
265          */\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
269             this.value = v;\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
274             }\r
275         }\r
276     },\r
277 \r
278         // private\r
279     translateValue : function(v){\r
280         var ratio = this.getRatio();\r
281         return (v * ratio)-(this.minValue * ratio)-this.halfThumb;\r
282     },\r
283 \r
284         reverseValue : function(pos){\r
285         var ratio = this.getRatio();\r
286         return (pos+this.halfThumb+(this.minValue * ratio))/ratio;\r
287     },\r
288 \r
289         // private\r
290     moveThumb: function(v, animate){\r
291         if(!animate || this.animate === false){\r
292             this.thumb.setLeft(v);\r
293         }else{\r
294             this.thumb.shift({left: v, stopFx: true, duration:.35});\r
295         }\r
296     },\r
297 \r
298         // private\r
299     focus : function(){\r
300         this.focusEl.focus(10);\r
301     },\r
302 \r
303         // private\r
304     onBeforeDragStart : function(e){\r
305         return !this.disabled;\r
306     },\r
307 \r
308         // private\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
314     },\r
315 \r
316         // private\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
321     },\r
322 \r
323         // private\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
330         }\r
331     },\r
332 \r
333         // private\r
334     onResize : function(w, h){\r
335         this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));\r
336         this.syncThumb();\r
337     },\r
338     \r
339     //private\r
340     onDisable: function(){\r
341         Ext.Slider.superclass.onDisable.call(this);\r
342         this.thumb.addClass(this.disabledClass);\r
343         if(Ext.isIE){\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
347             this.thumb.hide();\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
351             }\r
352             this.thumbHolder.show().setXY(xy);\r
353         }\r
354     },\r
355     \r
356     //private\r
357     onEnable: function(){\r
358         Ext.Slider.superclass.onEnable.call(this);\r
359         this.thumb.removeClass(this.disabledClass);\r
360         if(Ext.isIE){\r
361             this.innerEl.removeClass(this.disabledClass).dom.disabled = false;\r
362             if (this.thumbHolder){\r
363                 this.thumbHolder.hide();\r
364             }\r
365             this.thumb.show();\r
366             this.syncThumb();\r
367         }\r
368     },\r
369     \r
370     /**\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
375      */\r
376     syncThumb : function(){\r
377         if(this.rendered){\r
378             this.moveThumb(this.translateValue(this.value));\r
379         }\r
380     },\r
381 \r
382         /**\r
383          * Returns the current value of the slider\r
384          * @return {Number} The current value of the slider\r
385          */\r
386     getValue : function(){\r
387         return this.value;\r
388     }\r
389 });\r
390 Ext.reg('slider', Ext.Slider);\r
391 \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
396         this.syncThumb();\r
397     },\r
398 \r
399     getRatio : function(){\r
400         var h = this.innerEl.getHeight();\r
401         var v = this.maxValue - this.minValue;\r
402         return h/v;\r
403     },\r
404 \r
405     moveThumb: function(v, animate){\r
406         if(!animate || this.animate === false){\r
407             this.thumb.setBottom(v);\r
408         }else{\r
409             this.thumb.shift({bottom: v, stopFx: true, duration:.35});\r
410         }\r
411     },\r
412 \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
418     },\r
419 \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
424         }\r
425     }\r
426 };