2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('slider', function(Y) {
10
* Create a sliding value range input visualized as a draggable thumb on a
16
var SLIDER = 'slider',
22
THUMB_IMAGE = 'thumbImage',
23
RAIL_SIZE = 'railSize',
24
CONTENT_BOX = 'contentBox',
26
SLIDE_START = 'slideStart',
27
SLIDE_END = 'slideEnd',
29
THUMB_DRAG = 'thumbDrag',
31
VALUE_SET = 'valueSet',
32
RENDERED = 'rendered',
33
DISABLED = 'disabled',
34
DISABLED_CHANGE = 'disabledChange',
40
COMPLETE = 'complete',
44
isBoolean= L.isBoolean,
45
isString = L.isString,
46
isNumber = L.isNumber,
48
getCN = Y.ClassNameManager.getClassName,
51
C_RAIL = getCN(SLIDER,RAIL),
52
C_THUMB = getCN(SLIDER,THUMB),
53
C_THUMB_IMAGE = getCN(SLIDER,THUMB,IMAGE),
54
C_IMAGE_ERROR = getCN(SLIDER,IMAGE,'error'),
64
* Create a slider to represent an integer value between a given minimum and
69
* @param config {Object} Configuration object
73
Slider.superclass.constructor.apply(this,arguments);
79
* The identity of the widget.
81
* @property Slider.NAME
88
* Object property names used for respective X and Y axis Sliders (e.g.
89
* "left" vs. "top" for placing the thumb according to
90
* its representative value).
92
* @property Slider.AXIS_KEYS
101
eventPageAxis : 'pageX',
109
eventPageAxis : 'pageY',
116
* Static Object hash used to capture existing markup for progressive
117
* enhancement. Keys correspond to config attribute names and values
118
* are selectors used to inspect the contentBox for an existing node
121
* @property Slider.HTML_PARSER
127
thumb : DOT + C_THUMB,
128
thumbImage : DOT + C_THUMB_IMAGE
132
* Static property used to define the default attribute configuration of
135
* @property Slider.ATTRS
142
* Axis upon which the Slider's thumb moves. "x" for
143
* horizontal, "y" for vertical.
147
* @default "x"
153
validator : function (v) {
154
return this._validateNewAxis(v);
157
return this._setAxisFn(v);
162
* Integer value associated with the left or top terminus of the
163
* Slider's rail, depending on the configured axis.
171
validator : function (v) {
172
return this._validateNewMin(v);
177
* Integer value associated with the right or bottom terminus of the
178
* Slider's rail, depending on the configured axis.
186
validator : function (v) {
187
return this._validateNewMax(v);
192
* The current value of the Slider. This value is interpretted into a
193
* position for the thumb along the Slider's rail.
201
validator : function (v) {
202
return this._validateNewValue(v);
205
return this._setValueFn(v);
210
* The Node representing the Slider's rail, usually visualized as a
211
* bar of some sort using a background image, along which the thumb
212
* moves. This Node contains the thumb Node.
220
validator : function (v) {
221
return this._validateNewRail(v);
224
return this._setRailFn(v);
229
* The Node representing the Slider's thumb, usually visualized as a
230
* pointer using a contained image Node (see thumbImage). The current
231
* value of the Slider is calculated from the centerpoint of this
232
* Node in relation to the rail Node. If provided, the thumbImage
233
* Node is contained within this Node.
235
* If no thumbImage is provided and the Node passed as the thumb is an
236
* <code>img</code> element, the assigned Node will be allocated to the
237
* thumbImage and the thumb container defaulted.
245
validator : function (v) {
246
return this._validateNewThumb(v);
249
return this._setThumbFn(v);
254
* The Node representing the image element to use for the Slider's
257
* Alternately, an image URL can be passed and an <code>img</code>
258
* Node will be generated accordingly.
260
* If no thumbImage is provided and the Node passed as the thumb is an
261
* <code>img</code> element, the assigned Node will be allocated to the
262
* thumbImage and the thumb container defaulted.
264
* If thumbImage is provided but its URL resolves to a 404, a default
265
* style will be applied to maintain basic functionality.
267
* @attribute thumbImage
273
validator : function (v) {
274
return this._validateNewThumbImage(v);
277
return this._setThumbImageFn(v);
282
* The width or height of the rail element representing the physical
283
* space along which the thumb can move. CSS size values (e.g. '30em')
284
* accepted but converted to pixels during render.
286
* Alternately, but not recommended, this attribute can be left
287
* unassigned in favor of specifying height or width.
289
* @attribute railSize
295
validator : function (v) {
296
return this._validateNewRailSize(v);
301
* Boolean indicating whether clicking and dragging on the rail will
302
* trigger thumb movement.
304
* @attribute railEnabled
310
validator : isBoolean
315
Y.extend(Slider, Y.Widget, {
318
* Collection of object property names from the appropriate hash set in
328
* Factor used to translate positional coordinates (e.g. left or top) to
329
* the Slider's value.
338
* Pixel dimension of the rail Node's width for X axis Sliders or height
339
* for Y axis Sliders. Used with _factor to calculate positional
340
* coordinates for the thumb.
342
* @property _railSize
349
* Pixel dimension of the thumb Node's width for X axis Sliders or height
350
* for Y axis Sliders. Used with _factor to calculate positional
351
* coordinates for the thumb.
353
* @property _thumbSize
360
* Pixel offset of the point in the thumb element from its top/left edge
361
* to where the value calculation should take place. By default, this is
362
* calculated to half the width of the thumb, causing the value to be
363
* marked from the center of the thumb.
365
* @property _thumbOffset
372
* Object returned from temporary subscription to disabledChange event to
373
* defer setting the disabled state while Slider is loading the thumb
383
* Deferred value for the disabled attribute when stalled (see _stall
386
* @property _disabled
393
* Construction logic executed durint Slider instantiation. Subscribe to
394
* after events for min, max, and railSize. Publish custom events
395
* including slideStart and slideEnd.
397
* @method initializer
400
initializer : function () {
401
this._key = Slider.AXIS_KEYS[this.get('axis')];
403
this.after('minChange', this._afterMinChange);
404
this.after('maxChange', this._afterMaxChange);
406
this.after('railSizeChange', this._afterRailSizeChange);
409
* Signals the beginning of a thumb drag operation. Payload includes
410
* the DD.Drag instance's drag:start event under key ddEvent.
413
* @param event {Event.Facade} An Event Facade object with the following attribute specific properties added:
416
* <dd><code>drag:start</code> event from the managed DD.Drag instance</dd>
419
this.publish(SLIDE_START);
422
* Signals the end of a thumb drag operation. Payload includes
423
* the DD.Drag instance's drag:end event under key ddEvent.
426
* @param event {Event.Facade} An Event Facade object with the following attribute specific properties added:
429
* <dd><code>drag:end</code> event from the managed DD.Drag instance</dd>
432
this.publish(SLIDE_END);
435
* Communicates a request to synchronize the Slider UI with the
436
* attribute state. Links the sync request with the default sync
437
* logic in the default function _defSyncUI.
440
* @param event {Event.Facade} Event Facade object
441
* @preventable _defSyncUI
443
this.publish(SYNC, {defaultFn: this._defSyncUI});
446
* Signals a value change via API, requiring the thumb position to be
447
* updated. Triggers the thumb placement logic in the default function
448
* _defSetThumbPosition.
451
* @param event {Event.Facade} An Event Facade object with the following attribute specific properties added:
454
* <dd><code>valueChange</code> event fired in response to the change in the value attribute</dd>
456
* @preventable _defSetThumbPosition
458
this.publish(VALUE_SET, {defaultFn: this._defSetThumbPosition});
462
* Create the DOM structure for the Slider. Calls _initRail and _initThumb.
467
renderUI : function () {
473
* Creates the rail element if not provided or discovered via HTML_PARSER.
478
_initRail : function () {
479
var cb = this.get(CONTENT_BOX),
480
rail = this.get(RAIL);
482
// Create rail if necessary. Make sure it's in the contentBox
484
rail = cb.appendChild(
485
Y.Node.create('<div class="'+C_RAIL+'"></div>'));
488
} else if (!cb.contains(rail)) {
489
cb.appendChild(rail);
492
rail.addClass(C_RAIL);
493
rail.addClass(this.getClassName(RAIL,this.get('axis')));
497
* Creates the thumb element (not image) if not provided or discovered via
498
* HTML_PARSER. If thumb is present and an <code>img</code> element
499
* <em>and</em> no thumbImage provided, reassigns the thumb element to the
500
* thumbImage and defaults the thumb element as a div.
502
* Makes sure the thumb is a child of the rail element and calls
503
* _initThumbImage if thumbImage is provided.
508
_initThumb : function () {
509
var rail = this.get(RAIL),
510
thumb = this.get(THUMB);
512
// Passed an img element as the thumb
513
if (thumb && !this.get(THUMB_IMAGE) &&
514
thumb.get('nodeName').toLowerCase() === 'img') {
515
this.set(THUMB_IMAGE, thumb);
516
this.set(THUMB,null);
521
thumb = Y.Node.create(
522
'<div class="'+C_THUMB+'"></div>');
524
this.set(THUMB,thumb);
527
thumb.addClass(C_THUMB);
529
if (!rail.contains(thumb)) {
530
rail.appendChild(thumb);
533
if (this.get(THUMB_IMAGE)) {
534
this._initThumbImage();
539
* Ensures the thumbImage is a child of the thumb element.
541
* @method _initThumbImage
544
_initThumbImage : function () {
545
var thumb = this.get(THUMB),
546
img = this.get(THUMB_IMAGE);
549
img.replaceClass(C_THUMB,C_THUMB_IMAGE);
551
if (!thumb.contains(img)) {
552
thumb.appendChild(img);
558
* Calls _bindThumbDD to create the Y.DD instance used to handle the thumb
559
* movement and binds Slider interaction to the configured value model.
564
bindUI : function () {
566
* Communicates user interaction with the thumb. Triggers the logic
567
* to update the value via the default function _defUpdateValueFromDD.
570
* @param event {Event.Facade} An Event Facade object with the following attribute specific properties added:
573
* <dd><code>drag:drag</code> event from the managed DD.Drag instance</dd>
575
* @preventable _defUpdateValueFromDD
577
this.publish(THUMB_DRAG, {defaultFn: this._defUpdateValueFromDD});
581
this.after('valueChange', this._afterValueChange);
582
this.after('thumbImageChange', this._afterThumbImageChange);
583
this.after(DISABLED_CHANGE, this._afterDisabledChange);
587
* Creates the Y.DD instance used to handle the thumb interaction.
589
* @method _bindThumbDD
592
_bindThumbDD : function () {
594
node : this.get(THUMB),
595
constrain2node : this.get(RAIL)
599
ddConf[this._key.ddStick] = true;
601
this._dd = dd = new Y.DD.Drag(ddConf);
602
dd.on('drag:start', Y.bind(this._onDDStartDrag, this));
603
dd.on('drag:drag', Y.bind(this._onDDDrag, this));
604
dd.on('drag:end', Y.bind(this._onDDEndDrag, this));
610
* Subscribes to the rail Node's mousedown event to actuate the thumb when
611
* backgroundEnabled is true.
613
* @method _initRailDD
616
_initRailDD : function () {
617
this.get(RAIL).on('mousedown',Y.bind(this._handleRailMouseDown,this));
621
* Moves the thumb to the mousedown position and hands control over to DD
622
* if the Slider is not disabled and railEnabled is true.
624
* @method _handleRailMouseDown
625
* @param e {Event} Mousedown event facade
628
_handleRailMouseDown : function (e) {
629
if (this.get('railEnabled') && !this.get(DISABLED)) {
631
xyIndex = this._key.xyIndex,
634
if (dd.get('primaryButtonOnly') && e.button > 1) {
635
Y.log('Mousedown was not produced by the primary button',
640
dd._dragThreshMet = true;
642
dd._fixIEMouseDown();
645
Y.DD.DDM.activeDrag = dd;
647
// Adjust registered starting position by half the thumb's x/y
648
xy = dd.get('dragNode').getXY();
649
xy[xyIndex] += this._thumbOffset;
651
dd._setStartPosition(xy);
654
dd._moveNode([e.pageX,e.pageY]);
659
* Synchronizes the DOM state with the attribute settings (most notably
660
* railSize and value). If thumbImage is provided and is still loading,
661
* sync is delayed until it is complete, since the image's dimensions are
662
* taken into consideration for calculations.
666
syncUI : function () {
667
var img = this.get(THUMB_IMAGE);
669
if (this._isImageLoading(img)) {
670
Y.log('Thumb image loading. Scheduling sync.','info','slider');
671
// Schedule the sync for when the image loads/errors
672
this._scheduleSync();
674
Y.log('No thumb image, or image already loaded. Syncing immediately.','info','slider');
675
this._ready(img,!this._isImageLoaded(img));
680
* Binds to the load and error event on the thumbImage to sync the DOM
681
* state with the attribute settings when the image resource is resolved.
682
* The Slider is disabled while it waits.
684
* @method _scheduleSync
687
_scheduleSync : function () {
691
// disable the control until the image is loaded
692
this._disabled = this.get(DISABLED);
693
this.set(DISABLED,true);
694
this._stall = this.on(DISABLED_CHANGE,this._stallDisabledChange);
696
img = this.get(THUMB_IMAGE);
697
handler = Y.bind(this._imageLoaded,this,img);
698
img.on('load', handler);
699
img.on('error',handler);
704
* Method subscribed to the disabledChange event when thumbImage is being
705
* loaded. Prevents manually enabling the Slider until the thumbImage
706
* resource is resolved. Intended value is stored during load and set upon
709
* @method _stallDisabledChange
710
* @param e {Event} Change event for the disabled attribute
713
_stallDisabledChange : function (e) {
714
this._disabled = e.newVal;
719
* Event handler assigned to the thumbImage's load and error event if it
720
* was not loaded prior to instantiation. Calls _ready method and restores
721
* the Slider's disabled attribute.
723
* @method _imageLoaded
724
* @param e {Event} load or error event fired by the thumbImage
725
* @param img {Node} The thumbImage Node
728
_imageLoaded : function (e,img) {
729
var error = (e.type.toLowerCase().indexOf('error') > -1);
732
this._stall.detach();
735
Y.log('Thumb image '+e.type+'ed. Syncing','info','slider');
739
this._ready(img,error);
741
this.set(DISABLED,this._disabled);
745
* Fires the internal sync event, which barring preventDefault should
746
* execute _defSyncUI.
749
* @param img {Node} the thumbImage Node
750
* @param error {Boolean} Indicates an error while loading the thumbImage
753
_ready : function (img,error) {
754
var method = error ? 'addClass' : 'removeClass';
756
// If the thumb image url results in 404, assign a class to provide
757
// default thumb dimensions/UI
758
this.get(CONTENT_BOX)[method](C_IMAGE_ERROR);
764
* The default synchronization behavior, updating the Slider's DOM state to
765
* match the current attribute values.
768
* @param e {Event} Internal sync event
771
_defSyncUI : function (e) {
772
this._uiSetThumbSize();
774
this._setThumbOffset();
776
this._uiSetRailSize();
778
this._setRailOffsetXY();
784
Y.log('placing thumb for value '+this.get(VALUE),'info','slider');
786
this.set(VALUE,this.get(VALUE));
790
* Captures the thumbs pixel height or width, depending on the Slider's
791
* axis, for use in positioning calculations.
793
* @method _uiSetThumbSize
796
_uiSetThumbSize : function () {
797
var thumb = this.get(THUMB),
799
img = this.get(THUMB_IMAGE),
802
// offsetWidth fails in hidden containers
803
size = parseInt(thumb.getComputedStyle(dim),10);
805
Y.log('thumb '+dim+': '+size+'px','info','slider');
807
if (img && this._isImageLoaded(img)) {
808
Y.log('using thumbImage '+dim+' ('+img.get(dim)+') for _thumbSize','info','slider');
813
this._thumbSize = size;
817
* Sets the _thumbOffset property for use in establishing the point in the
818
* thumb that should align to the rail position representing the calculated
821
* @method _setThumbOffset
824
_setThumbOffset : function () {
825
this._thumbOffset = floor(this._thumbSize / 2);
826
Y.log('_thumbOffset calculated to '+this._thumbOffset+'px','info','slider');
830
* Stores the rail Node's pixel height or width, depending on the Slider's
831
* axis, for use in calculating thumb position from the value.
833
* @method _uiSetRailSize
836
_uiSetRailSize : function () {
837
var rail = this.get(RAIL),
838
thumb = this.get(THUMB),
839
img = this.get(THUMB_IMAGE),
841
size = this.get(RAIL_SIZE),
844
if (parseInt(size,10)) {
845
Y.log('railSize provided: '+size,'info','slider');
848
rail.setStyle(dim,size);
849
size = parseInt(rail.getComputedStyle(dim),10);
851
Y.log('pixel '+dim+' of railSize: '+size+'px', 'info', 'slider');
853
Y.log('defaulting railSize from max of computed style and configured '+dim+' attribute value', 'info', 'slider');
854
// Default from height or width (axis respective), or dims assigned
855
// via css to the rail or thumb, whichever is largest.
856
// Dear implementers, please use railSize, not height/width to
858
size = this.get(dim);
859
if (parseInt(size,10)) {
861
rail.setStyle(dim,size);
862
size = parseInt(rail.getComputedStyle(dim),10);
866
parseInt(thumb.getComputedStyle(dim),10),
867
parseInt(rail.getComputedStyle(dim),10));
869
Y.log('pixel '+dim+' of rail: '+size+'px', 'info', 'slider');
871
if (img && this._isImageLoaded(img)) {
872
Y.log('using max of thumbImage '+dim+' ('+img.get(dim)+' and '+size+' for railSize', 'info', 'slider');
874
size = max(img.get(dim),size);
878
rail.setStyle(dim, size + PX);
880
this._railSize = size;
882
// handle the (not recommended) fallback case of setting rail size via
883
// widget height/width params. This is the only case that sets the
884
// off-axis rail dim in the code.
886
dim = this._key.offAxisDim;
887
size = this.get(dim);
895
* Store the current XY position of the rail Node on the page. For use in calculating thumb position from value.
897
* @method _setRailOffsetXY
900
_setRailOffsetXY : function () {
901
this._offsetXY = this.get(RAIL).getXY()[this._key.xyIndex] -
906
* Assigns the gutter attribute to the DD instance to allow the thumb to
907
* overshoot the edges of the rail element up to the _thumbOffset. By
908
* default, this allows the thumb's center point to align with the far left
909
* or top edge of the rail element to represent the min value and the far
910
* right or bottom edge for the max.
912
* @method _setDDGutter
915
_setDDGutter : function () {
916
var gutter = [0,0,0,0],
917
i = this._key.xyIndex,
918
dim = this._thumbOffset,
920
end = -1 * (this._thumbSize - dim);
930
Y.log('setting DD gutter '+gutter.join(' '),'info','slider');
932
this._dd.set('gutter', gutter.join(' '));
936
* Calculates the multiplier used to translate the value into a thumb
942
_setFactor : function () {
943
this._factor = this._railSize ?
944
(this.get(MAX) - this.get(MIN)) / this._railSize :
946
Y.log('_factor set to '+this._factor,'info','slider');
950
* Convenience method for accessing the current value of the Slider.
951
* Equivalent to <code>slider.get("value")</code>.
954
* @return {Number} the value
956
getValue : function () {
957
return this.get(VALUE);
961
* Convenience method for updating the current value of the Slider.
962
* Equivalent to <code>slider.set("value",val)</code>.
965
* @param val {Number} the new value
967
setValue : function (val) {
972
* Validator applied to new values for the axis attribute. Only
973
* "x" and "y" are permitted.
975
* @method _validateNewAxis
976
* @param v {String} proposed value for the axis attribute
980
_validateNewAxis : function (v) {
981
return isString(v) &&
982
v.length === 1 && 'xy'.indexOf(v.toLowerCase()) > -1;
986
* Validator applied to the min attribute. Only numbers are allowed.
988
* @method _validateNewMin
989
* @param v {String} proposed value for the min attribute
993
_validateNewMin : function (v) {
998
* Validator applied to the max attribute. Only numbers are allowed.
1000
* @method _validateNewMax
1001
* @param v {String} proposed value for the max attribute
1005
_validateNewMax : function (v) {
1010
* Validator applied to the value attribute. Only numbers between the min
1011
* and max are allowed.
1013
* @method _validateNewValue
1014
* @param v {String} proposed value for the value attribute
1018
_validateNewValue : function (v) {
1019
var min = this.get(MIN),
1020
max = this.get(MAX);
1022
return isNumber(v) &&
1023
(min < max ? (v >= min && v <= max) : (v >= max && v <= min));
1027
* Validator applied to the rail attribute. Only allows values through
1028
* before the Slider is rendered.
1030
* @method _validateNewRail
1031
* @param v {String} proposed value for the rail attribute
1035
_validateNewRail : function (v) {
1036
return !this.get(RENDERED) || v;
1040
* Validator applied to the thumb attribute. Only allows values through
1041
* before the Slider is rendered.
1043
* @method _validateNewThumb
1044
* @param v {String} proposed value for the thumb attribute
1048
_validateNewThumb : function (v) {
1049
return !this.get(RENDERED) || v;
1053
* Validator applied to the thumbImage attribute. Only allows values through
1054
* before the Slider is rendered.
1056
* @method _validateNewThumbImage
1057
* @param v {String} proposed value for the thumbImage attribute
1061
_validateNewThumbImage : function (v) {
1062
return !this.get(RENDERED) || v;
1066
* Validator applied to the railSize attribute. Only css size values (e.g.
1067
* '200px' are allowed.
1069
* @method _validateNewRailSize
1070
* @param v {String} proposed value for the railSize attribute
1074
_validateNewRailSize : function (v) {
1075
return isString(v) &&
1076
(v === '0' || /^\d+(?:p[xtc]|%|e[mx]|in|[mc]m)$/.test(v));
1080
* Setter applied to the input when updating the railSize attribute.
1082
* @method _setAxisFn
1083
* @param v {String} proposed value for the axis attribute
1084
* @return {String} lowercased first character of the input string
1087
_setAxisFn : function (v) {
1088
return isString(v) ? v.toLowerCase().charAt(0) : null;
1092
* Setter applied to the input when updating the value attribute.
1094
* @method _setValueFn
1095
* @param v {Number} proposed new value for the Slider
1096
* @return {Number} rounded value or configured min if non-number input
1099
_setValueFn : function (v) {
1108
* Setter applied to the input when updating the rail attribute. Input can
1109
* be a Node, raw HTMLElement, or a selector string to locate it.
1111
* @method _setRailFn
1112
* @param v {Node|String|HTMLElement} The rail element Node or selector
1113
* @return {Node} The Node if found. Otherwise null.
1116
_setRailFn : function (v) {
1117
return v ? Y.get(v) : null;
1121
* Setter applied to the input when updating the thumb attribute. Input can
1122
* be a Node, raw HTMLElement, or a selector string to locate it.
1124
* @method _setThumbFn
1125
* @param v {Node|String|HTMLElement} The thumb element Node or selector
1126
* @return {Node} The Node if found. Otherwise null.
1129
_setThumbFn : function (v) {
1130
return v ? Y.get(v) : null;
1134
* Setter applied to the input when updating the thumbImage attribute.
1135
* Input can be a Node, raw HTMLElement, selector string to locate it, or
1136
* the URL for an image resource.
1138
* String input will be treated as a selector. If no element is found using
1139
* the selector, an <code>img</code> Node will be created with the string
1140
* used as the <code>src</code> attribute.
1142
* @method _setThumbImageFn
1143
* @param v {Node|String|HTMLElement} The thumbImage element Node, selector,
1145
* @return {Node} The Node if found or created. Otherwise null.
1148
_setThumbImageFn : function (v) {
1149
return v ? Y.get(v) ||
1150
Y.Node.create('<img src="'+v+'" alt="Slider thumb">') :
1158
* Caches the current page position of the rail element and fires the
1159
* slideStart event in response to the DD's drag:start.
1161
* @method _onDDStartDrag
1162
* @param e {Event} the DD instance's drag:start custom event
1165
_onDDStartDrag : function (e) {
1166
Y.log('slide start','info','slider');
1167
this._setRailOffsetXY();
1168
this.fire(SLIDE_START,{ ddEvent: e });
1172
* Fires the thumbDrag event to queue Slider value update.
1175
* @param e {Event} the DD instance's drag:drag custom event
1178
_onDDDrag : function (e) {
1179
Y.log('thumb drag','info','slider');
1180
this.fire(THUMB_DRAG, { ddEvent: e });
1184
* The default value update behavior in response to Slider thumb
1185
* interaction. Calculates the value using stored offsets, the _factor
1186
* multiplier and the min value.
1188
* @method _defUpdateValueFromDD
1189
* @param e {Event} the internal thumbDrag event
1192
_defUpdateValueFromDD : function (e) {
1193
var before = this.get(VALUE),
1194
val = e.ddEvent[this._key.eventPageAxis] - this._offsetXY;
1196
Y.log('setting value from thumb drag: before('+before+') raw('+val+') factored('+round(this.get(MIN) + (val * this._factor))+')', 'info','slider');
1198
val = round(this.get(MIN) + (val * this._factor));
1200
if (before !== val) {
1201
this.set(VALUE, val, { ddEvent: e.ddEvent });
1206
* Fires the slideEnd event.
1208
* @method _onDDEndDrag
1209
* @param e {Event} the DD instance's drag:end custom event
1212
_onDDEndDrag : function (e) {
1213
Y.log('slide end','info','slider');
1214
this.fire(SLIDE_END,{ ddEvent: e });
1221
* The default behavior for calculating the placement of the thumb in
1222
* response to a value attribute update. This is performed in response
1223
* to firing the internal valueSet event.
1225
* @method _defSetThumbPosition
1226
* @param e {Event} the valueSet custom event
1229
_defSetThumbPosition : function (e) {
1230
var min = this.get(MIN),
1231
max = this.get(MAX),
1232
v = e.changeEv.newVal;
1234
Y.log('setting thumb position from value attribute update ('+v+')', 'info', 'slider');
1236
v = round(((v - min) / (max - min)) * this._railSize);
1238
this._uiPositionThumb(v);
1242
* Places the thumb at a particular X or Y location based on the configured
1245
* @method _uiPositionThumb
1246
* @param xy {Number} the desired left or top pixel position of the thumb
1247
* in relation to the rail Node.
1250
_uiPositionThumb : function (xy) {
1253
xy += this._offsetXY;
1255
dd._setStartPosition(dd.get('dragNode').getXY());
1257
// stickX/stickY config on DD instance will negate off-axis move
1258
dd._moveNode([xy,xy],true);
1264
* Fires the internal valueSet event in response to a change in the value
1267
* @method _afterValueChange
1268
* @param e {Event} valueChange custom event
1271
_afterValueChange : function (e) {
1273
Y.log('firing valueSet to position thumb', 'info', 'slider');
1275
this.fire(VALUE_SET,{ changeEv: e });
1280
* Replaces the thumb Node in response to a change in the thumb attribute.
1281
* This only has effect before the Slider is rendered.
1283
* @method _afterThumbChange
1284
* @param e {Event} thumbChange custom event
1287
_afterThumbChange : function (e) {
1290
if (this.get(RENDERED)) {
1292
e.prevValue.get('parentNode').removeChild(e.prevValue);
1297
thumb = this.get(THUMB);
1298
this._dd.set('node',thumb);
1299
this._dd.set('dragNode',thumb);
1306
* Sets or replaces the thumb's contained <code>img</code> Node with the
1307
* new Node in response to a change in the thumbImage attribute. This only
1308
* has effect before the Slider is rendered.
1310
* @method _afterThumbImageChange
1311
* @param e {Event} thumbImageChange custom event
1314
_afterThumbImageChange : function (e) {
1315
if (this.get(RENDERED)) {
1317
e.prevValue.get('parentNode').removeChild(e.prevValue);
1320
this._initThumbImage();
1327
* Calls syncUI to update the Slider UI in response to change in the min
1330
* @method _afterMinChange
1331
* @param e {Event} minChange custom event
1334
_afterMinChange : function (e) {
1339
* Calls syncUI to update the Slider UI in response to change in the max
1342
* @method _afterMaxChange
1343
* @param e {Event} maxChange custom event
1346
_afterMaxChange : function (e) {
1351
* Calls syncUI to update the Slider UI in response to change in the
1352
* railSize attribute.
1354
* @method _afterRailSizeChange
1355
* @param e {Event} railSizeChange custom event
1358
_afterRailSizeChange : function (e) {
1363
* Locks or unlocks the DD instance in response to a change in the disabled
1366
* @method _afterDisabledChange
1367
* @param e {Event} disabledChange custom event
1370
_afterDisabledChange : function (e) {
1372
this._dd.set('lock',e.newVal);
1377
* Common handler to call syncUI in response to change events that occurred
1378
* after the Slider is rendered.
1381
* @param e {Event} An attribute change event
1384
_refresh : function (e) {
1385
if (e.newVal !== e.prevVal && this.get(RENDERED)) {
1391
* Used to determine if there is a current or pending request for the
1392
* thumbImage resource.
1394
* @method _isImageLoading
1395
* @param img {Node} <code>img</code> Node
1399
_isImageLoading : function (img) {
1400
return img && !img.get(COMPLETE);
1404
* Used to determine if the image resource loaded successfully or there was
1409
* <li>img load error fired xbrowser for image resources not yet resolved</li>
1410
* <li>img.complete reports false in IE for images not yet loaded as well as images that failed to load</li>
1411
* <li>img.complete true && img.naturalWidth == 0 in FF and Safari indicate image failed to load</li>
1412
* <li>img.complete && img.width == 0 in Opera indicates image failed to load</li>
1415
* @method _isImageLoaded
1416
* @param img {Node} <code>img</code> Node
1420
_isImageLoaded : function (img) {
1422
var w = img.get('naturalWidth');
1423
return img.get(COMPLETE) && (!isNumber(w) ? img.get(WIDTH) : w);
1434
}, '3.0.0pr2' ,{requires:['widget','dd-constrain']});