📄 slider.js
字号:
* The current value of the min thumb. <strong>read only</strong>. * @property minVal * @type int */ minVal : -1, /** * The current value of the max thumb. <strong>read only</strong>. * @property maxVal * @type int */ maxVal : -1, /** * Pixel distance to maintain between thumbs. * @property minRange * @type int * @default 0 */ minRange : 0, /** * Executed when one of the sliders fires the slideStart event * @method _handleSlideStart * @private */ _handleSlideStart: function(data, slider) { this.fireEvent("slideStart", slider); }, /** * Executed when one of the sliders fires the slideEnd event * @method _handleSlideEnd * @private */ _handleSlideEnd: function(data, slider) { this.fireEvent("slideEnd", slider); }, /** * Overrides the onDrag method for both sliders * @method _handleDrag * @private */ _handleDrag: function(e) { YAHOO.widget.Slider.prototype.onDrag.call(this.activeSlider, e); }, /** * Executed when the min slider fires the change event * @method _handleMinChange * @private */ _handleMinChange: function() { this.activeSlider = this.minSlider; this.updateValue(); }, /** * Executed when the max slider fires the change event * @method _handleMaxChange * @private */ _handleMaxChange: function() { this.activeSlider = this.maxSlider; this.updateValue(); }, /** * Sets the min and max thumbs to new values. * @method setValues * @param min {int} Pixel offset to assign to the min thumb * @param max {int} Pixel offset to assign to the max thumb * @param skipAnim {boolean} (optional) Set to true to skip thumb animation. * Default false * @param force {boolean} (optional) ignore the locked setting and set * value anyway. Default false * @param silent {boolean} (optional) Set to true to skip firing change * events. Default false */ setValues : function (min, max, skipAnim, force, silent) { var mins = this.minSlider, maxs = this.maxSlider, mint = mins.thumb, maxt = maxs.thumb, self = this, done = { min : false, max : false }; // Clear constraints to prevent animated thumbs from prematurely // stopping when hitting a constraint that's moving with the other // thumb. if (mint._isHoriz) { mint.setXConstraint(mint.leftConstraint,maxt.rightConstraint,mint.tickSize); maxt.setXConstraint(mint.leftConstraint,maxt.rightConstraint,maxt.tickSize); } else { mint.setYConstraint(mint.topConstraint,maxt.bottomConstraint,mint.tickSize); maxt.setYConstraint(mint.topConstraint,maxt.bottomConstraint,maxt.tickSize); } // Set up one-time slideEnd callbacks to call updateValue when both // thumbs have been set this._oneTimeCallback(mins,'slideEnd',function () { done.min = true; if (done.max) { self.updateValue(silent); // Clean the slider's slideEnd events on a timeout since this // will be executed from inside the event's fire setTimeout(function () { self._cleanEvent(mins,'slideEnd'); self._cleanEvent(maxs,'slideEnd'); },0); } }); this._oneTimeCallback(maxs,'slideEnd',function () { done.max = true; if (done.min) { self.updateValue(silent); // Clean both sliders' slideEnd events on a timeout since this // will be executed from inside one of the event's fire setTimeout(function () { self._cleanEvent(mins,'slideEnd'); self._cleanEvent(maxs,'slideEnd'); },0); } }); // Must emit Slider slideEnd event to propagate to updateValue mins.setValue(min,skipAnim,force,false); maxs.setValue(max,skipAnim,force,false); }, /** * Set the min thumb position to a new value. * @method setMinValue * @param min {int} Pixel offset for min thumb * @param skipAnim {boolean} (optional) Set to true to skip thumb animation. * Default false * @param force {boolean} (optional) ignore the locked setting and set * value anyway. Default false * @param silent {boolean} (optional) Set to true to skip firing change * events. Default false */ setMinValue : function (min, skipAnim, force, silent) { var mins = this.minSlider; this.activeSlider = mins; // Use a one-time event callback to delay the updateValue call // until after the slide operation is done var self = this; this._oneTimeCallback(mins,'slideEnd',function () { self.updateValue(silent); // Clean the slideEnd event on a timeout since this // will be executed from inside the event's fire setTimeout(function () { self._cleanEvent(mins,'slideEnd'); }, 0); }); mins.setValue(min, skipAnim, force, silent); }, /** * Set the max thumb position to a new value. * @method setMaxValue * @param max {int} Pixel offset for max thumb * @param skipAnim {boolean} (optional) Set to true to skip thumb animation. * Default false * @param force {boolean} (optional) ignore the locked setting and set * value anyway. Default false * @param silent {boolean} (optional) Set to true to skip firing change * events. Default false */ setMaxValue : function (max, skipAnim, force, silent) { var maxs = this.maxSlider; this.activeSlider = maxs; // Use a one-time event callback to delay the updateValue call // until after the slide operation is done var self = this; this._oneTimeCallback(maxs,'slideEnd',function () { self.updateValue(silent); // Clean the slideEnd event on a timeout since this // will be executed from inside the event's fire setTimeout(function () { self._cleanEvent(maxs,'slideEnd'); }, 0); }); maxs.setValue(max, skipAnim, force, silent); }, /** * Executed when one of the sliders is moved * @method updateValue * @param silent {boolean} (optional) Set to true to skip firing change * events. Default false * @private */ updateValue: function(silent) { var min = this.minSlider.getValue(), max = this.maxSlider.getValue(), changed = false; if (min != this.minVal || max != this.maxVal) { changed = true; var mint = this.minSlider.thumb, maxt = this.maxSlider.thumb, dim = this.isHoriz ? 'x' : 'y'; var thumbInnerWidth = this.minSlider.thumbCenterPoint[dim] + this.maxSlider.thumbCenterPoint[dim]; // Establish barriers within the respective other thumb's edge, less // the minRange. Limit to the Slider's range in the case of // negative minRanges. var minConstraint = Math.max(max-thumbInnerWidth-this.minRange,0); var maxConstraint = Math.min(-min-thumbInnerWidth-this.minRange,0); if (this.isHoriz) { minConstraint = Math.min(minConstraint,maxt.rightConstraint); mint.setXConstraint(mint.leftConstraint,minConstraint, mint.tickSize); maxt.setXConstraint(maxConstraint,maxt.rightConstraint, maxt.tickSize); } else { minConstraint = Math.min(minConstraint,maxt.bottomConstraint); mint.setYConstraint(mint.leftConstraint,minConstraint, mint.tickSize); maxt.setYConstraint(maxConstraint,maxt.bottomConstraint, maxt.tickSize); } } this.minVal = min; this.maxVal = max; if (changed && !silent) { this.fireEvent("change", this); } }, /** * A background click will move the slider thumb nearest to the click. * Override if you need different behavior. * @method selectActiveSlider * @param e {Event} the mousedown event * @private */ selectActiveSlider: function(e) { var min = this.minSlider, max = this.maxSlider, minLocked = min.isLocked(), maxLocked = max.isLocked(), Ev = YAHOO.util.Event, d; if (minLocked || maxLocked) { this.activeSlider = minLocked ? max : min; } else { if (this.isHoriz) { d = Ev.getPageX(e)-min.thumb.initPageX-min.thumbCenterPoint.x; } else { d = Ev.getPageY(e)-min.thumb.initPageY-min.thumbCenterPoint.y; } this.activeSlider = d*2 > max.getValue()+min.getValue() ? max : min; } }, /** * Overrides the onMouseDown for both slider, only moving the active slider * @method handleMouseDown * @private */ _handleMouseDown: function(e) { this.selectActiveSlider(e); YAHOO.widget.Slider.prototype.onMouseDown.call(this.activeSlider, e); }, /** * Schedule an event callback that will execute once, then unsubscribe * itself. * @method _oneTimeCallback * @param o {EventProvider} Object to attach the event to * @param evt {string} Name of the event * @param fn {Function} function to execute once * @private */ _oneTimeCallback : function (o,evt,fn) { o.subscribe(evt,function () { // Unsubscribe myself o.unsubscribe(evt,arguments.callee); // Pass the event handler arguments to the one time callback fn.apply({},[].slice.apply(arguments)); }); }, /** * Clean up the slideEnd event subscribers array, since each one-time * callback will be replaced in the event's subscribers property with * null. This will cause memory bloat and loss of performance. * @method _cleanEvent * @param o {EventProvider} object housing the CustomEvent * @param evt {string} name of the CustomEvent * @private */ _cleanEvent : function (o,evt) { if (o.__yui_events && o.events[evt]) { var ce, i, len; for (i = o.__yui_events.length; i >= 0; --i) { if (o.__yui_events[i].type === evt) { ce = o.__yui_events[i]; break; } } if (ce) { var subs = ce.subscribers, newSubs = [], j = 0; for (i = 0, len = subs.length; i < len; ++i) { if (subs[i]) { newSubs[j++] = subs[i]; } } ce.subscribers = newSubs; } } }};YAHOO.augment(YAHOO.widget.DualSlider, YAHOO.util.EventProvider);/** * Factory method for creating a horizontal dual-thumb slider * @for YAHOO.widget.Slider * @method YAHOO.widget.Slider.getHorizDualSlider * @static * @param {String} bg the id of the slider's background element * @param {String} minthumb the id of the min thumb * @param {String} maxthumb the id of the thumb thumb * @param {int} range the number of pixels the thumbs can move within * @param {int} iTickSize (optional) the element should move this many pixels * at a time * @param {Array} initVals (optional) [min,max] Initial thumb placement * @return {DualSlider} a horizontal dual-thumb slider control */YAHOO.widget.Slider.getHorizDualSlider = function (bg, minthumb, maxthumb, range, iTickSize, initVals) { var mint, maxt; var YW = YAHOO.widget, Slider = YW.Slider, Thumb = YW.SliderThumb; mint = new Thumb(minthumb, bg, 0, range, 0, 0, iTickSize); maxt = new Thumb(maxthumb, bg, 0, range, 0, 0, iTickSize); return new YW.DualSlider(new Slider(bg, bg, mint, "horiz"), new Slider(bg, bg, maxt, "horiz"), range, initVals);};/** * Factory method for creating a vertical dual-thumb slider. * @for YAHOO.widget.Slider * @method YAHOO.widget.Slider.getVertDualSlider * @static * @param {String} bg the id of the slider's background element * @param {String} minthumb the id of the min thumb * @param {String} maxthumb the id of the thumb thumb * @param {int} range the number of pixels the thumbs can move within * @param {int} iTickSize (optional) the element should move this many pixels * at a time * @param {Array} initVals (optional) [min,max] Initial thumb placement * @return {DualSlider} a vertical dual-thumb slider control */YAHOO.widget.Slider.getVertDualSlider = function (bg, minthumb, maxthumb, range, iTickSize, initVals) { var mint, maxt; var YW = YAHOO.widget, Slider = YW.Slider, Thumb = YW.SliderThumb; mint = new Thumb(minthumb, bg, 0, 0, 0, range, iTickSize); maxt = new Thumb(maxthumb, bg, 0, 0, 0, range, iTickSize); return new YW.DualSlider(new Slider(bg, bg, mint, "vert"), new Slider(bg, bg, maxt, "vert"), range, initVals);};YAHOO.register("slider", YAHOO.widget.Slider, {version: "2.6.0", build: "1321"});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -