1 // $Id: progress.js 144 2007-03-28 07:52:20Z thierry $
4 * A progressbar object. Initialized with the given id. Must be inserted into
5 * the DOM afterwards through progressBar.element.
7 * method is the function which will perform the HTTP request to get the
8 * progress bar state. Either HTTPGet or HTTPPost.
10 * e.g. pb = new progressBar('myProgressBar');
11 * some_element.appendChild(pb.element);
13 function progressBar(id, updateCallback, method, errorCallback) {
16 this.method = method ? method : HTTPGet;
17 this.updateCallback = updateCallback;
18 this.errorCallback = errorCallback;
20 this.element = document.createElement('div');
22 this.element.className = 'progress';
23 this.element.innerHTML = '<div class="percentage"></div>'+
24 '<div class="message"> </div>'+
25 '<div class="bar"><div class="filled"></div></div>';
29 * Set the percentage and status message for the progressbar.
31 progressBar.prototype.setProgress = function (percentage, message) {
32 var divs = this.element.getElementsByTagName('div');
34 for (var i = 0; div = divs[i]; ++i) {
35 if (percentage >= 0) {
36 if (hasClass(divs[i], 'filled')) {
37 divs[i].style.width = percentage + '%';
39 if (hasClass(divs[i], 'percentage')) {
40 divs[i].innerHTML = percentage + '%';
43 if (hasClass(divs[i], 'message')) {
44 divs[i].innerHTML = message;
47 if (this.updateCallback) {
48 this.updateCallback(percentage, message, this);
53 * Start monitoring progress via Ajax.
55 progressBar.prototype.startMonitoring = function (uri, delay) {
62 * Stop monitoring progress via Ajax.
64 progressBar.prototype.stopMonitoring = function () {
65 clearTimeout(this.timer);
66 // This allows monitoring to be stopped from within the callback
71 * Request progress data from server.
73 progressBar.prototype.sendPing = function () {
75 clearTimeout(this.timer);
78 this.method(this.uri, this.receivePing, this, '');
83 * HTTP callback function. Passes data back to the progressbar and sets a new
84 * timer for the next ping.
86 progressBar.prototype.receivePing = function (string, xmlhttp, pb) {
87 if (xmlhttp.status != 200) {
88 return pb.displayError('An HTTP error '+ xmlhttp.status +' occured.\n'+ pb.uri);
91 var progress = parseJson(string);
93 if (progress.status == 0) {
94 pb.displayError(progress.data);
99 pb.setProgress(progress.percentage, progress.message);
100 // Schedule next timer
101 pb.timer = setTimeout(function() { pb.sendPing(); }, pb.delay);
105 * Display errors on the page.
107 progressBar.prototype.displayError = function (string) {
108 var error = document.createElement('div');
109 error.className = 'error';
110 error.innerHTML = string;
112 this.element.style.display = 'none';
113 this.element.parentNode.insertBefore(error, this.element);
115 if (this.errorCallback) {
116 this.errorCallback(this);