progressbar.js
Summary
Progress Bar widget class derived from Widget. See
description of base Widget class at
http://trac.zapatec.com:8000/trac/wiki/Widget.
Copyright (c) 2004-2006 by Zapatec, Inc.
http://www.zapatec.com
1700 MLK Way, Berkeley, California,
94709, U.S.A.
All rights reserved.
Zapatec.ProgressBar = function(objArgs) {
if (arguments.length == 0) {
objArgs = {};
}
Zapatec.ProgressBar.SUPERconstructor.call(this, objArgs);
};
Zapatec.ProgressBar.id = 'Zapatec.ProgressBar';
Zapatec.inherit(Zapatec.ProgressBar, Zapatec.Widget);
Zapatec.ProgressBar.prototype.init = function(objArgs) {
Zapatec.ProgressBar.SUPERclass.init.call(this, objArgs);
this.create();
};
Zapatec.ProgressBar.prototype.configure = function(objArgs) {
this.defineConfigOption('parent', null);
this.defineConfigOption('width', null);
this.defineConfigOption('height', null);
this.defineConfigOption('minValue', 0);
this.defineConfigOption('maxValue', 100);
this.defineConfigOption('value', null);
this.defineConfigOption('title', null);
this.defineConfigOption('showLabels', 'none');
this.defineConfigOption('themePath', Zapatec.zapatecPath+
'../zpextra/themes/progressbar/');
Zapatec.ProgressBar.SUPERclass.configure.call(this, objArgs);
this.config.parent = Zapatec.Widget.getElementById(this.config.parent);
if (!this.config.parent) {
Zapatec.Log({description: "No reference to parent element."});
return null;
}
if (this.config.minValue >= this.config.maxValue) {
Zapatec.Log({description: "Minimal value is greater "+
"than or equals to maximal value."});
throw "Minimal value is greater than or equals to maximal value.";
return null;
}
if (this.config.value > this.config.maxValue) {
Zapatec.Log({description: "Actual value is out of bounds."});
this.config.value = this.config.maxValue;
return null;
}
if (this.config.value < this.config.minValue) {
Zapatec.Log({description: "Actual value is out of bounds."});
this.config.value = this.config.minValue;
return null;
}
this.currentValue = this.config.value;
if ((this.config.showLabels != 'none') &&
(this.config.showLabels != 'values') &&
(this.config.showLabels != 'percents')) {
Zapatec.Log(
{description: "Wrong showLabels value. Must be 'none',"+
"'values' or 'percents'."});
return null;
}
};
Zapatec.ProgressBar.prototype.create = function() {
this.container = Zapatec.Utils.createElement("div", this.config.parent);
this.container.className = this.getClassName({
prefix: "zpProgressBar", suffix: "Container"});
this.container.style.width = this.config.width + "px";
this.internalContainer = Zapatec.Utils.createElement("div",this.container);
this.internalContainer.className = "internal";
this.internalContainer.style.height = this.config.height - 4 + "px";
this.internalContainer.style.width = this.calculatePercentage() + "%";
var additionalHeightForLabels = 0;
if (this.config.showLabels != 'none') {
this.labelsContainer = Zapatec.Utils.createElement("div",
this.container);
this.labelsContainer.className = "labels";
this.labelsContainer.style.width = this.config.width - 4 + "px";
additionalHeightForLabels = this.labelsContainer.offsetHeight;
}
this.container.style.height = this.config.height +
additionalHeightForLabels +
"px";
this.displayLabel();
}
Zapatec.ProgressBar.prototype.calculateWidth = function() {
return Math.round(
(this.currentValue / (this.config.maxValue - this.config.minValue))
) * this.config.width;
}
Zapatec.ProgressBar.prototype.calculatePercentage = function() {
return (this.currentValue / (this.config.maxValue - this.config.minValue))
* 100;
}
Zapatec.ProgressBar.prototype.redraw = function() {
this.internalContainer.style.width = this.calculatePercentage() + "%";
this.displayLabel();
}
Zapatec.ProgressBar.prototype.displayLabel = function(){
var text = '';
if (this.config.showLabels == 'values') {
text = this.currentValue+"/"+
(this.config.maxValue-this.config.minValue);
}
if (this.config.showLabels == 'percents') {
text = Math.round(this.calculatePercentage())+"%";
}
if (this.config.showLabels != 'none') {
if (this.config.title != null) {
text = this.config.title + text;
}
this.labelsContainer.innerHTML = text;
}
}
Zapatec.ProgressBar.prototype.setProgress = function (value) {
value = parseFloat(value);
if (isNaN(value)) {
Zapatec.Log({description:'Progress value is not a number!'});
return false;
}
this.currentValue = value;
this.redraw();
return null;
}
Zapatec.ProgressBar.prototype.getProgress = function () {
return this.currentValue;
}
Zapatec.ProgressBar.prototype.cancel = function () {
this.fireEvent('cancel', this.currentValue);
}
Zapatec.ProgressBar.prototype.hide = function () {
this.container.style.display = 'none';
}
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007