2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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',
43 ACC = 'animAcceleration',
44 BG_POSITION = 'background-position',
48 PROGRESS = 'progress',
49 COMPLETE = 'complete';
52 * The ProgressBar widget provides an easy way to draw a bar depicting progress of an operation,
53 * a level meter, rating or any such simple linear measure.
54 * It allows for highly customized styles including animation, vertical or horizontal and forward or reverse.
55 * @namespace YAHOO.widget
57 * @extends YAHOO.util.Element
58 * @param oConfigs {object} An object containing any configuration attributes to be set
61 var Prog = function(oConfigs) {
63 Prog.superclass.constructor.call(this, document.createElement('div') , oConfigs);
68 YAHOO.widget.ProgressBar = Prog;
71 * String containing the HTML string which is the basis for the Progress
72 * Bar. Value is inserted into the DOM with innerHTML.
74 * @property ProgressBar.MARKUP
83 '"></div><div class="',
87 '"></div><div class="',
89 '"></div><div class="',
91 '"></div><div class="',
97 Lang.extend(Prog, YAHOO.util.Element, {
99 * Initialization code for the widget, separate from the constructor to allow for overriding/patching.
100 * It is called after <a href="#method_initAttributes">initAttributes</a>
103 * @param oConfigs {Object} (Optional) Object literal definition of configuration values.
106 _init: function (oConfigs) {
108 * Fires when the value is about to change. It reports the starting value
111 * @param value {Number} the current (initial) value
113 // No actual creation required, event will be created when subscribed to
114 //this.createEvent(START);
116 * If animation is active, it will trigger several times during the animation providing intermediate values
117 * If animation is not active, it will fire only once providing the end value
120 * @param value{Number} the current, changing value
122 // No actual creation required, event will be created when subscribed to
123 //this.createEvent(PROGRESS);
125 * Fires at the end of the animation or immediately upon changing values if animation is not loaded
128 * @param value {Number} the current (final) value
130 // No actual creation required, event will be created when listened to
131 //this.createEvent(COMPLETE);
136 * Implementation of Element's abstract method. Sets up config values.
138 * @method initAttributes
139 * @param oConfigs {Object} (Optional) Object literal definition of configuration values.
142 initAttributes: function (oConfigs) {
144 Prog.superclass.initAttributes.call(this, oConfigs);
145 this.set('innerHTML',Prog.MARKUP);
146 this.addClass(CLASS_PROGBAR);
148 // I need to apply at least the following styles, if present in oConfigs,
149 // to the ProgressBar so when it later reads the width and height,
150 // they are already set to the correct values.
151 // id is important because it can be used as a CSS selector.
152 var key, presets = ['id',WIDTH,HEIGHT,'class','style'];
153 while((key = presets.pop())) {
154 if (key in oConfigs) {
155 this.set(key,oConfigs[key]);
162 * @description Reference to the HTML object that makes the moving bar (read-only)
163 * @type HTMLElement (div)
166 this.setAttributeConfig(BAR_EL, {
168 value: this.getElementsByClassName(CLASS_BAR)[0]
172 * @description Reference to the HTML object that overlays the bar providing the mask. (read-only)
173 * @type HTMLElement (table)
176 this.setAttributeConfig(MASK_EL, {
178 value: this.getElementsByClassName(CLASS_MASK)[0]
183 * @attribute direction
184 * @description Direction of movement of the bar.
185 * It can be any of 'ltr' (left to right), 'rtl' (the reverse) , 'ttb' (top to bottom) or 'btt'.
186 * Can only be set before rendering.
188 * @type String (any of "ltr", "rtl", "ttb" or "btt")
190 this.setAttributeConfig(DIRECTION, {
192 validator:function(value) {
193 if (this._rendered) { return false; }
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 Pixel width of the ProgressBar, i.e., 200 or "200px".
243 * It will always be returned as a string including units.
245 * @type Number or String
248 this.setAttributeConfig(WIDTH, {
250 return this.getStyle(WIDTH);
252 method: this._widthChange
258 * @description Pixel height of the ProgressBar, i.e., 200 or "200px".
259 * It will always be returned as a string including units.
261 * @type Number or String
263 this.setAttributeConfig(HEIGHT, {
265 return this.getStyle(HEIGHT);
267 method: this._heightChange
273 * @attribute ariaTextTemplate
274 * @description Text to be voiced by screen readers.
275 * The text is processed by <a href="YAHOO.lang.html#method_substitute">YAHOO.lang.substitute</a>.
276 * It can use the placeholders {value}, {minValue} and {maxValue}
280 this.setAttributeConfig(ARIA_TEXT_TEMPLATE, {
286 * @description The value for the bar.
287 * Valid values are in between the minValue and maxValue attributes.
291 this.setAttributeConfig(VALUE, {
293 validator: function(value) {
294 return Lang.isNumber(value) && value >= this.get(MIN_VALUE) && value <= this.get(MAX_VALUE);
296 method: this._valueChange
301 * @description It accepts either a boolean (recommended) or an instance of <a href="YAHOO.util.Anim.html">YAHOO.util.Anim</a>.
302 * If a boolean, it will enable/disable animation creating its own instance of the animation utility.
303 * If given an instance of <a href="YAHOO.util.Anim.html">YAHOO.util.Anim</a> it will use that instance.
304 * The <a href="YAHOO.util.Anim.html">animation</a> utility needs to be loaded.
305 * When read, it returns the instance of the animation utility in use or null if none.
306 * It can be used to set the animation parameters such as easing methods or duration.
308 * @type {boolean} or {instance of YAHOO.util.Anim}
310 this.setAttributeConfig(ANIM, {
311 validator: function(value) {
312 return !!YAHOO.util.Anim;
314 setter: this._animSetter
318 * @attribute animAcceleration
319 * @description It accepts a number or null to cancel.
320 * If a number, it is how fast the background image for the bar will move in the
321 * opposite direction to the bar itself. null or 0 means the background won't move.
322 * Negative values will make the background move along the bar.
323 * Only valid with animation active and it requires a suitable background image to make it evident.
325 * @type {number} or {null}
328 this.setAttributeConfig(ACC, {
330 validator: function(value) {
331 return Lang.isNumber(value) || Lang.isNull(value);
333 method: function(value) {
334 this._fixAnim(this.get(ANIM),value);
340 * Renders the ProgressBar into the given container.
341 * If the container has other content, the ProgressBar will be appended to it.
342 * If the second argument is provided, the ProgressBar will be inserted before the given child.
343 * The method is chainable since it returns a reference to this instance.
345 * @param el {HTML Element} HTML element that will contain the ProgressBar
346 * @param before {HTML Element} (optional) If present, the ProgressBar will be inserted before this element.
347 * @return {YAHOO.widget.ProgressBar}
350 render: function(parent, before) {
352 if (this._rendered) { return; }
353 this._rendered = true;
355 var direction = this.get(DIRECTION);
357 // If the developer set a className attribute on initialization,
358 // Element would have wiped out my own classNames
359 // So I need to insist on them, plus add the one for direction.
360 this.addClass(CLASS_PROGBAR);
361 this.addClass(CLASS_PROGBAR + '-' + direction);
363 var container = this.get('element');
364 container.tabIndex = 0;
365 container.setAttribute('role','progressbar');
366 container.setAttribute('aria-valuemin',this.get(MIN_VALUE));
367 container.setAttribute('aria-valuemax',this.get(MAX_VALUE));
369 this.appendTo(parent,before);
373 this._previousValue = this.get(VALUE);
376 this.on('minValueChange',this.redraw);
377 this.on('maxValueChange',this.redraw);
383 * Recalculates the bar size and position and redraws it
385 * @param noAnim {boolean} Don't use animation to redraw
388 redraw: function (noAnim) {
389 this._recalculateConstants();
390 this._valueChange(this.get(VALUE), noAnim);
394 * Destroys the ProgressBar, related objects and unsubscribes from all events
398 destroy: function() {
399 this.set(ANIM,false);
400 this.unsubscribeAll();
401 var el = this.get('element');
402 if (el.parentNode) { el.parentNode.removeChild(el); }
405 * The previous value setting for the bar. Used mostly as information to event listeners
406 * @property _previousValue
413 * The actual space (in pixels) available for the bar within the mask (excludes margins)
414 * @property _barSpace
421 * The factor to convert the actual value of the bar into pixels
422 * @property _barSpace
430 * A flag to signal that rendering has already happened
431 * @property _rendered
440 * Method called when the height attribute is changed
441 * @method _heightChange
442 * @param {int or string} value New height, in pixels if int or string including units
446 _heightChange: function(value) {
447 if (Lang.isNumber(value)) {
450 this.setStyle(HEIGHT,value);
456 * Method called when the height attribute is changed
457 * @method _widthChange
458 * @param {int or string} value New width, in pixels if int or string including units
462 _widthChange: function(value) {
463 if (Lang.isNumber(value)) {
466 this.setStyle(WIDTH,value);
472 * Due to rounding differences, some browsers fail to cover the whole area
473 * with the mask quadrants when the width or height is odd. This method
474 * stretches the lower and/or right quadrants to make the difference.
479 _fixEdges:function() {
480 if (!this._rendered || YAHOO.env.ua.ie || YAHOO.env.ua.gecko ) { return; }
481 var maskEl = this.get(MASK_EL),
482 tlEl = Dom.getElementsByClassName(CLASS_TL,undefined,maskEl)[0],
483 trEl = Dom.getElementsByClassName(CLASS_TR,undefined,maskEl)[0],
484 blEl = Dom.getElementsByClassName(CLASS_BL,undefined,maskEl)[0],
485 brEl = Dom.getElementsByClassName(CLASS_BR,undefined,maskEl)[0],
486 newSize = (parseInt(Dom.getStyle(maskEl,HEIGHT),10) -
487 parseInt(Dom.getStyle(tlEl,HEIGHT),10)) + PX;
489 Dom.setStyle(blEl,HEIGHT,newSize);
490 Dom.setStyle(brEl,HEIGHT,newSize);
491 newSize = (parseInt(Dom.getStyle(maskEl,WIDTH),10) -
492 parseInt(Dom.getStyle(tlEl,WIDTH),10)) + PX;
493 Dom.setStyle(trEl,WIDTH,newSize);
494 Dom.setStyle(brEl,WIDTH,newSize);
500 * Calculates some auxiliary values to make the rendering faster
501 * @method _recalculateConstants
505 _recalculateConstants: function() {
506 var barEl = this.get(BAR_EL);
508 switch (this.get(DIRECTION)) {
511 this._barSpace = parseInt(this.get(WIDTH),10) -
512 (parseInt(Dom.getStyle(barEl,'marginLeft'),10) || 0) -
513 (parseInt(Dom.getStyle(barEl,'marginRight'),10) || 0);
517 this._barSpace = parseInt(this.get(HEIGHT),10) -
518 (parseInt(Dom.getStyle(barEl,'marginTop'),10) || 0)-
519 (parseInt(Dom.getStyle(barEl,'marginBottom'),10) || 0);
522 this._barFactor = this._barSpace / (this.get(MAX_VALUE) - (this.get(MIN_VALUE) || 0)) || 1;
526 * Called in response to a change in the <a href="#config_anim">anim</a> attribute.
527 * It creates and sets up or destroys the instance of the animation utility that will move the bar
528 * @method _animSetter
529 * @param value {boolean ,YAHOO.util.Anim} Enable animation or set to specific instance
533 _animSetter: function (value) {
534 var anim, barEl = this.get(BAR_EL);
536 if (value instanceof YAHOO.util.Anim) {
539 anim = new YAHOO.util.Anim(barEl);
541 anim.onTween.subscribe(this._animOnTween,this,true);
542 anim.onComplete.subscribe(this._animComplete,this,true);
544 this._fixAnim(anim,this.get(ACC));
547 anim = this.get(ANIM);
549 anim.onTween.unsubscribeAll();
550 anim.onComplete.unsubscribeAll();
557 * Temporary solution until http://yuilibrary.com/projects/yui2/ticket/2528222 gets solved.
558 * Also fixes: http://yuilibrary.com/projects/yui2/ticket/2528919.
559 * It also handles moving the background as per the animAcceleration configuration attribute
560 * since it turned out to be the best place to handle it.
562 * @param anim {YAHOO.util.Anim} Instance of Animation to fix
563 * @param acc {number} Value of animAcceleration attribute
567 _fixAnim: function(anim, acc) {
571 if (!this._oldSetAttribute) {
572 this._oldSetAttribute = anim.setAttribute;
575 switch(this.get(DIRECTION)) {
577 anim.setAttribute = function(attr , val , unit) {
578 val = Math.round(val);
579 pb._oldSetAttribute.call(this,attr,val,unit);
581 pb._oldSetAttribute.call(this,BG_POSITION,-val * acc,PX);
586 anim.setAttribute = function(attr , val , unit) {
587 val = Math.round(val);
588 pb._oldSetAttribute.call(this,attr,val,unit);
590 var left = pb._barSpace - val;
591 pb._oldSetAttribute.call(this,'left',left, PX);
592 pb._oldSetAttribute.call(this, BG_POSITION, -left + val * acc, PX);
597 anim.setAttribute = function(attr , val , unit) {
598 val = Math.round(val);
599 pb._oldSetAttribute.call(this,attr,val,unit);
600 if (attr == HEIGHT) {
601 pb._oldSetAttribute.call(this,BG_POSITION,'center ' + (- val * acc),PX);
607 anim.setAttribute = function(attr , val , unit) {
608 val = Math.round(val);
609 pb._oldSetAttribute.call(this,attr,val,unit);
610 if (attr == HEIGHT) {
611 var top = pb._barSpace - val;
612 pb._oldSetAttribute.call(this,'top',top, PX);
613 pb._oldSetAttribute.call(this, BG_POSITION,'center ' + (val * acc - top), PX);
622 * Called when the animation signals it has completed.
623 * @method _animComplete
627 _animComplete: function() {
628 var value = this.get(VALUE);
629 this._previousValue = value;
630 this.fireEvent(PROGRESS,value);
631 this.fireEvent(COMPLETE, value);
632 Dom.removeClass(this.get(BAR_EL),CLASS_ANIM);
635 * Called for each onTween event of the animation instance.
636 * @method _animComplete
637 * @param name {string} Name of the event fired
638 * @param oArgs {object} Arguments provided by the Anim instance
642 _animOnTween:function (name,oArgs) {
643 var value = Math.floor(this._tweenFactor * oArgs[0].currentFrame + this._previousValue);
644 this.fireEvent(PROGRESS,value);
648 * Called in response to a change in the <a href="#config_value">value</a> attribute.
649 * Moves the bar to reflect the new value
650 * @method _valueChange
651 * @param value {number} New value to be set
652 * @param noAnim {boolean} Disable animation for this redraw
656 _valueChange: function (value, noAnim) {
657 var anim = this.get(ANIM),
658 pixelValue = Math.floor((value - this.get(MIN_VALUE)) * this._barFactor);
660 this._setAriaText(value);
661 if (this._rendered) {
664 if (anim.isAnimated()) { anim._onComplete.fire(); } // see: http://yuilibrary.com/projects/yui2/ticket/2528217
666 this.fireEvent(START,this._previousValue);
667 Prog._barSizeFunctions[((noAnim !== false) && anim)?1:0][this.get(DIRECTION)].call(this, value, pixelValue, this.get(BAR_EL), anim);
672 * Utility method to set the ARIA value attributes
673 * @method _setAriaText
674 * @param value {number} Value to be voiced
678 _setAriaText: function(value) {
680 var container = this.get('element'),
681 text = Lang.substitute(this.get(ARIA_TEXT_TEMPLATE),{
683 minValue:this.get(MIN_VALUE),
684 maxValue:this.get(MAX_VALUE)
686 container.setAttribute('aria-valuenow',value);
687 container.setAttribute('aria-valuetext',text);
691 * Collection of functions used to calculate the size of the bar.
692 * One of this will be used depending on direction and whether animation is active.
693 * @property _barSizeFunctions
694 * @type {collection of functions}
699 Prog._barSizeFunctions = b;
701 b[0][DIRECTION_LTR] = function(value, pixelValue, barEl, anim) {
702 Dom.setStyle(barEl,WIDTH, pixelValue + PX);
703 this.fireEvent(PROGRESS,value);
704 this.fireEvent(COMPLETE,value);
706 b[0][DIRECTION_RTL] = function(value, pixelValue, barEl, anim) {
707 Dom.setStyle(barEl,WIDTH, pixelValue + PX);
708 Dom.setStyle(barEl,'left',(this._barSpace - pixelValue) + PX);
709 this.fireEvent(PROGRESS,value);
710 this.fireEvent(COMPLETE,value);
712 b[0][DIRECTION_TTB] = function(value, pixelValue, barEl, anim) {
713 Dom.setStyle(barEl,HEIGHT, pixelValue + PX);
714 this.fireEvent(PROGRESS,value);
715 this.fireEvent(COMPLETE,value);
717 b[0][DIRECTION_BTT] = function(value, pixelValue, barEl, anim) {
718 Dom.setStyle(barEl,HEIGHT, pixelValue + PX);
719 Dom.setStyle(barEl,'top', (this._barSpace - pixelValue) + PX);
720 this.fireEvent(PROGRESS,value);
721 this.fireEvent(COMPLETE,value);
723 b[1][DIRECTION_LTR] = function(value, pixelValue, barEl, anim) {
724 Dom.addClass(barEl,CLASS_ANIM);
725 this._tweenFactor = (value - this._previousValue) / anim.totalFrames / anim.duration;
726 anim.attributes = {width:{ to: pixelValue }};
729 b[1][DIRECTION_RTL] = b[1][DIRECTION_LTR];
730 b[1][DIRECTION_TTB] = function(value, pixelValue, barEl, anim) {
731 Dom.addClass(barEl,CLASS_ANIM);
732 this._tweenFactor = (value - this._previousValue) / anim.totalFrames / anim.duration;
733 anim.attributes = {height:{to: pixelValue}};
736 b[1][DIRECTION_BTT] = b[1][DIRECTION_TTB];
740 YAHOO.register("progressbar", YAHOO.widget.ProgressBar, {version: "2.9.0", build: "2800"});