slider.js

来自「国外很不错的一个开源OA系统Group-Office」· JavaScript 代码 · 共 1,114 行 · 第 1/3 页

JS
1,114
字号
/*                                                                                                                                                      Copyright (c) 2006, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 0.12.0*/ /** * The Slider component is a UI control that enables the user to adjust  * values in a finite range along one or two axes. Typically, the Slider  * control is used in a web application as a rich, visual replacement  * for an input box that takes a number as input. The Slider control can  * also easily accommodate a second dimension, providing x,y output for  * a selection point chosen from a rectangular region. * * @module    slider * @title     Slider Widget * @namespace YAHOO.widget * @requires  yahoo,dom,dragdrop,event * @optional  animation *//** * A DragDrop implementation that can be used as a background for a * slider.  It takes a reference to the thumb instance  * so it can delegate some of the events to it.  The goal is to make the  * thumb jump to the location on the background when the background is  * clicked.   * * @class Slider * @extends YAHOO.util.DragDrop * @constructor * @param {String} id     The id of the element linked to this instance * @param {String} sGroup The group of related DragDrop items * @param {String} sType  The type of slider (horiz, vert, region) */YAHOO.widget.Slider = function(sElementId, sGroup, oThumb, sType) {    if (sElementId) {        /**         * The type of the slider (horiz, vert, region)         * @property type         * @type string         */        this.type = sType;        this.init(sElementId, sGroup, true);        //this.removeInvalidHandleType("A");        var self = this;        /**         * Event the fires when the value of the control changes.  If          * the control is animated the event will fire every point         * along the way.         * @event change         * @param {int} new         * @param {int} firstOffset the number of pixels the thumb has moved         * from its start position. Normal horizontal and vertical sliders will only         * have the firstOffset.  Regions will have both, the first is the horizontal         * offset, the second the vertical.         * @param {int} secondOffset the y offset for region sliders         */        this.createEvent("change", this);        /**         * Event that fires at the end of a slider thumb move.         * @event slideStart         */        this.createEvent("slideStart", this);        /**         * Event that fires at the end of a slider thumb move         * @event slideEnd         */        this.createEvent("slideEnd", this);        /**         * A YAHOO.widget.SliderThumb instance that we will use to          * reposition the thumb when the background is clicked         * @property thumb         * @type YAHOO.widget.SliderThumb         */        this.thumb = oThumb;        // add handler for the handle onchange event        oThumb.onChange = function() {             self.handleThumbChange();         };        /**         * Overrides the isTarget property in YAHOO.util.DragDrop         * @property isTarget         * @private         */        this.isTarget = false;            /**         * Flag that determines if the thumb will animate when moved         * @property animate         * @type boolean         */        this.animate = YAHOO.widget.Slider.ANIM_AVAIL;        /**         * Set to false to disable a background click thumb move         * @property backgroundEnabled         * @type boolean         */        this.backgroundEnabled = true;        /**         * Adjustment factor for tick animation, the more ticks, the         * faster the animation (by default)         * @property tickPause         * @type int         */        this.tickPause = 40;        /**         * Enables the arrow, home and end keys, defaults to true.         * @property enableKeys         * @type boolean         */        this.enableKeys = true;        /**         * Specifies the number of pixels the arrow keys will move the slider.         * Default is 25.         * @property keyIncrement         * @type int         */        this.keyIncrement = 20;        /**         * moveComplete is set to true when the slider has moved to its final         * destination.  For animated slider, this value can be checked in          * the onChange handler to make it possible to execute logic only         * when the move is complete rather than at all points along the way.         *         * @property moveComplete         * @type Boolean         */        this.moveComplete = true;        /**         * If animation is configured, specifies the length of the animation         * in seconds.         * @property animationDuration         * @type int         * @default 0.2         */        this.animationDuration = 0.2;        if (oThumb._isHoriz && oThumb.xTicks && oThumb.xTicks.length) {            this.tickPause = Math.round(360 / oThumb.xTicks.length);        } else if (oThumb.yTicks && oThumb.yTicks.length) {            this.tickPause = Math.round(360 / oThumb.yTicks.length);        }        // delegate thumb methods        oThumb.onMouseDown = function () { return self.focus(); };        //oThumb.b4MouseDown = function () { return self.b4MouseDown(); };        // oThumb.lock = function() { self.lock(); };        // oThumb.unlock = function() { self.unlock(); };        oThumb.onMouseUp = function() { self.thumbMouseUp(); };        oThumb.onDrag = function() { self.fireEvents(); };        oThumb.onAvailable = function() { return self.setStartSliderState(); };    }};/** * Factory method for creating a horizontal slider * @method YAHOO.widget.Slider.getHorizSlider * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iLeft the number of pixels the element can move left * @param {int} iRight the number of pixels the element can move right * @param {int} iTickSize optional parameter for specifying that the element  * should move a certain number pixels at a time. * @return {Slider} a horizontal slider control */YAHOO.widget.Slider.getHorizSlider =     function (sBGElId, sHandleElId, iLeft, iRight, iTickSize) {        return new YAHOO.widget.Slider(sBGElId, sBGElId,             new YAHOO.widget.SliderThumb(sHandleElId, sBGElId,                                iLeft, iRight, 0, 0, iTickSize), "horiz");};/** * Factory method for creating a vertical slider * @method YAHOO.widget.Slider.getVertSlider * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iUp the number of pixels the element can move up * @param {int} iDown the number of pixels the element can move down * @param {int} iTickSize optional parameter for specifying that the element  * should move a certain number pixels at a time. * @return {Slider} a vertical slider control */YAHOO.widget.Slider.getVertSlider =     function (sBGElId, sHandleElId, iUp, iDown, iTickSize) {        return new YAHOO.widget.Slider(sBGElId, sBGElId,             new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, 0, 0,                                iUp, iDown, iTickSize), "vert");};/** * Factory method for creating a slider region like the one in the color * picker example * @method YAHOO.widget.Slider.getSliderRegion * @static * @param {String} sBGElId the id of the slider's background element * @param {String} sHandleElId the id of the thumb element * @param {int} iLeft the number of pixels the element can move left * @param {int} iRight the number of pixels the element can move right * @param {int} iUp the number of pixels the element can move up * @param {int} iDown the number of pixels the element can move down * @param {int} iTickSize optional parameter for specifying that the element  * should move a certain number pixels at a time. * @return {Slider} a slider region control */YAHOO.widget.Slider.getSliderRegion =     function (sBGElId, sHandleElId, iLeft, iRight, iUp, iDown, iTickSize) {        return new YAHOO.widget.Slider(sBGElId, sBGElId,             new YAHOO.widget.SliderThumb(sHandleElId, sBGElId, iLeft, iRight,                                iUp, iDown, iTickSize), "region");};/** * By default, animation is available if the animation library is detected. * @property YAHOO.widget.Slider.ANIM_AVAIL * @static * @type boolean */YAHOO.widget.Slider.ANIM_AVAIL = true;YAHOO.extend(YAHOO.widget.Slider, YAHOO.util.DragDrop, {    onAvailable: function() {        var Event = YAHOO.util.Event;        Event.on(this.id, "keydown",  this.handleKeyDown,  this, true);        Event.on(this.id, "keypress", this.handleKeyPress, this, true);    },     handleKeyPress: function(e) {        if (this.enableKeys) {            var Event = YAHOO.util.Event;            var kc = Event.getCharCode(e);            switch (kc) {                case 0x25: // left                case 0x26: // up                case 0x27: // right                case 0x28: // down                case 0x24: // home                case 0x23: // end                    Event.preventDefault(e);                    break;                default:            }        }   },    handleKeyDown: function(e) {        if (this.enableKeys) {            var Event = YAHOO.util.Event;            var kc = Event.getCharCode(e), t=this.thumb;            var h=this.getXValue(),v=this.getYValue();            var horiz = false;            var changeValue = true;            switch (kc) {                // left                case 0x25: h -= this.keyIncrement; break;                // up                case 0x26: v -= this.keyIncrement; break;                // right                case 0x27: h += this.keyIncrement; break;                // down                case 0x28: v += this.keyIncrement; break;                // home                case 0x24: h = t.leftConstraint;                               v = t.topConstraint;                               break;                // end                case 0x23: h = t.rightConstraint;                            v = t.bottomConstraint;                               break;                default:   changeValue = false;            }            if (changeValue) {                if (t._isRegion) {                    this.setRegionValue(h, v, true);                } else {                    var newVal = (t._isHoriz) ? h : v;                    this.setValue(newVal, true);                }                Event.stopEvent(e);            }        }    },    /**     * Initialization that sets up the value offsets once the elements are ready     * @method setSliderStartState     */    setStartSliderState: function() {        this.setThumbCenterPoint();        /**         * The basline position of the background element, used         * to determine if the background has moved since the last         * operation.         * @property baselinePos         * @type [int, int]         */        this.baselinePos = YAHOO.util.Dom.getXY(this.getEl());        this.thumb.startOffset = this.thumb.getOffsetFromParent(this.baselinePos);        if (this.thumb._isRegion) {            if (this.deferredSetRegionValue) {                this.setRegionValue.apply(this, this.deferredSetRegionValue, true);                this.deferredSetRegionValue = null;            } else {                this.setRegionValue(0, 0, true);            }        } else {            if (this.deferredSetValue) {                this.setValue.apply(this, this.deferredSetValue, true);                this.deferredSetValue = null;            } else {                this.setValue(0, true, true);            }        }    },    /**     * When the thumb is available, we cache the centerpoint of the element so     * we can position the element correctly when the background is clicked     * @method setThumbCenterPoint     */    setThumbCenterPoint: function() {        var el = this.thumb.getEl();        if (el) {            /**             * The center of the slider element is stored so we can position              * place it in the correct position when the background is clicked             * @property thumbCenterPoint             * @type {"x": int, "y": int}             */            this.thumbCenterPoint = {                     x: parseInt(el.offsetWidth/2, 10), 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?