2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('slider-base', function(Y) {
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
90 * <dd><code>drag:drag</code> event from the thumb</dd>
93 this.publish( 'thumbMove', {
94 defaultFn: this._defThumbMoveFn,
100 * Create the DOM structure for the Slider.
105 renderUI : function () {
106 var contentBox = this.get( 'contentBox' );
109 * The Node instance of the Slider's rail element. Do not write to
115 this.rail = this.renderRail();
117 this._uiSetRailLength( this.get( 'length' ) );
120 * The Node instance of the Slider's thumb element. Do not write to
126 this.thumb = this.renderThumb();
128 this.rail.appendChild( this.thumb );
129 // @TODO: insert( contentBox, 'replace' ) or setContent?
130 contentBox.appendChild( this.rail );
132 // <span class="yui3-slider-x">
133 contentBox.addClass( this.getClassName( this.axis ) );
137 * Creates the Slider rail DOM subtree for insertion into the Slider's
138 * <code>contentBox</code>. Override this method if you want to provide
139 * the rail element (presumably from existing markup).
142 * @return {Node} the rail node subtree
144 renderRail: function () {
145 var minCapClass = this.getClassName( 'rail', 'cap', this._key.minEdge ),
146 maxCapClass = this.getClassName( 'rail', 'cap', this._key.maxEdge );
148 return Y.Node.create(
149 Y.substitute( this.RAIL_TEMPLATE, {
150 railClass : this.getClassName( 'rail' ),
151 railMinCapClass: minCapClass,
152 railMaxCapClass: maxCapClass
157 * Sets the rail length according to the <code>length</code> attribute.
159 * @method _uiSetRailLength
160 * @param length {String} the length to apply to the rail style
163 _uiSetRailLength: function ( length ) {
164 this.rail.setStyle( this._key.dim, length );
168 * Creates the Slider thumb DOM subtree for insertion into the Slider's
169 * rail. Override this method if you want to provide the thumb element
170 * (presumably from existing markup).
172 * @method renderThumb
173 * @return {Node} the thumb node subtree
175 renderThumb: function () {
176 this._initThumbUrl();
178 var imageUrl = this.get( 'thumbUrl' );
180 return Y.Node.create(
181 Y.substitute( this.THUMB_TEMPLATE, {
182 thumbClass : this.getClassName( 'thumb' ),
183 thumbShadowClass: this.getClassName( 'thumb', 'shadow' ),
184 thumbImageClass : this.getClassName( 'thumb', 'image' ),
185 thumbShadowUrl : imageUrl,
186 thumbImageUrl : imageUrl
191 * Creates the Y.DD.Drag instance used to handle the thumb movement and
192 * binds Slider interaction to the configured value model.
197 bindUI : function () {
200 this._bindValueLogic();
202 this.after( 'disabledChange', this._afterDisabledChange );
203 this.after( 'lengthChange', this._afterLengthChange );
207 * Makes the thumb draggable and constrains it to the rail.
209 * @method _bindThumbDD
212 _bindThumbDD: function () {
213 var config = { constrain: this.rail };
215 // { constrain: rail, stickX: true }
216 config[ 'stick' + this.axis.toUpperCase() ] = true;
219 * The DD.Drag instance linked to the thumb node.
225 this._dd = new Y.DD.Drag( {
229 'drag:start': Y.bind( this._onDragStart, this )
232 'drag:drag': Y.bind( this._afterDrag, this ),
233 'drag:end' : Y.bind( this._afterDragEnd, this )
237 // Constrain the thumb to the rail
238 this._dd.plug( Y.Plugin.DDConstrained, config );
242 * Stub implementation. Override this (presumably in a class extension) to
243 * initialize any value logic that depends on the presence of the Drag
246 * @method _bindValueLogic
249 _bindValueLogic: function () {},
252 * Moves the thumb to pixel offset position along the rail.
254 * @method _uiMoveThumb
255 * @param offset {Number} the pixel offset to set as left or top style
258 _uiMoveThumb: function ( offset ) {
260 this.thumb.setStyle( this._key.minEdge, offset + 'px' );
263 this.fire( 'thumbMove', { offset: offset } );
268 * Dispatches the <code>slideStart</code> event.
270 * @method _onDragStart
271 * @param e {Event} the <code>drag:start</code> event from the thumb
274 _onDragStart: function ( e ) {
276 * Signals the beginning of a thumb drag operation. Payload includes
277 * the thumb's drag:start event.
280 * @param event {Event} The event object for the slideStart with the
281 * following extra properties:
284 * <dd><code>drag:start</code> event from the thumb</dd>
287 this.fire( 'slideStart', { ddEvent: e } );
291 * Dispatches the <code>thumbMove</code> event.
294 * @param e {Event} the <code>drag:drag</code> event from the thumb
297 _afterDrag: function ( e ) {
298 var thumbXY = e.info.xy[ this._key.xyIndex ],
299 railXY = e.target.con._regionCache[ this._key.minEdge ];
301 this.fire( 'thumbMove', {
302 offset : (thumbXY - railXY),
308 * Dispatches the <code>slideEnd</code> event.
311 * @param e {Event} the <code>drag:end</code> event from the thumb
314 _afterDragEnd: function ( e ) {
316 * Signals the end of a thumb drag operation. Payload includes
317 * the thumb's drag:end event.
320 * @param event {Event} The event object for the slideEnd with the
321 * following extra properties:
324 * <dd><code>drag:end</code> event from the thumb</dd>
327 this.fire( 'slideEnd', { ddEvent: e } );
331 * Locks or unlocks the thumb.
333 * @method _afterDisabledChange
334 * @param e {Event} The disabledChange event object
337 _afterDisabledChange: function ( e ) {
338 this._dd.set( 'lock', e.newVal );
342 * Handles changes to the <code>length</code> attribute. By default, it
343 * triggers an update to the UI.
345 * @method _afterLengthChange
346 * @param e {Event} The lengthChange event object
349 _afterLengthChange: function ( e ) {
350 if ( this.get( 'rendered' ) ) {
351 this._uiSetRailLength( e.newVal );
358 * Synchronizes the DOM state with the attribute settings.
362 syncUI : function () {
363 this._dd.con.resetCache();
365 this._syncThumbPosition();
367 // Forces a reflow of the bounding box to address IE8 inline-block
368 // container not expanding correctly. bug 2527905
369 //this.get('boundingBox').toggleClass('');
373 * Stub implementation. Override this (presumably in a class extension) to
374 * ensure the thumb is in the correct position according to the value
378 * @method _syncThumbPosition
381 _syncThumbPosition: function () {},
384 * Validates the axis is "x" or "y" (case insensitive).
385 * Converts to lower case for storage.
388 * @param v {String} proposed value for the axis attribute
389 * @return {String} lowercased first character of the input string
392 _setAxis : function (v) {
393 v = ( v + '' ).toLowerCase();
395 return ( v === 'x' || v === 'y' ) ? v : INVALID_VALUE;
399 * <p>Ensures the stored length value is a string with a quantity and unit.
400 * Unit will be defaulted to "px" if not included. Rejects
401 * values less than or equal to 0 and those that don't at least start with
404 * <p>Currently only pixel lengths are supported.</p>
407 * @param v {String} proposed value for the length attribute
408 * @return {String} the sanitized value
411 _setLength: function ( v ) {
412 v = ( v + '' ).toLowerCase();
414 var length = parseFloat( v, 10 ),
415 units = v.replace( /[\d\.\-]/g, '' ) || this.DEF_UNIT;
417 return length > 0 ? ( length + units ) : INVALID_VALUE;
421 * <p>Defaults the thumbURL attribute according to the current skin, or
422 * "sam" if none can be determined. Horizontal Sliders will have
423 * their <code>thumbUrl</code> attribute set to</p>
424 * <p><code>"/<em>configured</em>/<em>yu</em>i/<em>builddi</em>r/slider/assets/skins/sam/thumb-x.png"</code></p>
425 * <p>And vertical thumbs will get</p>
426 * <p><code>"/<em>configured</em>/<em>yui</em>/<em>builddir</em>/slider/assets/skins/sam/thumb-y.png"</code></p>
428 * @method _initThumbUrl
431 _initThumbUrl: function () {
432 if (!this.get('thumbUrl')) {
433 var skin = this.getSkinName() || 'sam',
434 base = Y.config.base;
436 // Unfortunate hack to avoid requesting image resources from the
437 // combo service. The combo service does not serve images.
438 if (base.indexOf('http://yui.yahooapis.com/combo') === 0) {
439 base = 'http://yui.yahooapis.com/' + Y.version + '/build/';
442 // <img src="/path/to/build/slider/assets/skins/sam/thumb-x.png">
443 this.set('thumbUrl', base + 'slider/assets/skins/' +
444 skin + '/thumb-' + this.axis + '.png');
450 * Bounding box template that will contain the Slider's DOM subtree. <span>s are used to support inline-block styling.
452 * @property BOUNDING_TEMPLATE
454 * @default <span></span>
456 BOUNDING_TEMPLATE : '<span></span>',
459 * Content box template that will contain the Slider's rail and thumb.
461 * @property CONTENT_TEMPLATE
463 * @default <span></span>
465 CONTENT_TEMPLATE : '<span></span>',
468 * Rail template that will contain the end caps and the thumb.
469 * {placeholder}s are used for template substitution at render time.
471 * @property RAIL_TEMPLATE
473 * @default <span class="{railClass}"><span class="{railMinCapClass}"></span><span class="{railMaxCapClass}"></span></span>
475 RAIL_TEMPLATE : '<span class="{railClass}">' +
476 '<span class="{railMinCapClass}"></span>' +
477 '<span class="{railMaxCapClass}"></span>' +
481 * Thumb template that will contain the thumb image and shadow. <img>
482 * tags are used instead of background images to avoid a flicker bug in IE.
483 * {placeholder}s are used for template substitution at render time.
485 * @property THUMB_TEMPLATE
487 * @default <span class="{thumbClass}" tabindex="-1"><img src="{thumbShadowUrl}" alt="Slider thumb shadow" class="{thumbShadowClass}"><img src="{thumbImageUrl}" alt="Slider thumb" class="{thumbImageClass}"></span>
489 THUMB_TEMPLATE : '<span class="{thumbClass}" tabindex="-1">' +
490 '<img src="{thumbShadowUrl}" ' +
491 'alt="Slider thumb shadow" ' +
492 'class="{thumbShadowClass}">' +
493 '<img src="{thumbImageUrl}" ' +
494 'alt="Slider thumb" ' +
495 'class="{thumbImageClass}">' +
500 // Y.SliderBase static properties
503 * The identity of the widget.
505 * @property SliderBase.NAME
507 * @default 'sliderBase'
515 * Static property used to define the default attribute configuration of
518 * @property SliderBase.ATTRS
526 * Axis upon which the Slider's thumb moves. "x" for
527 * horizontal, "y" for vertical.
531 * @default "x"
542 * The length of the rail (exclusive of the end caps if positioned by
543 * CSS). This corresponds to the movable range of the thumb.
546 * @type {String | Number} e.g. "200px" or 200
555 * Path to the thumb image. This will be used as both the thumb and
556 * shadow as a sprite. Defaults at render() to thumb-x.png or
557 * thumb-y.png in the skin directory of the current skin.
559 * @attribute thumbUrl
561 * @default thumb-x.png or thumb-y.png in the sam skin directory of the
562 * current build path for Slider
566 validator: Y.Lang.isString
572 }, '3.3.0' ,{requires:['widget', 'substitute', 'dd-constrain']});