Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / widgets / Slider.js
diff --git a/src/widgets/Slider.js b/src/widgets/Slider.js
new file mode 100644 (file)
index 0000000..c68049e
--- /dev/null
@@ -0,0 +1,426 @@
+/*!
+ * Ext JS Library 3.0.0
+ * Copyright(c) 2006-2009 Ext JS, LLC
+ * licensing@extjs.com
+ * http://www.extjs.com/license
+ */
+/**\r
+ * @class Ext.Slider\r
+ * @extends Ext.BoxComponent\r
+ * Slider which supports vertical or horizontal orientation, keyboard adjustments,\r
+ * configurable snapping, axis clicking and animation. Can be added as an item to\r
+ * any container. Example usage:\r
+<pre><code>\r
+new Ext.Slider({\r
+    renderTo: Ext.getBody(),\r
+    width: 200,\r
+    value: 50,\r
+    increment: 10,\r
+    minValue: 0,\r
+    maxValue: 100\r
+});\r
+</code></pre>\r
+ */\r
+Ext.Slider = Ext.extend(Ext.BoxComponent, {\r
+       /**\r
+        * @cfg {Number} value The value to initialize the slider with. Defaults to minValue.\r
+        */\r
+       /**\r
+        * @cfg {Boolean} vertical Orient the Slider vertically rather than horizontally, defaults to false.\r
+        */\r
+    vertical: false,\r
+       /**\r
+        * @cfg {Number} minValue The minimum value for the Slider. Defaults to 0.\r
+        */\r
+    minValue: 0,\r
+       /**\r
+        * @cfg {Number} maxValue The maximum value for the Slider. Defaults to 100.\r
+        */\r
+    maxValue: 100,\r
+    /**\r
+     * @cfg {Number/Boolean} decimalPrecision.\r
+     * <p>The number of decimal places to which to round the Slider's value. Defaults to 0.</p>\r
+     * <p>To disable rounding, configure as <tt><b>false</b></tt>.</p>\r
+     */\r
+    decimalPrecision: 0,\r
+       /**\r
+        * @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
+        */\r
+    keyIncrement: 1,\r
+       /**\r
+        * @cfg {Number} increment How many units to change the slider when adjusting by drag and drop. Use this option to enable 'snapping'.\r
+        */\r
+    increment: 0,\r
+       // private\r
+    clickRange: [5,15],\r
+       /**\r
+        * @cfg {Boolean} clickToChange Determines whether or not clicking on the Slider axis will change the slider. Defaults to true\r
+        */\r
+    clickToChange : true,\r
+       /**\r
+        * @cfg {Boolean} animate Turn on or off animation. Defaults to true\r
+        */\r
+    animate: true,\r
+\r
+    /**\r
+     * True while the thumb is in a drag operation\r
+     * @type boolean\r
+     */\r
+    dragging: false,\r
+\r
+    // private override\r
+    initComponent : function(){\r
+        if(!Ext.isDefined(this.value)){\r
+            this.value = this.minValue;\r
+        }\r
+        Ext.Slider.superclass.initComponent.call(this);\r
+        this.keyIncrement = Math.max(this.increment, this.keyIncrement);\r
+        this.addEvents(\r
+            /**\r
+             * @event beforechange\r
+             * Fires before the slider value is changed. By returning false from an event handler,\r
+             * you can cancel the event and prevent the slider from changing.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Number} newValue The new value which the slider is being changed to.\r
+                        * @param {Number} oldValue The old value which the slider was previously.\r
+             */\r
+                       'beforechange',\r
+                       /**\r
+                        * @event change\r
+                        * Fires when the slider value is changed.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Number} newValue The new value which the slider has been changed to.\r
+                        */\r
+                       'change',\r
+                       /**\r
+                        * @event changecomplete\r
+                        * Fires when the slider value is changed by the user and any drag operations have completed.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Number} newValue The new value which the slider has been changed to.\r
+                        */\r
+                       'changecomplete',\r
+                       /**\r
+                        * @event dragstart\r
+             * Fires after a drag operation has started.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker\r
+                        */\r
+                       'dragstart',\r
+                       /**\r
+                        * @event drag\r
+             * Fires continuously during the drag operation while the mouse is moving.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker\r
+                        */\r
+                       'drag',\r
+                       /**\r
+                        * @event dragend\r
+             * Fires after the drag operation has completed.\r
+                        * @param {Ext.Slider} slider The slider\r
+                        * @param {Ext.EventObject} e The event fired from Ext.dd.DragTracker\r
+                        */\r
+                       'dragend'\r
+               );\r
+\r
+        if(this.vertical){\r
+            Ext.apply(this, Ext.Slider.Vertical);\r
+        }\r
+    },\r
+\r
+       // private override\r
+    onRender : function(){\r
+        this.autoEl = {\r
+            cls: 'x-slider ' + (this.vertical ? 'x-slider-vert' : 'x-slider-horz'),\r
+            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
+        };\r
+        Ext.Slider.superclass.onRender.apply(this, arguments);\r
+        this.endEl = this.el.first();\r
+        this.innerEl = this.endEl.first();\r
+        this.thumb = this.innerEl.first();\r
+        this.halfThumb = (this.vertical ? this.thumb.getHeight() : this.thumb.getWidth())/2;\r
+        this.focusEl = this.thumb.next();\r
+        this.initEvents();\r
+    },\r
+\r
+       // private override\r
+    initEvents : function(){\r
+        this.thumb.addClassOnOver('x-slider-thumb-over');\r
+        this.mon(this.el, {\r
+            scope: this,\r
+            mousedown: this.onMouseDown,\r
+            keydown: this.onKeyDown\r
+        });\r
+\r
+        this.focusEl.swallowEvent("click", true);\r
+\r
+        this.tracker = new Ext.dd.DragTracker({\r
+            onBeforeStart: this.onBeforeDragStart.createDelegate(this),\r
+            onStart: this.onDragStart.createDelegate(this),\r
+            onDrag: this.onDrag.createDelegate(this),\r
+            onEnd: this.onDragEnd.createDelegate(this),\r
+            tolerance: 3,\r
+            autoStart: 300\r
+        });\r
+        this.tracker.initEl(this.thumb);\r
+        this.on('beforedestroy', this.tracker.destroy, this.tracker);\r
+    },\r
+\r
+       // private override\r
+    onMouseDown : function(e){\r
+        if(this.disabled) {return;}\r
+        if(this.clickToChange && e.target != this.thumb.dom){\r
+            var local = this.innerEl.translatePoints(e.getXY());\r
+            this.onClickChange(local);\r
+        }\r
+        this.focus();\r
+    },\r
+\r
+       // private\r
+    onClickChange : function(local){\r
+        if(local.top > this.clickRange[0] && local.top < this.clickRange[1]){\r
+            this.setValue(Ext.util.Format.round(this.reverseValue(local.left), this.decimalPrecision), undefined, true);\r
+        }\r
+    },\r
+\r
+       // private\r
+    onKeyDown : function(e){\r
+        if(this.disabled){e.preventDefault();return;}\r
+        var k = e.getKey();\r
+        switch(k){\r
+            case e.UP:\r
+            case e.RIGHT:\r
+                e.stopEvent();\r
+                if(e.ctrlKey){\r
+                    this.setValue(this.maxValue, undefined, true);\r
+                }else{\r
+                    this.setValue(this.value+this.keyIncrement, undefined, true);\r
+                }\r
+            break;\r
+            case e.DOWN:\r
+            case e.LEFT:\r
+                e.stopEvent();\r
+                if(e.ctrlKey){\r
+                    this.setValue(this.minValue, undefined, true);\r
+                }else{\r
+                    this.setValue(this.value-this.keyIncrement, undefined, true);\r
+                }\r
+            break;\r
+            default:\r
+                e.preventDefault();\r
+        }\r
+    },\r
+\r
+       // private\r
+    doSnap : function(value){\r
+        if(!this.increment || this.increment == 1 || !value) {\r
+            return value;\r
+        }\r
+        var newValue = value, inc = this.increment;\r
+        var m = value % inc;\r
+        if(m != 0){\r
+            newValue -= m;\r
+            if(m * 2 > inc){\r
+                newValue += inc;\r
+            }else if(m * 2 < -inc){\r
+                newValue -= inc;\r
+            }\r
+        }\r
+        return newValue.constrain(this.minValue,  this.maxValue);\r
+    },\r
+\r
+       // private\r
+    afterRender : function(){\r
+        Ext.Slider.superclass.afterRender.apply(this, arguments);\r
+        if(this.value !== undefined){\r
+            var v = this.normalizeValue(this.value);\r
+            if(v !== this.value){\r
+                delete this.value;\r
+                this.setValue(v, false);\r
+            }else{\r
+                this.moveThumb(this.translateValue(v), false);\r
+            }\r
+        }\r
+    },\r
+\r
+       // private\r
+    getRatio : function(){\r
+        var w = this.innerEl.getWidth();\r
+        var v = this.maxValue - this.minValue;\r
+        return v == 0 ? w : (w/v);\r
+    },\r
+\r
+       // private\r
+    normalizeValue : function(v){\r
+        v = this.doSnap(v);\r
+        v = Ext.util.Format.round(v, this.decimalPrecision);\r
+        v = v.constrain(this.minValue, this.maxValue);\r
+        return v;\r
+    },\r
+\r
+       /**\r
+        * Programmatically sets the value of the Slider. Ensures that the value is constrained within\r
+        * the minValue and maxValue.\r
+        * @param {Number} value The value to set the slider to. (This will be constrained within minValue and maxValue)\r
+        * @param {Boolean} animate Turn on or off animation, defaults to true\r
+        */\r
+    setValue : function(v, animate, changeComplete){\r
+        v = this.normalizeValue(v);\r
+        if(v !== this.value && this.fireEvent('beforechange', this, v, this.value) !== false){\r
+            this.value = v;\r
+            this.moveThumb(this.translateValue(v), animate !== false);\r
+            this.fireEvent('change', this, v);\r
+            if(changeComplete){\r
+                this.fireEvent('changecomplete', this, v);\r
+            }\r
+        }\r
+    },\r
+\r
+       // private\r
+    translateValue : function(v){\r
+        var ratio = this.getRatio();\r
+        return (v * ratio)-(this.minValue * ratio)-this.halfThumb;\r
+    },\r
+\r
+       reverseValue : function(pos){\r
+        var ratio = this.getRatio();\r
+        return (pos+this.halfThumb+(this.minValue * ratio))/ratio;\r
+    },\r
+\r
+       // private\r
+    moveThumb: function(v, animate){\r
+        if(!animate || this.animate === false){\r
+            this.thumb.setLeft(v);\r
+        }else{\r
+            this.thumb.shift({left: v, stopFx: true, duration:.35});\r
+        }\r
+    },\r
+\r
+       // private\r
+    focus : function(){\r
+        this.focusEl.focus(10);\r
+    },\r
+\r
+       // private\r
+    onBeforeDragStart : function(e){\r
+        return !this.disabled;\r
+    },\r
+\r
+       // private\r
+    onDragStart: function(e){\r
+        this.thumb.addClass('x-slider-thumb-drag');\r
+        this.dragging = true;\r
+        this.dragStartValue = this.value;\r
+        this.fireEvent('dragstart', this, e);\r
+    },\r
+\r
+       // private\r
+    onDrag: function(e){\r
+        var pos = this.innerEl.translatePoints(this.tracker.getXY());\r
+        this.setValue(Ext.util.Format.round(this.reverseValue(pos.left), this.decimalPrecision), false);\r
+        this.fireEvent('drag', this, e);\r
+    },\r
+\r
+       // private\r
+    onDragEnd: function(e){\r
+        this.thumb.removeClass('x-slider-thumb-drag');\r
+        this.dragging = false;\r
+        this.fireEvent('dragend', this, e);\r
+        if(this.dragStartValue != this.value){\r
+            this.fireEvent('changecomplete', this, this.value);\r
+        }\r
+    },\r
+\r
+       // private\r
+    onResize : function(w, h){\r
+        this.innerEl.setWidth(w - (this.el.getPadding('l') + this.endEl.getPadding('r')));\r
+        this.syncThumb();\r
+    },\r
+    \r
+    //private\r
+    onDisable: function(){\r
+        Ext.Slider.superclass.onDisable.call(this);\r
+        this.thumb.addClass(this.disabledClass);\r
+        if(Ext.isIE){\r
+            //IE breaks when using overflow visible and opacity other than 1.\r
+            //Create a place holder for the thumb and display it.\r
+            var xy = this.thumb.getXY();\r
+            this.thumb.hide();\r
+            this.innerEl.addClass(this.disabledClass).dom.disabled = true;\r
+            if (!this.thumbHolder){\r
+                this.thumbHolder = this.endEl.createChild({cls: 'x-slider-thumb ' + this.disabledClass});    \r
+            }\r
+            this.thumbHolder.show().setXY(xy);\r
+        }\r
+    },\r
+    \r
+    //private\r
+    onEnable: function(){\r
+        Ext.Slider.superclass.onEnable.call(this);\r
+        this.thumb.removeClass(this.disabledClass);\r
+        if(Ext.isIE){\r
+            this.innerEl.removeClass(this.disabledClass).dom.disabled = false;\r
+            if (this.thumbHolder){\r
+                this.thumbHolder.hide();\r
+            }\r
+            this.thumb.show();\r
+            this.syncThumb();\r
+        }\r
+    },\r
+    \r
+    /**\r
+     * Synchronizes the thumb position to the proper proportion of the total component width based\r
+     * on the current slider {@link #value}.  This will be called automatically when the Slider\r
+     * is resized by a layout, but if it is rendered auto width, this method can be called from\r
+     * another resize handler to sync the Slider if necessary.\r
+     */\r
+    syncThumb : function(){\r
+        if(this.rendered){\r
+            this.moveThumb(this.translateValue(this.value));\r
+        }\r
+    },\r
+\r
+       /**\r
+        * Returns the current value of the slider\r
+        * @return {Number} The current value of the slider\r
+        */\r
+    getValue : function(){\r
+        return this.value;\r
+    }\r
+});\r
+Ext.reg('slider', Ext.Slider);\r
+\r
+// private class to support vertical sliders\r
+Ext.Slider.Vertical = {\r
+    onResize : function(w, h){\r
+        this.innerEl.setHeight(h - (this.el.getPadding('t') + this.endEl.getPadding('b')));\r
+        this.syncThumb();\r
+    },\r
+\r
+    getRatio : function(){\r
+        var h = this.innerEl.getHeight();\r
+        var v = this.maxValue - this.minValue;\r
+        return h/v;\r
+    },\r
+\r
+    moveThumb: function(v, animate){\r
+        if(!animate || this.animate === false){\r
+            this.thumb.setBottom(v);\r
+        }else{\r
+            this.thumb.shift({bottom: v, stopFx: true, duration:.35});\r
+        }\r
+    },\r
+\r
+    onDrag: function(e){\r
+        var pos = this.innerEl.translatePoints(this.tracker.getXY());\r
+        var bottom = this.innerEl.getHeight()-pos.top;\r
+        this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), false);\r
+        this.fireEvent('drag', this, e);\r
+    },\r
+\r
+    onClickChange : function(local){\r
+        if(local.left > this.clickRange[0] && local.left < this.clickRange[1]){\r
+            var bottom = this.innerEl.getHeight()-local.top;\r
+            this.setValue(this.minValue + Ext.util.Format.round(bottom/this.getRatio(), this.decimalPrecision), undefined, true);\r
+        }\r
+    }\r
+};
\ No newline at end of file