Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / widgets / Slider.js
1 /*!
2  * Ext JS Library 3.1.0
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     },\r
166 \r
167         // private override\r
168     onMouseDown : function(e){\r
169         if(this.disabled){\r
170             return;\r
171         }\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
175         }\r
176         this.focus();\r
177     },\r
178 \r
179         // private\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
183         }\r
184     },\r
185 \r
186         // private\r
187     onKeyDown : function(e){\r
188         if(this.disabled){e.preventDefault();return;}\r
189         var k = e.getKey();\r
190         switch(k){\r
191             case e.UP:\r
192             case e.RIGHT:\r
193                 e.stopEvent();\r
194                 if(e.ctrlKey){\r
195                     this.setValue(this.maxValue, undefined, true);\r
196                 }else{\r
197                     this.setValue(this.value+this.keyIncrement, undefined, true);\r
198                 }\r
199             break;\r
200             case e.DOWN:\r
201             case e.LEFT:\r
202                 e.stopEvent();\r
203                 if(e.ctrlKey){\r
204                     this.setValue(this.minValue, undefined, true);\r
205                 }else{\r
206                     this.setValue(this.value-this.keyIncrement, undefined, true);\r
207                 }\r
208             break;\r
209             default:\r
210                 e.preventDefault();\r
211         }\r
212     },\r
213 \r
214         // private\r
215     doSnap : function(value){\r
216         if(!(this.increment && value)){\r
217             return value;\r
218         }\r
219         var newValue = value, \r
220             inc = this.increment,\r
221             m = value % inc;\r
222         if(m != 0){\r
223             newValue -= m;\r
224             if(m * 2 > inc){\r
225                 newValue += inc;\r
226             }else if(m * 2 < -inc){\r
227                 newValue -= inc;\r
228             }\r
229         }\r
230         return newValue.constrain(this.minValue,  this.maxValue);\r
231     },\r
232 \r
233         // private\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
239                 delete this.value;\r
240                 this.setValue(v, false);\r
241             }else{\r
242                 this.moveThumb(this.translateValue(v), false);\r
243             }\r
244         }\r
245     },\r
246 \r
247         // private\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
252     },\r
253 \r
254         // private\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
259         return v;\r
260     },\r
261 \r
262         /**\r
263          * Programmatically sets the value of the Slider. Ensures that the value is constrained within\r
264          * the minValue and maxValue.\r
265          * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)\r
266          * @param {Boolean} animate Turn on or off animation, defaults to true\r
267          */\r
268     setValue : function(v, animate, changeComplete){\r
269         v = this.normalizeValue(v);\r
270         if(v !== this.value && this.fireEvent('beforechange', this, v, this.value) !== false){\r
271             this.value = v;\r
272             this.moveThumb(this.translateValue(v), animate !== false);\r
273             this.fireEvent('change', this, v);\r
274             if(changeComplete){\r
275                 this.fireEvent('changecomplete', this, v);\r
276             }\r
277         }\r
278     },\r
279 \r
280         // private\r
281     translateValue : function(v){\r
282         var ratio = this.getRatio();\r
283         return (v * ratio) - (this.minValue * ratio) - this.halfThumb;\r
284     },\r
285 \r
286         reverseValue : function(pos){\r
287         var ratio = this.getRatio();\r
288         return (pos + this.halfThumb + (this.minValue * ratio)) / ratio;\r
289     },\r
290 \r
291         // private\r
292     moveThumb: function(v, animate){\r
293         if(!animate || this.animate === false){\r
294             this.thumb.setLeft(v);\r
295         }else{\r
296             this.thumb.shift({left: v, stopFx: true, duration:.35});\r
297         }\r
298     },\r
299 \r
300         // private\r
301     focus : function(){\r
302         this.focusEl.focus(10);\r
303     },\r
304 \r
305         // private\r
306     onBeforeDragStart : function(e){\r
307         return !this.disabled;\r
308     },\r
309 \r
310         // private\r
311     onDragStart: function(e){\r
312         this.thumb.addClass('x-slider-thumb-drag');\r
313         this.dragging = true;\r
314         this.dragStartValue = this.value;\r
315         this.fireEvent('dragstart', this, e);\r
316     },\r
317 \r
318         // private\r
319     onDrag: function(e){\r
320         var pos = this.innerEl.translatePoints(this.tracker.getXY());\r
321         this.setValue(Ext.util.Format.round(this.reverseValue(pos.left), this.decimalPrecision), false);\r
322         this.fireEvent('drag', this, e);\r
323     },\r
324 \r
325         // private\r
326     onDragEnd: function(e){\r
327         this.thumb.removeClass('x-slider-thumb-drag');\r
328         this.dragging = false;\r
329         this.fireEvent('dragend', this, e);\r
330         if(this.dragStartValue != this.value){\r
331             this.fireEvent('changecomplete', this, this.value);\r
332         }\r
333     },\r
334 \r
335         // private\r
336     onResize : function(w, h){\r
337         this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));\r
338         this.syncThumb();\r
339     },\r
340     \r
341     //private\r
342     onDisable: function(){\r
343         Ext.Slider.superclass.onDisable.call(this);\r
344         this.thumb.addClass(this.disabledClass);\r
345         if(Ext.isIE){\r
346             //IE breaks when using overflow visible and opacity other than 1.\r
347             //Create a place holder for the thumb and display it.\r
348             var xy = this.thumb.getXY();\r
349             this.thumb.hide();\r
350             this.innerEl.addClass(this.disabledClass).dom.disabled = true;\r
351             if (!this.thumbHolder){\r
352                 this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});    \r
353             }\r
354             this.thumbHolder.show().setXY(xy);\r
355         }\r
356     },\r
357     \r
358     //private\r
359     onEnable: function(){\r
360         Ext.Slider.superclass.onEnable.call(this);\r
361         this.thumb.removeClass(this.disabledClass);\r
362         if(Ext.isIE){\r
363             this.innerEl.removeClass(this.disabledClass).dom.disabled = false;\r
364             if(this.thumbHolder){\r
365                 this.thumbHolder.hide();\r
366             }\r
367             this.thumb.show();\r
368             this.syncThumb();\r
369         }\r
370     },\r
371     \r
372     /**\r
373      * Synchronizes the thumb position to the proper proportion of the total component width based\r
374      * on the current slider {@link #value}.  This will be called automatically when the Slider\r
375      * is resized by a layout, but if it is rendered auto width, this method can be called from\r
376      * another resize handler to sync the Slider if necessary.\r
377      */\r
378     syncThumb : function(){\r
379         if(this.rendered){\r
380             this.moveThumb(this.translateValue(this.value));\r
381         }\r
382     },\r
383 \r
384         /**\r
385          * Returns the current value of the slider\r
386          * @return {Number} The current value of the slider\r
387          */\r
388     getValue : function(){\r
389         return this.value;\r
390     },\r
391     \r
392     // private\r
393     beforeDestroy : function(){\r
394         Ext.destroyMembers(this, 'endEl', 'innerEl', 'thumb', 'halfThumb', 'focusEl', 'tracker', 'thumbHolder');\r
395         Ext.Slider.superclass.beforeDestroy.call(this);\r
396     }\r
397 });\r
398 Ext.reg('slider', Ext.Slider);\r
399 \r
400 // private class to support vertical sliders\r
401 Ext.Slider.Vertical = {\r
402     onResize : function(w, h){\r
403         this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));\r
404         this.syncThumb();\r
405     },\r
406 \r
407     getRatio : function(){\r
408         var h = this.innerEl.getHeight(),\r
409             v = this.maxValue - this.minValue;\r
410         return h/v;\r
411     },\r
412 \r
413     moveThumb: function(v, animate){\r
414         if(!animate || this.animate === false){\r
415             this.thumb.setBottom(v);\r
416         }else{\r
417             this.thumb.shift({bottom: v, stopFx: true, duration:.35});\r
418         }\r
419     },\r
420 \r
421     onDrag: function(e){\r
422         var pos = this.innerEl.translatePoints(this.tracker.getXY()),\r
423             bottom = this.innerEl.getHeight()-pos.top;\r
424         this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), false);\r
425         this.fireEvent('drag', this, e);\r
426     },\r
427 \r
428     onClickChange : function(local){\r
429         if(local.left > this.clickRange[0] && local.left < this.clickRange[1]){\r
430             var bottom = this.innerEl.getHeight() - local.top;\r
431             this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), undefined, true);\r
432         }\r
433     }\r
434 };