Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / slider / Single.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.slider.Single
17  * @extends Ext.slider.Multi
18  * Slider which supports vertical or horizontal orientation, keyboard adjustments,
19  * configurable snapping, axis clicking and animation. Can be added as an item to
20  * any container. 
21  * {@img Ext.slider.Single/Ext.slider.Single.png Ext.slider.Single component}
22  * Example usage:
23 <pre><code>
24     Ext.create('Ext.slider.Single', {
25         width: 200,
26         value: 50,
27         increment: 10,
28         minValue: 0,
29         maxValue: 100,
30         renderTo: Ext.getBody()
31     });
32 </code></pre>
33  * The class Ext.slider.Single is aliased to Ext.Slider for backwards compatibility.
34  */
35 Ext.define('Ext.slider.Single', {
36     extend: 'Ext.slider.Multi',
37     alias: ['widget.slider', 'widget.sliderfield'],
38     alternateClassName: ['Ext.Slider', 'Ext.form.SliderField', 'Ext.slider.SingleSlider', 'Ext.slider.Slider'],
39
40     /**
41      * Returns the current value of the slider
42      * @return {Number} The current value of the slider
43      */
44     getValue: function() {
45         //just returns the value of the first thumb, which should be the only one in a single slider
46         return this.callParent([0]);
47     },
48
49     /**
50      * Programmatically sets the value of the Slider. Ensures that the value is constrained within
51      * the minValue and maxValue.
52      * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)
53      * @param {Boolean} animate Turn on or off animation, defaults to true
54      */
55     setValue: function(value, animate) {
56         var args = Ext.toArray(arguments),
57             len  = args.length;
58
59         //this is to maintain backwards compatiblity for sliders with only one thunb. Usually you must pass the thumb
60         //index to setValue, but if we only have one thumb we inject the index here first if given the multi-slider
61         //signature without the required index. The index will always be 0 for a single slider
62         if (len == 1 || (len <= 3 && typeof arguments[1] != 'number')) {
63             args.unshift(0);
64         }
65
66         return this.callParent(args);
67     },
68
69     // private
70     getNearest : function(){
71         // Since there's only 1 thumb, it's always the nearest
72         return this.thumbs[0];
73     }
74 });
75