2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
10 * @requires yahoo, dom, event, element
12 * @title ProgressBar Widget
16 var Dom = YAHOO.util.Dom,
19 CLASS_PROGBAR = 'yui-pb',
20 CLASS_MASK = CLASS_PROGBAR + '-mask',
21 CLASS_BAR = CLASS_PROGBAR + '-bar',
22 CLASS_ANIM = CLASS_PROGBAR + '-anim',
23 CLASS_TL = CLASS_PROGBAR + '-tl',
24 CLASS_TR = CLASS_PROGBAR + '-tr',
25 CLASS_BL = CLASS_PROGBAR + '-bl',
26 CLASS_BR = CLASS_PROGBAR + '-br',
28 // Configuration attributes
31 MIN_VALUE = 'minValue',
32 MAX_VALUE = 'maxValue',
35 DIRECTION = 'direction',
36 DIRECTION_LTR = 'ltr',
37 DIRECTION_RTL = 'rtl',
38 DIRECTION_TTB = 'ttb',
39 DIRECTION_BTT = 'btt',
42 ARIA_TEXT_TEMPLATE = 'ariaTextTemplate',
46 PROGRESS = 'progress',
47 COMPLETE = 'complete';
50 * The ProgressBar widget provides an easy way to draw a bar depicting progress of an operation,
51 * a level meter, rating or any such simple linear measure.
52 * It allows for highly customized styles including animation, vertical or horizontal and forward or reverse.
53 * @namespace YAHOO.widget
55 * @extends YAHOO.util.Element
56 * @param oConfigs {object} An object containing any configuration attributes to be set
59 var Prog = function(oConfigs) {
61 Prog.superclass.constructor.call(this, document.createElement('div') , oConfigs);
66 YAHOO.widget.ProgressBar = Prog;
69 * String containing the HTML string which is the basis for the Progress Bar.
71 * @property ProgressBar.MARKUP
80 '"></div><div class="',
84 '"></div><div class="',
86 '"></div><div class="',
88 '"></div><div class="',
94 Lang.extend(Prog, YAHOO.util.Element, {
96 * Initialization code for the widget, separate from the constructor to allow for overriding/patching.
97 * It is called after <a href="#method_initAttributes">initAttributes</a>
100 * @param oConfigs {Object} (Optional) Object literal definition of configuration values.
103 _init: function (oConfigs) {
105 * Fires when the value is about to change. It reports the starting value
108 * @param value {Number} the current (initial) value
110 // No actual creation required, event will be created when subscribed to
111 //this.createEvent(START);
113 * If animation is active, it will trigger several times during the animation providing intermediate values
114 * If animation is not active, it will fire only once providing the end value
117 * @param value{Number} the current, changing value
119 // No actual creation required, event will be created when subscribed to
120 //this.createEvent(PROGRESS);
122 * Fires at the end of the animation or immediately upon changing values if animation is not loaded
125 * @param value {Number} the current (final) value
127 // No actual creation required, event will be created when listened to
128 //this.createEvent(COMPLETE);
133 * Implementation of Element's abstract method. Sets up config values.
135 * @method initAttributes
136 * @param oConfigs {Object} (Optional) Object literal definition of configuration values.
139 initAttributes: function (oConfigs) {
141 Prog.superclass.initAttributes.call(this, oConfigs);
142 this.set('innerHTML',Prog.MARKUP);
143 this.addClass(CLASS_PROGBAR);
145 // I need to apply at least the following styles, if present in oConfigs,
146 // to the ProgressBar so when it later reads the width and height,
147 // they are already set to the correct values.
148 // id is important because it can be used as a CSS selector.
149 var key, presets = ['id',WIDTH,HEIGHT,'class','style'];
150 while((key = presets.pop())) {
151 if (key in oConfigs) {
152 this.set(key,oConfigs[key]);
159 * @description Reference to the HTML object that makes the moving bar (read-only)
160 * @type HTMLElement (div)
163 this.setAttributeConfig(BAR_EL, {
165 value: this.getElementsByClassName(CLASS_BAR)[0]
169 * @description Reference to the HTML object that overlays the bar providing the mask. (read-only)
170 * @type HTMLElement (table)
173 this.setAttributeConfig(MASK_EL, {
175 value: this.getElementsByClassName(CLASS_MASK)[0]
180 * @attribute direction
181 * @description Direction of movement of the bar.
182 * It can be any of 'ltr' (left to right), 'rtl' (the reverse) , 'ttb' (top to bottom) or 'btt'.
183 * Can only be set once and only before rendering.
185 * @type String (any of "ltr", "rtl", "ttb" or "btt")
187 this.setAttributeConfig(DIRECTION, {
189 validator:function(value) {
190 if (this._rendered) { return false; }
201 method: function(value) {
202 this._barSizeFunction = this._barSizeFunctions[this.get(ANIM)?1:0][value];
207 * @attribute maxValue
208 * @description Represents the top value for the bar.
209 * The bar will be fully extended when reaching this value.
210 * Values higher than this will be ignored.
214 this.setAttributeConfig(MAX_VALUE, {
216 validator: Lang.isNumber,
217 method: function (value) {
218 this.get('element').setAttribute('aria-valuemax',value);
219 if (this.get(VALUE) > value) { this.set(VALUE,value); }
224 * @attribute minValue
225 * @description Represents the lowest value for the bar.
226 * The bar will be totally collapsed when reaching this value.
227 * Values lower than this will be ignored.
232 this.setAttributeConfig(MIN_VALUE, {
234 validator: Lang.isNumber,
235 method: function (value) {
236 this.get('element').setAttribute('aria-valuemin',value);
237 if (this.get(VALUE) < value) { this.set(VALUE,value); }
242 * @description Width of the ProgressBar.
243 * If a number, it will be assumed to be in pixels.
244 * If a string it should be a valid setting for the CSS width attribute.
245 * It will always be returned as a string including units.
247 * @type Number or String
250 this.setAttributeConfig(WIDTH, {
252 return this.getStyle(WIDTH);
254 method: this._widthChange
260 * @description Height of the ProgressBar.
261 * If a number, it will be assumed to be in pixels.
262 * If a string it should be a valid setting for the CSS height attribute.
263 * It will always be returned as a string including units.
265 * @type Number or String
267 this.setAttributeConfig(HEIGHT, {
269 return this.getStyle(HEIGHT);
271 method: this._heightChange
277 * @attribute ariaTextTemplate
278 * @description Text to be voiced by screen readers.
279 * The text is processed by <a href="YAHOO.lang.html#method_substitute">YAHOO.lang.substitute</a>.
280 * It can use the placeholders {value}, {minValue} and {maxValue}
284 this.setAttributeConfig(ARIA_TEXT_TEMPLATE, {
290 * @description The value for the bar.
291 * Valid values are in between the minValue and maxValue attributes.
295 this.setAttributeConfig(VALUE, {
297 validator: function(value) {
298 return Lang.isNumber(value) && value >= this.get(MIN_VALUE) && value <= this.get(MAX_VALUE);
300 method: this._valueChange
305 * @description it accepts either a boolean (recommended) or an instance of <a href="YAHOO.util.Anim.html">YAHOO.util.Anim</a>.
306 * If a boolean, it will enable/disable animation creating its own instance of the animation utility.
307 * If given an instance of <a href="YAHOO.util.Anim.html">YAHOO.util.Anim</a> it will use that instance.
308 * The <a href="YAHOO.util.Anim.html">animation</a> utility needs to be loaded.
309 * When read, it returns the instance of the animation utility in use or null if none.
310 * It can be used to set the animation parameters such as easing methods or duration.
312 * @type {boolean} or {instance of YAHOO.util.Anim}
314 this.setAttributeConfig(ANIM, {
315 validator: function(value) {
316 return !!YAHOO.util.Anim;
318 setter: this._animSetter
324 * Renders the ProgressBar into the given container.
325 * If the container has other content, the ProgressBar will be appended to it.
326 * If the second argument is provided, the ProgressBar will be inserted before the given child.
327 * The method is chainable since it returns a reference to this instance.
329 * @param el {HTML Element} HTML element that will contain the ProgressBar
330 * @param before {HTML Element} (optional) If present, the ProgressBar will be inserted before this element.
331 * @return {YAHOO.widget.ProgressBar}
334 render: function(parent,before) {
336 if (this._rendered) { return; }
337 this._rendered = true;
339 var direction = this.get(DIRECTION);
341 // If the developer set a className attribute on initialization,
342 // Element would have wiped out my own classNames
343 // So I need to insist on them, plus add the one for direction.
344 this.addClass(CLASS_PROGBAR);
345 this.addClass(CLASS_PROGBAR + '-' + direction);
347 var container = this.get('element');
348 container.tabIndex = 0;
349 container.setAttribute('role','progressbar');
350 container.setAttribute('aria-valuemin',this.get(MIN_VALUE));
351 container.setAttribute('aria-valuemax',this.get(MAX_VALUE));
353 this.appendTo(parent,before);
356 // I need to use the non-animated bar resizing function for initial redraw
357 this._barSizeFunction = this._barSizeFunctions[0][direction];
359 this._previousValue = this.get(VALUE);
361 // I can now set the correct bar resizer
362 if (this.get(ANIM)) {
363 this._barSizeFunction = this._barSizeFunctions[1][direction];
366 this.on('minValueChange',this.redraw);
367 this.on('maxValueChange',this.redraw);
373 * Recalculates the bar size and position and redraws it
377 redraw: function () {
378 this._recalculateConstants();
379 this._valueChange(this.get(VALUE));
383 * Destroys the ProgressBar, related objects and unsubscribes from all events
387 destroy: function() {
388 this.set(ANIM,false);
389 this.unsubscribeAll();
390 var el = this.get('element');
391 if (el.parentNode) { el.parentNode.removeChild(el); }
394 * The previous value setting for the bar. Used mostly as information to event listeners
395 * @property _previousValue
402 * The actual space (in pixels) available for the bar within the mask (excludes margins)
403 * @property _barSpace
410 * The factor to convert the actual value of the bar into pixels
411 * @property _barSpace
419 * A flag to signal that rendering has already happened
420 * @property _rendered
428 * Function to be used to calculate bar size.
429 * It is picked from <a href="#property_barSizeFunctions">_barSizeFunctions</a>
430 * depending on direction and whether animation is active.
431 * @property _barSizeFunction
436 _barSizeFunction: null,
439 * Method called when the height attribute is changed
440 * @method _heightChange
441 * @param {int or string} value New height, in pixels if int or string including units
445 _heightChange: function(value) {
446 if (Lang.isNumber(value)) {
449 this.setStyle(HEIGHT,value);
455 * Method called when the height attribute is changed
456 * @method _widthChange
457 * @param {int or string} value New width, in pixels if int or string including units
461 _widthChange: function(value) {
462 if (Lang.isNumber(value)) {
465 this.setStyle(WIDTH,value);
471 * Due to rounding differences, some browsers fail to cover the whole area
472 * with the mask quadrants when the width or height is odd. This method
473 * stretches the lower and/or right quadrants to make the difference.
478 _fixEdges:function() {
479 if (!this._rendered || YAHOO.env.ua.ie || YAHOO.env.ua.gecko ) { return; }
480 var maskEl = this.get(MASK_EL),
481 tlEl = Dom.getElementsByClassName(CLASS_TL,undefined,maskEl)[0],
482 trEl = Dom.getElementsByClassName(CLASS_TR,undefined,maskEl)[0],
483 blEl = Dom.getElementsByClassName(CLASS_BL,undefined,maskEl)[0],
484 brEl = Dom.getElementsByClassName(CLASS_BR,undefined,maskEl)[0],
485 newSize = (parseInt(Dom.getStyle(maskEl,HEIGHT),10) -
486 parseInt(Dom.getStyle(tlEl,HEIGHT),10)) + 'px';
488 Dom.setStyle(blEl,HEIGHT,newSize);
489 Dom.setStyle(brEl,HEIGHT,newSize);
490 newSize = (parseInt(Dom.getStyle(maskEl,WIDTH),10) -
491 parseInt(Dom.getStyle(tlEl,WIDTH),10)) + 'px';
492 Dom.setStyle(trEl,WIDTH,newSize);
493 Dom.setStyle(brEl,WIDTH,newSize);
499 * Calculates some auxiliary values to make the rendering faster
500 * @method _recalculateConstants
504 _recalculateConstants: function() {
505 var barEl = this.get(BAR_EL);
507 switch (this.get(DIRECTION)) {
510 this._barSpace = parseInt(this.get(WIDTH),10) -
511 (parseInt(Dom.getStyle(barEl,'marginLeft'),10) || 0) -
512 (parseInt(Dom.getStyle(barEl,'marginRight'),10) || 0);
516 this._barSpace = parseInt(this.get(HEIGHT),10) -
517 (parseInt(Dom.getStyle(barEl,'marginTop'),10) || 0)-
518 (parseInt(Dom.getStyle(barEl,'marginBottom'),10) || 0);
521 this._barFactor = this._barSpace / (this.get(MAX_VALUE) - (this.get(MIN_VALUE) || 0)) || 1;
525 * Called in response to a change in the <a href="#config_anim">anim</a> attribute.
526 * It creates and sets up or destroys the instance of the animation utility that will move the bar
527 * @method _animSetter
531 _animSetter: function (value) {
532 var anim, barEl = this.get(BAR_EL);
534 if (value instanceof YAHOO.util.Anim) {
537 anim = new YAHOO.util.Anim(barEl);
539 anim.onTween.subscribe(this._animOnTween,this,true);
540 anim.onComplete.subscribe(this._animComplete,this,true);
542 // Temporary solution until http://yuilibrary.com/projects/yui2/ticket/2528222 gets solved:
543 var oldSetAttribute = anim.setAttribute,
545 switch(this.get(DIRECTION)) {
547 anim.setAttribute = function(attr , val , unit) {
548 val = Math.round(val);
549 oldSetAttribute.call(this,attr,val,unit);
550 Dom.setStyle(barEl,'top',(pb._barSpace - val) + 'px');
554 anim.setAttribute = function(attr , val , unit) {
555 val = Math.round(val);
556 oldSetAttribute.call(this,attr,val,unit);
557 Dom.setStyle(barEl,'left',(pb._barSpace - val) + 'px');
564 anim = this.get(ANIM);
566 anim.onTween.unsubscribeAll();
567 anim.onComplete.unsubscribeAll();
571 this._barSizeFunction = this._barSizeFunctions[anim?1:0][this.get(DIRECTION)];
575 _animComplete: function() {
576 var value = this.get(VALUE);
577 this._previousValue = value;
578 this.fireEvent(PROGRESS,value);
579 this.fireEvent(COMPLETE, value);
580 Dom.removeClass(this.get(BAR_EL),CLASS_ANIM);
582 _animOnTween:function (name,oArgs) {
583 var value = Math.floor(this._tweenFactor * oArgs[0].currentFrame + this._previousValue);
584 this.fireEvent(PROGRESS,value);
588 * Called in response to a change in the <a href="#config_value">value</a> attribute.
589 * Moves the bar to reflect the new value
590 * @method _valueChange
594 _valueChange: function (value) {
595 var anim = this.get(ANIM),
596 pixelValue = Math.floor((value - this.get(MIN_VALUE)) * this._barFactor),
597 barEl = this.get(BAR_EL);
599 this._setAriaText(value);
600 if (this._rendered) {
603 if (anim.isAnimated()) { anim._onComplete.fire(); } // see: http://yuilibrary.com/projects/yui2/ticket/2528217
605 this.fireEvent(START,this._previousValue);
606 this._barSizeFunction(value, pixelValue, barEl, anim);
611 * Utility method to set the ARIA value attributes
612 * @method _setAriaText
616 _setAriaText: function(value) {
618 var container = this.get('element'),
619 text = Lang.substitute(this.get(ARIA_TEXT_TEMPLATE),{
621 minValue:this.get(MIN_VALUE),
622 maxValue:this.get(MAX_VALUE)
624 container.setAttribute('aria-valuenow',value);
625 container.setAttribute('aria-valuetext',text);
629 * Collection of functions used by to calculate the size of the bar.
630 * One of this will be used depending on direction and whether animation is active.
631 * @property _barSizeFunctions
632 * @type {collection of functions}
636 Prog.prototype._barSizeFunctions = b;
638 b[0][DIRECTION_LTR] = function(value, pixelValue, barEl, anim) {
639 Dom.setStyle(barEl,WIDTH, pixelValue + 'px');
640 this.fireEvent(PROGRESS,value);
641 this.fireEvent(COMPLETE,value);
643 b[0][DIRECTION_RTL] = function(value, pixelValue, barEl, anim) {
644 Dom.setStyle(barEl,WIDTH, pixelValue + 'px');
645 Dom.setStyle(barEl,'left',(this._barSpace - pixelValue) + 'px');
646 this.fireEvent(PROGRESS,value);
647 this.fireEvent(COMPLETE,value);
649 b[0][DIRECTION_TTB] = function(value, pixelValue, barEl, anim) {
650 Dom.setStyle(barEl,HEIGHT, pixelValue + 'px');
651 this.fireEvent(PROGRESS,value);
652 this.fireEvent(COMPLETE,value);
654 b[0][DIRECTION_BTT] = function(value, pixelValue, barEl, anim) {
655 Dom.setStyle(barEl,HEIGHT, pixelValue + 'px');
656 Dom.setStyle(barEl,'top', (this._barSpace - pixelValue) + 'px');
657 this.fireEvent(PROGRESS,value);
658 this.fireEvent(COMPLETE,value);
660 b[1][DIRECTION_LTR] = function(value, pixelValue, barEl, anim) {
661 Dom.addClass(barEl,CLASS_ANIM);
662 this._tweenFactor = (value - this._previousValue) / anim.totalFrames / anim.duration;
663 anim.attributes = {width:{ to: pixelValue }};
666 b[1][DIRECTION_RTL] = b[1][DIRECTION_LTR];
667 b[1][DIRECTION_TTB] = function(value, pixelValue, barEl, anim) {
668 Dom.addClass(barEl,CLASS_ANIM);
669 this._tweenFactor = (value - this._previousValue) / anim.totalFrames / anim.duration;
670 anim.attributes = {height:{to: pixelValue}};
673 b[1][DIRECTION_BTT] = b[1][DIRECTION_TTB];
677 YAHOO.register("progressbar", YAHOO.widget.ProgressBar, {version: "2.8.0r4", build: "2449"});