2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('slider-base', function (Y, NAME) {
11
* Create a sliding value range input visualized as a draggable thumb on a
15
* @submodule slider-base
18
var INVALID_VALUE = Y.Attribute.INVALID_VALUE;
21
* Create a slider to represent an input control capable of representing a
22
* series of intermediate states based on the position of the slider's thumb.
23
* These states are typically aligned to a value algorithm whereby the thumb
24
* position corresponds to a given value. Sliders may be oriented vertically or
25
* horizontally, based on the <code>axis</code> configuration.
29
* @param config {Object} Configuration object
32
function SliderBase() {
33
SliderBase.superclass.constructor.apply( this, arguments );
36
Y.SliderBase = Y.extend( SliderBase, Y.Widget, {
41
* Construction logic executed during Slider instantiation.
46
initializer : function () {
48
* The configured axis, stored for fast lookup since it's a writeOnce
49
* attribute. This is for use by extension classes. For
50
* implementation code, use <code>get( "axis" )</code> for
51
* authoritative source. Never write to this property.
57
this.axis = this.get( 'axis' );
60
* Cached fast access map for DOM properties and attributes that
61
* pertain to accessing dimensional or positioning information
62
* according to the Slider's axis (e.g. "height" vs.
63
* "width"). Extension classes should add to this collection
64
* for axis related strings if necessary.
71
dim : ( this.axis === 'y' ) ? 'height' : 'width',
72
minEdge: ( this.axis === 'y' ) ? 'top' : 'left',
73
maxEdge: ( this.axis === 'y' ) ? 'bottom' : 'right',
74
xyIndex: ( this.axis === 'y' ) ? 1 : 0
78
* Signals that the thumb has moved. Payload includes the thumb's
79
* pixel offset from the top/left edge of the rail, and if triggered by
80
* dragging the thumb, the <code>drag:drag</code> event.
83
* @param event {Event} The event object for the thumbMove with the
84
* following extra properties:
87
* <dd>Pixel offset from top/left of the slider to the new
89
* <dt>ddEvent (deprecated)</dt>
90
* <dd><code>drag:drag</code> event from the thumb</dd>
91
* <dt>originEvent</dt>
92
* <dd><code>drag:drag</code> event from the thumb</dd>
95
this.publish( 'thumbMove', {
96
defaultFn: this._defThumbMoveFn,
102
* Create the DOM structure for the Slider.
107
renderUI : function () {
108
var contentBox = this.get( 'contentBox' );
111
* The Node instance of the Slider's rail element. Do not write to
117
this.rail = this.renderRail();
119
this._uiSetRailLength( this.get( 'length' ) );
122
* The Node instance of the Slider's thumb element. Do not write to
128
this.thumb = this.renderThumb();
130
this.rail.appendChild( this.thumb );
131
// @TODO: insert( contentBox, 'replace' ) or setHTML?
132
contentBox.appendChild( this.rail );
134
// <span class="yui3-slider-x">
135
contentBox.addClass( this.getClassName( this.axis ) );
139
* Creates the Slider rail DOM subtree for insertion into the Slider's
140
* <code>contentBox</code>. Override this method if you want to provide
141
* the rail element (presumably from existing markup).
144
* @return {Node} the rail node subtree
146
renderRail: function () {
147
var minCapClass = this.getClassName( 'rail', 'cap', this._key.minEdge ),
148
maxCapClass = this.getClassName( 'rail', 'cap', this._key.maxEdge );
150
return Y.Node.create(
151
Y.Lang.sub( this.RAIL_TEMPLATE, {
152
railClass : this.getClassName( 'rail' ),
153
railMinCapClass: minCapClass,
154
railMaxCapClass: maxCapClass
159
* Sets the rail length according to the <code>length</code> attribute.
161
* @method _uiSetRailLength
162
* @param length {String} the length to apply to the rail style
165
_uiSetRailLength: function ( length ) {
166
this.rail.setStyle( this._key.dim, length );
170
* Creates the Slider thumb DOM subtree for insertion into the Slider's
171
* rail. Override this method if you want to provide the thumb element
172
* (presumably from existing markup).
174
* @method renderThumb
175
* @return {Node} the thumb node subtree
177
renderThumb: function () {
178
this._initThumbUrl();
180
var imageUrl = this.get( 'thumbUrl' );
182
return Y.Node.create(
183
Y.Lang.sub( this.THUMB_TEMPLATE, {
184
thumbClass : this.getClassName( 'thumb' ),
185
thumbShadowClass: this.getClassName( 'thumb', 'shadow' ),
186
thumbImageClass : this.getClassName( 'thumb', 'image' ),
187
thumbShadowUrl : imageUrl,
188
thumbImageUrl : imageUrl,
189
thumbAriaLabelId: this.getClassName( 'label', Y.guid()) // get unique id for specifying a label for ARIA
194
* Gives focus to the thumb enabling keyboard access after clicking thumb
196
* @method _onThumbClick
199
_onThumbClick : function(e){
205
* Creates the Y.DD.Drag instance used to handle the thumb movement and
206
* binds Slider interaction to the configured value model.
211
bindUI : function () {
213
// Begin keyboard listeners ///////////////////////////////
214
var boundingBox = this.get("boundingBox"), //Y.one('body'),
215
// Looking for a key event which will fire continously across browsers while the key is held down.
216
keyEvent = (!Y.UA.opera) ? "down:" : "press:",
217
// 38, 40 = arrow up/down, 33, 34 = page up/down, 35 , 36 = end/home
218
keyEventSpec = keyEvent + "38,40,33,34,35,36",
219
// 37 , 39 = arrow left/right
220
keyLeftRightSpec = keyEvent + "37,39",
221
// 37 , 39 = arrow left/right + meta (command/apple key) for mac
222
keyLeftRightSpecMeta = keyEvent + "37+meta,39+meta";
224
boundingBox.on("key", this._onDirectionKey, keyEventSpec, this);
225
boundingBox.on("key", this._onLeftRightKey, keyLeftRightSpec, this);
226
boundingBox.on("key", this._onLeftRightKeyMeta, keyLeftRightSpecMeta, this);
227
// End keyboard listeners //////////////////////////////////
229
this.thumb.on('click', this._onThumbClick, this);
233
this._bindValueLogic();
235
this.after( 'disabledChange', this._afterDisabledChange );
236
this.after( 'lengthChange', this._afterLengthChange );
241
* increments Slider value by a minor increment
246
_incrMinor : function(){
247
this.set('value', (this.get('value') + this.get('minorStep')));
251
* decrements Slider value by a minor increment
256
_decrMinor : function(){
257
this.set('value', (this.get('value') - this.get('minorStep')));
261
* increments Slider value by a major increment
266
_incrMajor : function(){
267
this.set('value', (this.get('value') + this.get('majorStep')));
271
* decrements Slider value by a major increment
276
_decrMajor : function(){
277
this.set('value', (this.get('value') - this.get('majorStep')));
281
* sets the Slider value to the min value.
286
_setToMin : function(e){
287
this.set('value', this.get('min'));
291
* sets the Slider value to the max value.
296
_setToMax : function(e){
297
this.set('value', this.get('max'));
301
* sets the Slider's value in response to key events.
302
* Left and right keys are in a separate method
303
* in case an implementation wants to increment values
304
* but needs left and right arrow keys for other purposes.
306
* @method _onDirectionKey
307
* @param e {Event} the key event
310
_onDirectionKey : function(e) {
312
if(this.get('disabled') === false){
313
switch (e.charCode) {
329
case 34: // page down
337
* sets the Slider's value in response to left or right key events
339
* @method _onLeftRightKey
340
* @param e {Event} the key event
343
_onLeftRightKey : function(e) {
345
if(this.get('disabled') === false){
346
switch (e.charCode) {
358
* sets the Slider's value in response to left or right key events when a meta (mac command/apple) key is also pressed
360
* @method _onLeftRightKeyMeta
361
* @param e {Event} the key event
364
_onLeftRightKeyMeta : function(e) {
366
if(this.get('disabled') === false){
367
switch (e.charCode) {
368
case 37: // left + meta
371
case 39: // right + meta
383
* Makes the thumb draggable and constrains it to the rail.
385
* @method _bindThumbDD
388
_bindThumbDD: function () {
389
var config = { constrain: this.rail };
391
// { constrain: rail, stickX: true }
392
config[ 'stick' + this.axis.toUpperCase() ] = true;
395
* The DD.Drag instance linked to the thumb node.
401
this._dd = new Y.DD.Drag( {
405
'drag:start': Y.bind( this._onDragStart, this )
408
'drag:drag': Y.bind( this._afterDrag, this ),
409
'drag:end' : Y.bind( this._afterDragEnd, this )
413
// Constrain the thumb to the rail
414
this._dd.plug( Y.Plugin.DDConstrained, config );
418
* Stub implementation. Override this (presumably in a class extension) to
419
* initialize any value logic that depends on the presence of the Drag
422
* @method _bindValueLogic
425
_bindValueLogic: function () {},
428
* Moves the thumb to pixel offset position along the rail.
430
* @method _uiMoveThumb
431
* @param offset {Number} the pixel offset to set as left or top style
432
* @param [options] {Object} Details to send with the `thumbMove` event
435
_uiMoveThumb: function ( offset, options ) {
437
this.thumb.setStyle( this._key.minEdge, offset + 'px' );
439
Y.log("Setting thumb " + this._key.minEdge + " to " + offset + "px","info","slider");
441
options || (options = {});
442
options.offset = offset;
444
this.fire( 'thumbMove', options );
449
* Dispatches the <code>slideStart</code> event.
451
* @method _onDragStart
452
* @param e {Event} the <code>drag:start</code> event from the thumb
455
_onDragStart: function ( e ) {
457
* Signals the beginning of a thumb drag operation. Payload includes
458
* the thumb's drag:start event.
461
* @param event {Event} The event object for the slideStart with the
462
* following extra properties:
464
* <dt>ddEvent (deprecated)</dt>
465
* <dd><code>drag:start</code> event from the thumb</dd>
466
* <dt>originEvent</dt>
467
* <dd><code>drag:start</code> event from the thumb</dd>
470
this.fire('slideStart', {
471
ddEvent: e, // for backward compatibility
477
* Dispatches the <code>thumbMove</code> event.
480
* @param e {Event} the <code>drag:drag</code> event from the thumb
483
_afterDrag: function ( e ) {
484
var thumbXY = e.info.xy[ this._key.xyIndex ],
485
railXY = e.target.con._regionCache[ this._key.minEdge ];
487
Y.log("Thumb position: " + thumbXY + ", Rail position: " + railXY, "info", "slider");
488
this.fire( 'thumbMove', {
489
offset : (thumbXY - railXY),
490
ddEvent: e, // for backward compatibility
496
* Dispatches the <code>slideEnd</code> event.
499
* @param e {Event} the <code>drag:end</code> event from the thumb
502
_afterDragEnd: function ( e ) {
504
* Signals the end of a thumb drag operation. Payload includes
505
* the thumb's drag:end event.
508
* @param event {Event} The event object for the slideEnd with the
509
* following extra properties:
511
* <dt>ddEvent (deprecated)</dt>
512
* <dd><code>drag:end</code> event from the thumb</dd>
513
* <dt>originEvent</dt>
514
* <dd><code>drag:end</code> event from the thumb</dd>
517
this.fire('slideEnd', {
524
* Locks or unlocks the thumb.
526
* @method _afterDisabledChange
527
* @param e {Event} The disabledChange event object
530
_afterDisabledChange: function ( e ) {
531
this._dd.set( 'lock', e.newVal );
535
* Handles changes to the <code>length</code> attribute. By default, it
536
* triggers an update to the UI.
538
* @method _afterLengthChange
539
* @param e {Event} The lengthChange event object
542
_afterLengthChange: function ( e ) {
543
if ( this.get( 'rendered' ) ) {
544
this._uiSetRailLength( e.newVal );
551
* Synchronizes the DOM state with the attribute settings.
555
syncUI : function () {
556
this._dd.con.resetCache();
558
this._syncThumbPosition();
560
// Forces a reflow of the bounding box to address IE8 inline-block
561
// container not expanding correctly. bug 2527905
562
//this.get('boundingBox').toggleClass('');
563
this.thumb.set('aria-valuemin', this.get('min'));
564
this.thumb.set('aria-valuemax', this.get('max'));
566
this._dd.set('lock', this.get('disabled'));
570
* Stub implementation. Override this (presumably in a class extension) to
571
* ensure the thumb is in the correct position according to the value
575
* @method _syncThumbPosition
578
_syncThumbPosition: function () {},
581
* Validates the axis is "x" or "y" (case insensitive).
582
* Converts to lower case for storage.
585
* @param v {String} proposed value for the axis attribute
586
* @return {String} lowercased first character of the input string
589
_setAxis : function (v) {
590
v = ( v + '' ).toLowerCase();
592
return ( v === 'x' || v === 'y' ) ? v : INVALID_VALUE;
596
* <p>Ensures the stored length value is a string with a quantity and unit.
597
* Unit will be defaulted to "px" if not included. Rejects
598
* values less than or equal to 0 and those that don't at least start with
601
* <p>Currently only pixel lengths are supported.</p>
604
* @param v {String} proposed value for the length attribute
605
* @return {String} the sanitized value
608
_setLength: function ( v ) {
609
v = ( v + '' ).toLowerCase();
611
var length = parseFloat( v, 10 ),
612
units = v.replace( /[\d\.\-]/g, '' ) || this.DEF_UNIT;
614
return length > 0 ? ( length + units ) : INVALID_VALUE;
618
* <p>Defaults the thumbURL attribute according to the current skin, or
619
* "sam" if none can be determined. Horizontal Sliders will have
620
* their <code>thumbUrl</code> attribute set to</p>
621
* <p><code>"/<em>configured</em>/<em>yu</em>i/<em>builddi</em>r/slider-base/assets/skins/sam/thumb-x.png"</code></p>
622
* <p>And vertical thumbs will get</p>
623
* <p><code>"/<em>configured</em>/<em>yui</em>/<em>builddir</em>/slider-base/assets/skins/sam/thumb-y.png"</code></p>
625
* @method _initThumbUrl
628
_initThumbUrl: function () {
629
if (!this.get('thumbUrl')) {
630
var skin = this.getSkinName() || 'sam',
631
base = Y.config.base;
633
// Unfortunate hack to avoid requesting image resources from the
634
// combo service. The combo service does not serve images.
635
if (base.indexOf('http://yui.yahooapis.com/combo') === 0) {
636
base = 'http://yui.yahooapis.com/' + Y.version + '/build/';
639
// <img src="/path/to/build/slider-base/assets/skins/sam/thumb-x.png">
640
this.set('thumbUrl', base + 'slider-base/assets/skins/' +
641
skin + '/thumb-' + this.axis + '.png');
647
* Bounding box template that will contain the Slider's DOM subtree. <span>s are used to support inline-block styling.
649
* @property BOUNDING_TEMPLATE
651
* @default <span></span>
653
BOUNDING_TEMPLATE : '<span></span>',
656
* Content box template that will contain the Slider's rail and thumb.
658
* @property CONTENT_TEMPLATE
660
* @default <span></span>
662
CONTENT_TEMPLATE : '<span></span>',
665
* Rail template that will contain the end caps and the thumb.
666
* {placeholder}s are used for template substitution at render time.
668
* @property RAIL_TEMPLATE
670
* @default <span class="{railClass}"><span class="{railMinCapClass}"></span><span class="{railMaxCapClass}"></span></span>
672
RAIL_TEMPLATE : '<span class="{railClass}">' +
673
'<span class="{railMinCapClass}"></span>' +
674
'<span class="{railMaxCapClass}"></span>' +
678
* Thumb template that will contain the thumb image and shadow. <img>
679
* tags are used instead of background images to avoid a flicker bug in IE.
680
* {placeholder}s are used for template substitution at render time.
682
* @property THUMB_TEMPLATE
684
* @default <span class="{thumbClass}" tabindex="-1"><img src="{thumbShadowUrl}" alt="Slider thumb shadow" class="{thumbShadowClass}"><img src="{thumbImageUrl}" alt="Slider thumb" class="{thumbImageClass}"></span>
686
THUMB_TEMPLATE : '<span class="{thumbClass}" aria-labelledby="{thumbAriaLabelId}" aria-valuetext="" aria-valuemax="" aria-valuemin="" aria-valuenow="" role="slider" tabindex="0">' + // keyboard access jeff tabindex="-1"
687
'<img src="{thumbShadowUrl}" ' +
688
'alt="Slider thumb shadow" ' +
689
'class="{thumbShadowClass}">' +
690
'<img src="{thumbImageUrl}" ' +
691
'alt="Slider thumb" ' +
692
'class="{thumbImageClass}">' +
697
// Y.SliderBase static properties
700
* The identity of the widget.
704
* @default 'sliderBase'
712
* Static property used to define the default attribute configuration of
723
* Axis upon which the Slider's thumb moves. "x" for
724
* horizontal, "y" for vertical.
728
* @default "x"
739
* The length of the rail (exclusive of the end caps if positioned by
740
* CSS). This corresponds to the movable range of the thumb.
743
* @type {String | Number} e.g. "200px" or 200
752
* Path to the thumb image. This will be used as both the thumb and
753
* shadow as a sprite. Defaults at render() to thumb-x.png or
754
* thumb-y.png in the skin directory of the current skin.
756
* @attribute thumbUrl
758
* @default thumb-x.png or thumb-y.png in the sam skin directory of the
759
* current build path for Slider
763
validator: Y.Lang.isString
769
}, '3.10.3', {"requires": ["widget", "dd-constrain", "event-key"], "skinnable": true});