2 Copyright (c) 2006, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
9 * The Slider component is a UI control that enables the user to adjust
10 * values in a finite range along one or two axes. Typically, the Slider
11 * control is used in a web application as a rich, visual replacement
12 * for an input box that takes a number as input. The Slider control can
13 * also easily accommodate a second dimension, providing x,y output for
14 * a selection point chosen from a rectangular region.
17 * @title Slider Widget
18 * @namespace YAHOO.widget
19 * @requires yahoo,dom,dragdrop,event
24 * A DragDrop implementation that can be used as a background for a
25 * slider. It takes a reference to the thumb instance
26 * so it can delegate some of the events to it. The goal is to make the
27 * thumb jump to the location on the background when the background is
31 * @extends YAHOO.util.DragDrop
32 * @uses YAHOO.util.EventProvider
34 * @param {String} id The id of the element linked to this instance
35 * @param {String} sGroup The group of related DragDrop items
36 * @param {SliderThumb} oThumb The thumb for this slider
37 * @param {String} sType The type of slider (horiz, vert, region)
39 YAHOO
.widget
.Slider = function(sElementId
, sGroup
, oThumb
, sType
) {
41 this.init(sElementId
, sGroup
, true);
42 this.initSlider(sType
);
43 this.initThumb(oThumb
);
48 * Factory method for creating a horizontal slider
49 * @method YAHOO.widget.Slider.getHorizSlider
51 * @param {String} sBGElId the id of the slider's background element
52 * @param {String} sHandleElId the id of the thumb element
53 * @param {int} iLeft the number of pixels the element can move left
54 * @param {int} iRight the number of pixels the element can move right
55 * @param {int} iTickSize optional parameter for specifying that the element
56 * should move a certain number pixels at a time.
57 * @return {Slider} a horizontal slider control
59 YAHOO
.widget
.Slider
.getHorizSlider
=
60 function (sBGElId
, sHandleElId
, iLeft
, iRight
, iTickSize
) {
61 return new YAHOO
.widget
.Slider(sBGElId
, sBGElId
,
62 new YAHOO
.widget
.SliderThumb(sHandleElId
, sBGElId
,
63 iLeft
, iRight
, 0, 0, iTickSize
), "horiz");
67 * Factory method for creating a vertical slider
68 * @method YAHOO.widget.Slider.getVertSlider
70 * @param {String} sBGElId the id of the slider's background element
71 * @param {String} sHandleElId the id of the thumb element
72 * @param {int} iUp the number of pixels the element can move up
73 * @param {int} iDown the number of pixels the element can move down
74 * @param {int} iTickSize optional parameter for specifying that the element
75 * should move a certain number pixels at a time.
76 * @return {Slider} a vertical slider control
78 YAHOO
.widget
.Slider
.getVertSlider
=
79 function (sBGElId
, sHandleElId
, iUp
, iDown
, iTickSize
) {
80 return new YAHOO
.widget
.Slider(sBGElId
, sBGElId
,
81 new YAHOO
.widget
.SliderThumb(sHandleElId
, sBGElId
, 0, 0,
82 iUp
, iDown
, iTickSize
), "vert");
86 * Factory method for creating a slider region like the one in the color
88 * @method YAHOO.widget.Slider.getSliderRegion
90 * @param {String} sBGElId the id of the slider's background element
91 * @param {String} sHandleElId the id of the thumb element
92 * @param {int} iLeft the number of pixels the element can move left
93 * @param {int} iRight the number of pixels the element can move right
94 * @param {int} iUp the number of pixels the element can move up
95 * @param {int} iDown the number of pixels the element can move down
96 * @param {int} iTickSize optional parameter for specifying that the element
97 * should move a certain number pixels at a time.
98 * @return {Slider} a slider region control
100 YAHOO
.widget
.Slider
.getSliderRegion
=
101 function (sBGElId
, sHandleElId
, iLeft
, iRight
, iUp
, iDown
, iTickSize
) {
102 return new YAHOO
.widget
.Slider(sBGElId
, sBGElId
,
103 new YAHOO
.widget
.SliderThumb(sHandleElId
, sBGElId
, iLeft
, iRight
,
104 iUp
, iDown
, iTickSize
), "region");
108 * By default, animation is available if the animation library is detected.
109 * @property YAHOO.widget.Slider.ANIM_AVAIL
113 YAHOO
.widget
.Slider
.ANIM_AVAIL
= true;
115 YAHOO
.extend(YAHOO
.widget
.Slider
, YAHOO
.util
.DragDrop
, {
118 * Initializes the slider. Executed in the constructor
120 * @param {string} sType the type of slider (horiz, vert, region)
122 initSlider: function(sType
) {
125 * The type of the slider (horiz, vert, region)
131 //this.removeInvalidHandleType("A");
133 this.logger
= new YAHOO
.widget
.LogWriter(this.toString());
136 * Event the fires when the value of the control changes. If
137 * the control is animated the event will fire every point
140 * @param {int} newOffset|x the new offset for normal sliders, or the new
141 * x offset for region sliders
142 * @param {int} y the number of pixels the thumb has moved on the y axis
143 * (region sliders only)
145 this.createEvent("change", this);
148 * Event that fires at the beginning of a slider thumb move.
151 this.createEvent("slideStart", this);
154 * Event that fires at the end of a slider thumb move
157 this.createEvent("slideEnd", this);
160 * Overrides the isTarget property in YAHOO.util.DragDrop
164 this.isTarget
= false;
167 * Flag that determines if the thumb will animate when moved
171 this.animate
= YAHOO
.widget
.Slider
.ANIM_AVAIL
;
174 * Set to false to disable a background click thumb move
175 * @property backgroundEnabled
178 this.backgroundEnabled
= true;
181 * Adjustment factor for tick animation, the more ticks, the
182 * faster the animation (by default)
183 * @property tickPause
189 * Enables the arrow, home and end keys, defaults to true.
190 * @property enableKeys
193 this.enableKeys
= true;
196 * Specifies the number of pixels the arrow keys will move the slider.
198 * @property keyIncrement
201 this.keyIncrement
= 20;
204 * moveComplete is set to true when the slider has moved to its final
205 * destination. For animated slider, this value can be checked in
206 * the onChange handler to make it possible to execute logic only
207 * when the move is complete rather than at all points along the way.
209 * @property moveComplete
212 this.moveComplete
= true;
215 * If animation is configured, specifies the length of the animation
217 * @property animationDuration
221 this.animationDuration
= 0.2;
225 * Initializes the slider's thumb. Executed in the constructor.
227 * @param {YAHOO.widget.SliderThumb} t the slider thumb
229 initThumb: function(t
) {
234 * A YAHOO.widget.SliderThumb instance that we will use to
235 * reposition the thumb when the background is clicked
237 * @type YAHOO.widget.SliderThumb
240 t
.cacheBetweenDrags
= true;
242 // add handler for the handle onchange event
243 t
.onChange = function() {
244 self
.handleThumbChange();
247 if (t
._isHoriz
&& t
.xTicks
&& t
.xTicks
.length
) {
248 this.tickPause
= Math
.round(360 / t
.xTicks
.length
);
249 } else if (t
.yTicks
&& t
.yTicks
.length
) {
250 this.tickPause
= Math
.round(360 / t
.yTicks
.length
);
253 this.logger
.log("tickPause: " + this.tickPause
);
255 // delegate thumb methods
256 t
.onMouseDown = function () { return self
.focus(); };
257 t
.onMouseUp = function() { self
.thumbMouseUp(); };
258 t
.onDrag = function() { self
.fireEvents(true); };
259 t
.onAvailable = function() { return self
.setStartSliderState(); };
264 * Executed when the slider element is available
265 * @method onAvailable
267 onAvailable: function() {
268 var Event
= YAHOO
.util
.Event
;
269 Event
.on(this.id
, "keydown", this.handleKeyDown
, this, true);
270 Event
.on(this.id
, "keypress", this.handleKeyPress
, this, true);
274 * Executed when a keypress event happens with the control focused.
275 * Prevents the default behavior for navigation keys. The actual
276 * logic for moving the slider thumb in response to a key event
277 * happens in handleKeyDown.
278 * @param {Event} e the keypress event
280 handleKeyPress: function(e
) {
281 if (this.enableKeys
) {
282 var Event
= YAHOO
.util
.Event
;
283 var kc
= Event
.getCharCode(e
);
291 Event
.preventDefault(e
);
299 * Executed when a keydown event happens with the control focused.
300 * Updates the slider value and display when the keypress is an
301 * arrow key, home, or end as long as enableKeys is set to true.
302 * @param {Event} e the keydown event
304 handleKeyDown: function(e
) {
305 if (this.enableKeys
) {
306 var Event
= YAHOO
.util
.Event
;
308 var kc
= Event
.getCharCode(e
), t
=this.thumb
;
309 var h
=this.getXValue(),v
=this.getYValue();
312 var changeValue
= true;
316 case 0x25: h
-= this.keyIncrement
; break;
319 case 0x26: v
-= this.keyIncrement
; break;
322 case 0x27: h
+= this.keyIncrement
; break;
325 case 0x28: v
+= this.keyIncrement
; break;
328 case 0x24: h
= t
.leftConstraint
;
333 case 0x23: h
= t
.rightConstraint
;
334 v
= t
.bottomConstraint
;
337 default: changeValue
= false;
342 this.setRegionValue(h
, v
, true);
344 var newVal
= (t
._isHoriz
) ? h
: v
;
345 this.setValue(newVal
, true);
354 * Initialization that sets up the value offsets once the elements are ready
355 * @method setStartSliderState
357 setStartSliderState: function() {
359 this.logger
.log("Fixing state");
361 this.setThumbCenterPoint();
364 * The basline position of the background element, used
365 * to determine if the background has moved since the last
367 * @property baselinePos
370 this.baselinePos
= YAHOO
.util
.Dom
.getXY(this.getEl());
372 this.thumb
.startOffset
= this.thumb
.getOffsetFromParent(this.baselinePos
);
374 if (this.thumb
._isRegion
) {
375 if (this.deferredSetRegionValue
) {
376 this.setRegionValue
.apply(this, this.deferredSetRegionValue
, true);
377 this.deferredSetRegionValue
= null;
379 this.setRegionValue(0, 0, true, true);
382 if (this.deferredSetValue
) {
383 this.setValue
.apply(this, this.deferredSetValue
, true);
384 this.deferredSetValue
= null;
386 this.setValue(0, true, true);
392 * When the thumb is available, we cache the centerpoint of the element so
393 * we can position the element correctly when the background is clicked
394 * @method setThumbCenterPoint
396 setThumbCenterPoint: function() {
398 var el
= this.thumb
.getEl();
402 * The center of the slider element is stored so we can
403 * place it in the correct position when the background is clicked.
404 * @property thumbCenterPoint
405 * @type {"x": int, "y": int}
407 this.thumbCenterPoint
= {
408 x
: parseInt(el
.offsetWidth
/2, 10),
409 y
: parseInt(el
.offsetHeight
/2, 10)
416 * Locks the slider, overrides YAHOO.util.DragDrop
420 this.logger
.log("locking");
426 * Unlocks the slider, overrides YAHOO.util.DragDrop
430 this.logger
.log("unlocking");
436 * Handles mouseup event on the slider background
437 * @method thumbMouseUp
440 thumbMouseUp: function() {
441 this.logger
.log("bg mouseup");
442 if (!this.isLocked() && !this.moveComplete
) {
449 * Returns a reference to this slider's thumb
451 * @return {SliderThumb} this slider's thumb
453 getThumb: function() {
458 * Try to focus the element when clicked so we can add
459 * accessibility features
464 this.logger
.log("focus");
466 // Focus the background element if possible
467 var el
= this.getEl();
473 // Prevent permission denied unhandled exception in FF that can
474 // happen when setting focus while another element is handling
475 // the blur. @TODO this is still writing to the error log
476 // (unhandled error) in FF1.5 with strict error checking on.
482 if (this.isLocked()) {
491 * Event that fires when the value of the slider has changed
493 * @param {int} firstOffset the number of pixels the thumb has moved
494 * from its start position. Normal horizontal and vertical sliders will only
495 * have the firstOffset. Regions will have both, the first is the horizontal
496 * offset, the second the vertical.
497 * @param {int} secondOffset the y offset for region sliders
498 * @deprecated use instance.subscribe("change") instead
500 onChange: function (firstOffset
, secondOffset
) {
502 this.logger
.log("onChange: " + firstOffset
+ ", " + secondOffset
);
506 * Event that fires when the at the beginning of the slider thumb move
507 * @method onSlideStart
508 * @deprecated use instance.subscribe("slideStart") instead
510 onSlideStart: function () {
512 this.logger
.log("onSlideStart");
516 * Event that fires at the end of a slider thumb move
517 * @method onSliderEnd
518 * @deprecated use instance.subscribe("slideEnd") instead
520 onSlideEnd: function () {
522 this.logger
.log("onSlideEnd");
526 * Returns the slider's thumb offset from the start position
528 * @return {int} the current value
530 getValue: function () {
531 return this.thumb
.getValue();
535 * Returns the slider's thumb X offset from the start position
537 * @return {int} the current horizontal offset
539 getXValue: function () {
540 return this.thumb
.getXValue();
544 * Returns the slider's thumb Y offset from the start position
546 * @return {int} the current vertical offset
548 getYValue: function () {
549 return this.thumb
.getYValue();
553 * Internal handler for the slider thumb's onChange event
554 * @method handleThumbChange
557 handleThumbChange: function () {
560 t
.onChange(t
.getXValue(), t
.getYValue());
561 this.fireEvent("change", { x
: t
.getXValue(), y
: t
.getYValue() } );
563 t
.onChange(t
.getValue());
564 this.fireEvent("change", t
.getValue());
570 * Provides a way to set the value of the slider in code.
572 * @param {int} newOffset the number of pixels the thumb should be
573 * positioned away from the initial start point
574 * @param {boolean} skipAnim set to true to disable the animation
575 * for this move action (but not others).
576 * @param {boolean} force ignore the locked setting and set value anyway
577 * @return {boolean} true if the move was performed, false if it failed
579 setValue: function(newOffset
, skipAnim
, force
) {
580 this.logger
.log("setValue " + newOffset
);
582 if (!this.thumb
.available
) {
583 this.logger
.log("defer setValue until after onAvailble");
584 this.deferredSetValue
= arguments
;
588 if (this.isLocked() && !force
) {
589 this.logger
.log("Can't set the value, the control is locked");
593 if ( isNaN(newOffset
) ) {
594 this.logger
.log("setValue, Illegal argument: " + newOffset
);
600 this.verifyOffset(true);
603 } else if (t
._isHoriz
) {
605 // this.fireEvent("slideStart");
606 newX
= t
.initPageX
+ newOffset
+ this.thumbCenterPoint
.x
;
607 this.moveThumb(newX
, t
.initPageY
, skipAnim
);
610 // this.fireEvent("slideStart");
611 newY
= t
.initPageY
+ newOffset
+ this.thumbCenterPoint
.y
;
612 this.moveThumb(t
.initPageX
, newY
, skipAnim
);
619 * Provides a way to set the value of the region slider in code.
620 * @method setRegionValue
621 * @param {int} newOffset the number of pixels the thumb should be
622 * positioned away from the initial start point (x axis for region)
623 * @param {int} newOffset2 the number of pixels the thumb should be
624 * positioned away from the initial start point (y axis for region)
625 * @param {boolean} skipAnim set to true to disable the animation
626 * for this move action (but not others).
627 * @param {boolean} force ignore the locked setting and set value anyway
628 * @return {boolean} true if the move was performed, false if it failed
630 setRegionValue: function(newOffset
, newOffset2
, skipAnim
, force
) {
632 if (!this.thumb
.available
) {
633 this.logger
.log("defer setRegionValue until after onAvailble");
634 this.deferredSetRegionValue
= arguments
;
638 if (this.isLocked() && !force
) {
639 this.logger
.log("Can't set the value, the control is locked");
643 if ( isNaN(newOffset
) ) {
644 this.logger
.log("setRegionValue, Illegal argument: " + newOffset
);
651 var newX
= t
.initPageX
+ newOffset
+ this.thumbCenterPoint
.x
;
652 var newY
= t
.initPageY
+ newOffset2
+ this.thumbCenterPoint
.y
;
653 this.moveThumb(newX
, newY
, skipAnim
);
662 * Checks the background position element position. If it has moved from the
663 * baseline position, the constraints for the thumb are reset
664 * @param checkPos {boolean} check the position instead of using cached value
665 * @method verifyOffset
666 * @return {boolean} True if the offset is the same as the baseline.
668 verifyOffset: function(checkPos
) {
670 var newPos
= YAHOO
.util
.Dom
.getXY(this.getEl());
671 //var newPos = [this.initPageX, this.initPageY];
673 this.logger
.log("newPos: " + newPos
, "warn");
675 if (newPos
[0] != this.baselinePos
[0] || newPos
[1] != this.baselinePos
[1]) {
676 this.logger
.log("background moved, resetting constraints");
677 this.thumb
.resetConstraints();
678 this.baselinePos
= newPos
;
686 * Move the associated slider moved to a timeout to try to get around the
687 * mousedown stealing moz does when I move the slider element between the
688 * cursor and the background during the mouseup event
690 * @param {int} x the X coordinate of the click
691 * @param {int} y the Y coordinate of the click
692 * @param {boolean} skipAnim don't animate if the move happend onDrag
695 moveThumb: function(x
, y
, skipAnim
) {
697 // this.logger.log("move thumb", "warn");
703 this.logger
.log("thumb is not available yet, aborting move");
707 this.logger
.log("move thumb, x: " + x
+ ", y: " + y
);
709 // this.verifyOffset();
711 t
.setDelta(this.thumbCenterPoint
.x
, this.thumbCenterPoint
.y
);
713 var _p
= t
.getTargetCoord(x
, y
);
714 var p
= [_p
.x
, _p
.y
];
717 this.fireEvent("slideStart");
719 if (this.animate
&& YAHOO
.widget
.Slider
.ANIM_AVAIL
&& t
._graduated
&& !skipAnim
) {
720 this.logger
.log("graduated");
721 // this.thumb._animating = true;
724 // cache the current thumb pos
725 this.curCoord
= YAHOO
.util
.Dom
.getXY(this.thumb
.getEl());
727 setTimeout( function() { self
.moveOneTick(p
); }, this.tickPause
);
729 } else if (this.animate
&& YAHOO
.widget
.Slider
.ANIM_AVAIL
&& !skipAnim
) {
730 this.logger
.log("animating to " + p
);
732 // this.thumb._animating = true;
735 var oAnim
= new YAHOO
.util
.Motion(
736 t
.id
, { points
: { to
: p
} },
737 this.animationDuration
,
738 YAHOO
.util
.Easing
.easeOut
);
740 oAnim
.onComplete
.subscribe( function() { self
.endMove(); } );
743 t
.setDragElPos(x
, y
);
744 // this.fireEvents();
750 * Move the slider one tick mark towards its final coordinate. Used
751 * for the animation when tick marks are defined
752 * @method moveOneTick
753 * @param {int[]} the destination coordinate
756 moveOneTick: function(finalCoord
) {
758 var t
= this.thumb
, tmp
;
761 // redundant call to getXY since we set the position most of time prior
762 // to getting here. Moved to this.curCoord
763 //var curCoord = YAHOO.util.Dom.getXY(t.getEl());
765 // alignElWithMouse caches position in lastPageX, lastPageY .. doesn't work
766 //var curCoord = [this.lastPageX, this.lastPageY];
768 // var thresh = Math.min(t.tickSize + (Math.floor(t.tickSize/2)), 10);
770 // var thresh = t.tickSize + (Math.floor(t.tickSize/2));
772 var nextCoord
= null;
775 nextCoord
= this._getNextX(this.curCoord
, finalCoord
);
776 var tmpX
= (nextCoord
) ? nextCoord
[0] : this.curCoord
[0];
777 nextCoord
= this._getNextY([tmpX
, this.curCoord
[1]], finalCoord
);
779 } else if (t
._isHoriz
) {
780 nextCoord
= this._getNextX(this.curCoord
, finalCoord
);
782 nextCoord
= this._getNextY(this.curCoord
, finalCoord
);
785 this.logger
.log("moveOneTick: " +
786 " finalCoord: " + finalCoord
+
787 " this.curCoord: " + this.curCoord
+
788 " nextCoord: " + nextCoord
);
792 // cache the position
793 this.curCoord
= nextCoord
;
795 // move to the next coord
796 // YAHOO.util.Dom.setXY(t.getEl(), nextCoord);
798 // var el = t.getEl();
799 // YAHOO.util.Dom.setStyle(el, "left", (nextCoord[0] + this.thumb.deltaSetXY[0]) + "px");
800 // YAHOO.util.Dom.setStyle(el, "top", (nextCoord[1] + this.thumb.deltaSetXY[1]) + "px");
802 this.thumb
.alignElWithMouse(t
.getEl(), nextCoord
[0], nextCoord
[1]);
804 // check if we are in the final position, if not make a recursive call
805 if (!(nextCoord
[0] == finalCoord
[0] && nextCoord
[1] == finalCoord
[1])) {
807 setTimeout(function() { self
.moveOneTick(finalCoord
); },
816 //this.tickPause = Math.round(this.tickPause/2);
820 * Returns the next X tick value based on the current coord and the target coord.
824 _getNextX: function(curCoord
, finalCoord
) {
825 this.logger
.log("getNextX: " + curCoord
+ ", " + finalCoord
);
829 var nextCoord
= null;
830 if (curCoord
[0] > finalCoord
[0]) {
831 thresh
= t
.tickSize
- this.thumbCenterPoint
.x
;
832 tmp
= t
.getTargetCoord( curCoord
[0] - thresh
, curCoord
[1] );
833 nextCoord
= [tmp
.x
, tmp
.y
];
834 } else if (curCoord
[0] < finalCoord
[0]) {
835 thresh
= t
.tickSize
+ this.thumbCenterPoint
.x
;
836 tmp
= t
.getTargetCoord( curCoord
[0] + thresh
, curCoord
[1] );
837 nextCoord
= [tmp
.x
, tmp
.y
];
846 * Returns the next Y tick value based on the current coord and the target coord.
850 _getNextY: function(curCoord
, finalCoord
) {
854 var nextCoord
= null;
856 if (curCoord
[1] > finalCoord
[1]) {
857 thresh
= t
.tickSize
- this.thumbCenterPoint
.y
;
858 tmp
= t
.getTargetCoord( curCoord
[0], curCoord
[1] - thresh
);
859 nextCoord
= [tmp
.x
, tmp
.y
];
860 } else if (curCoord
[1] < finalCoord
[1]) {
861 thresh
= t
.tickSize
+ this.thumbCenterPoint
.y
;
862 tmp
= t
.getTargetCoord( curCoord
[0], curCoord
[1] + thresh
);
863 nextCoord
= [tmp
.x
, tmp
.y
];
872 * Resets the constraints before moving the thumb.
873 * @method b4MouseDown
876 b4MouseDown: function(e
) {
877 this.thumb
.autoOffset();
878 this.thumb
.resetConstraints();
883 * Handles the mousedown event for the slider background
884 * @method onMouseDown
887 onMouseDown: function(e
) {
888 // this.resetConstraints(true);
889 // this.thumb.resetConstraints(true);
891 if (! this.isLocked() && this.backgroundEnabled
) {
892 var x
= YAHOO
.util
.Event
.getPageX(e
);
893 var y
= YAHOO
.util
.Event
.getPageY(e
);
894 this.logger
.log("bg mousedown: " + x
+ "," + y
);
897 this.moveThumb(x
, y
);
903 * Handles the onDrag event for the slider background
907 onDrag: function(e
) {
908 if (! this.isLocked()) {
909 var x
= YAHOO
.util
.Event
.getPageX(e
);
910 var y
= YAHOO
.util
.Event
.getPageY(e
);
911 this.moveThumb(x
, y
, true);
916 * Fired when the slider movement ends
920 endMove: function () {
921 // this._animating = false;
923 this.moveComplete
= true;
928 * Fires the change event if the value has been changed. Ignored if we are in
929 * the middle of an animation as the event will fire when the animation is
932 * @param {boolean} thumbEvent set to true if this event is fired from an event
933 * that occurred on the thumb. If it is, the state of the
934 * thumb dd object should be correct. Otherwise, the event
935 * originated on the background, so the thumb state needs to
936 * be refreshed before proceeding.
939 fireEvents: function (thumbEvent
) {
942 // this.logger.log("FireEvents: " + t._isRegion);
948 if (! this.isLocked()) {
950 this.logger
.log("region");
951 var newX
= t
.getXValue();
952 var newY
= t
.getYValue();
954 if (newX
!= this.previousX
|| newY
!= this.previousY
) {
955 // this.logger.log("Firing onchange");
956 this.onChange(newX
, newY
);
957 this.fireEvent("change", { x
: newX
, y
: newY
});
960 this.previousX
= newX
;
961 this.previousY
= newY
;
964 var newVal
= t
.getValue();
965 if (newVal
!= this.previousVal
) {
966 this.logger
.log("Firing onchange: " + newVal
);
967 this.onChange( newVal
);
968 this.fireEvent("change", newVal
);
970 this.previousVal
= newVal
;
973 if (this.moveComplete
) {
975 this.fireEvent("slideEnd");
976 this.moveComplete
= false;
985 * @return {string} string representation of the instance
987 toString: function () {
988 return ("Slider (" + this.type
+") " + this.id
);
993 YAHOO
.augment(YAHOO
.widget
.Slider
, YAHOO
.util
.EventProvider
);
996 * A drag and drop implementation to be used as the thumb of a slider.
998 * @extends YAHOO.util.DD
1000 * @param {String} id the id of the slider html element
1001 * @param {String} sGroup the group of related DragDrop items
1002 * @param {int} iLeft the number of pixels the element can move left
1003 * @param {int} iRight the number of pixels the element can move right
1004 * @param {int} iUp the number of pixels the element can move up
1005 * @param {int} iDown the number of pixels the element can move down
1006 * @param {int} iTickSize optional parameter for specifying that the element
1007 * should move a certain number pixels at a time.
1009 YAHOO
.widget
.SliderThumb = function(id
, sGroup
, iLeft
, iRight
, iUp
, iDown
, iTickSize
) {
1012 //this.init(id, sGroup);
1013 YAHOO
.widget
.SliderThumb
.superclass
.constructor.call(this, id
, sGroup
);
1016 * The id of the thumbs parent HTML element (the slider background
1018 * @property parentElId
1021 this.parentElId
= sGroup
;
1025 //this.removeInvalidHandleType("A");
1027 this.logger
= new YAHOO
.widget
.LogWriter(this.toString());
1030 * Overrides the isTarget property in YAHOO.util.DragDrop
1031 * @property isTarget
1034 this.isTarget
= false;
1037 * The tick size for this slider
1038 * @property tickSize
1042 this.tickSize
= iTickSize
;
1045 * Informs the drag and drop util that the offsets should remain when
1046 * resetting the constraints. This preserves the slider value when
1047 * the constraints are reset
1048 * @property maintainOffset
1052 this.maintainOffset
= true;
1054 this.initSlider(iLeft
, iRight
, iUp
, iDown
, iTickSize
);
1057 * Turns off the autoscroll feature in drag and drop
1061 this.scroll
= false;
1065 YAHOO
.extend(YAHOO
.widget
.SliderThumb
, YAHOO
.util
.DD
, {
1068 * The (X and Y) difference between the thumb location and its parent
1069 * (the slider background) when the control is instantiated.
1070 * @property startOffset
1076 * Flag used to figure out if this is a horizontal or vertical slider
1077 * @property _isHoriz
1084 * Cache the last value so we can check for change
1085 * @property _prevVal
1092 * The slider is _graduated if there is a tick interval defined
1093 * @property _graduated
1101 * Returns the difference between the location of the thumb and its parent.
1102 * @method getOffsetFromParent
1103 * @param {[int, int]} parentPos Optionally accepts the position of the parent
1106 getOffsetFromParent0: function(parentPos
) {
1107 var myPos
= YAHOO
.util
.Dom
.getXY(this.getEl());
1108 var ppos
= parentPos
|| YAHOO
.util
.Dom
.getXY(this.parentElId
);
1110 return [ (myPos
[0] - ppos
[0]), (myPos
[1] - ppos
[1]) ];
1113 getOffsetFromParent: function(parentPos
) {
1115 var el
= this.getEl();
1117 if (!this.deltaOffset
) {
1119 var myPos
= YAHOO
.util
.Dom
.getXY(el
);
1120 var ppos
= parentPos
|| YAHOO
.util
.Dom
.getXY(this.parentElId
);
1122 var newOffset
= [ (myPos
[0] - ppos
[0]), (myPos
[1] - ppos
[1]) ];
1124 var l
= parseInt( YAHOO
.util
.Dom
.getStyle(el
, "left"), 10 );
1125 var t
= parseInt( YAHOO
.util
.Dom
.getStyle(el
, "top" ), 10 );
1127 var deltaX
= l
- newOffset
[0];
1128 var deltaY
= t
- newOffset
[1];
1130 if (isNaN(deltaX
) || isNaN(deltaY
)) {
1131 this.logger
.log("element does not have a position style def yet");
1133 this.deltaOffset
= [deltaX
, deltaY
];
1137 var newLeft
= parseInt( YAHOO
.util
.Dom
.getStyle(el
, "left"), 10 );
1138 var newTop
= parseInt( YAHOO
.util
.Dom
.getStyle(el
, "top" ), 10 );
1140 newOffset
= [newLeft
+ this.deltaOffset
[0], newTop
+ this.deltaOffset
[1]];
1145 //return [ (myPos[0] - ppos[0]), (myPos[1] - ppos[1]) ];
1149 * Set up the slider, must be called in the constructor of all subclasses
1150 * @method initSlider
1151 * @param {int} iLeft the number of pixels the element can move left
1152 * @param {int} iRight the number of pixels the element can move right
1153 * @param {int} iUp the number of pixels the element can move up
1154 * @param {int} iDown the number of pixels the element can move down
1155 * @param {int} iTickSize the width of the tick interval.
1157 initSlider: function (iLeft
, iRight
, iUp
, iDown
, iTickSize
) {
1160 //document these. new for 0.12.1
1161 this.initLeft
= iLeft
;
1162 this.initRight
= iRight
;
1164 this.initDown
= iDown
;
1166 this.setXConstraint(iLeft
, iRight
, iTickSize
);
1167 this.setYConstraint(iUp
, iDown
, iTickSize
);
1169 if (iTickSize
&& iTickSize
> 1) {
1170 this._graduated
= true;
1173 this._isHoriz
= (iLeft
|| iRight
);
1174 this._isVert
= (iUp
|| iDown
);
1175 this._isRegion
= (this._isHoriz
&& this._isVert
);
1180 * Clear's the slider's ticks
1181 * @method clearTicks
1183 clearTicks: function () {
1184 YAHOO
.widget
.SliderThumb
.superclass
.clearTicks
.call(this);
1186 this._graduated
= false;
1191 * Gets the current offset from the element's start position in
1194 * @return {int} the number of pixels (positive or negative) the
1195 * slider has moved from the start position.
1197 getValue: function () {
1198 if (!this.available
) { return 0; }
1199 var val
= (this._isHoriz
) ? this.getXValue() : this.getYValue();
1200 //this.logger.log("getVal: " + val);
1205 * Gets the current X offset from the element's start position in
1208 * @return {int} the number of pixels (positive or negative) the
1209 * slider has moved horizontally from the start position.
1211 getXValue: function () {
1212 if (!this.available
) { return 0; }
1213 var newOffset
= this.getOffsetFromParent();
1214 return (newOffset
[0] - this.startOffset
[0]);
1218 * Gets the current Y offset from the element's start position in
1221 * @return {int} the number of pixels (positive or negative) the
1222 * slider has moved vertically from the start position.
1224 getYValue: function () {
1225 if (!this.available
) { return 0; }
1226 var newOffset
= this.getOffsetFromParent();
1227 return (newOffset
[1] - this.startOffset
[1]);
1233 * @return {string} string representation of the instance
1235 toString: function () {
1236 return "SliderThumb " + this.id
;
1240 * The onchange event for the handle/thumb is delegated to the YAHOO.widget.Slider
1241 * instance it belongs to.
1245 onChange: function (x
, y
) {
1250 if ("undefined" == typeof YAHOO
.util
.Anim
) {
1251 YAHOO
.widget
.Slider
.ANIM_AVAIL
= false;