webmaster@1
|
1 // $Id: progress.js,v 1.20 2008/01/04 11:53:21 goba Exp $ |
webmaster@1
|
2 |
webmaster@1
|
3 /** |
webmaster@1
|
4 * A progressbar object. Initialized with the given id. Must be inserted into |
webmaster@1
|
5 * the DOM afterwards through progressBar.element. |
webmaster@1
|
6 * |
webmaster@1
|
7 * method is the function which will perform the HTTP request to get the |
webmaster@1
|
8 * progress bar state. Either "GET" or "POST". |
webmaster@1
|
9 * |
webmaster@1
|
10 * e.g. pb = new progressBar('myProgressBar'); |
webmaster@1
|
11 * some_element.appendChild(pb.element); |
webmaster@1
|
12 */ |
webmaster@1
|
13 Drupal.progressBar = function (id, updateCallback, method, errorCallback) { |
webmaster@1
|
14 var pb = this; |
webmaster@1
|
15 this.id = id; |
webmaster@1
|
16 this.method = method || "GET"; |
webmaster@1
|
17 this.updateCallback = updateCallback; |
webmaster@1
|
18 this.errorCallback = errorCallback; |
webmaster@1
|
19 |
webmaster@1
|
20 this.element = document.createElement('div'); |
webmaster@1
|
21 this.element.id = id; |
webmaster@1
|
22 this.element.className = 'progress'; |
webmaster@1
|
23 $(this.element).html('<div class="bar"><div class="filled"></div></div>'+ |
webmaster@1
|
24 '<div class="percentage"></div>'+ |
webmaster@1
|
25 '<div class="message"> </div>'); |
webmaster@1
|
26 }; |
webmaster@1
|
27 |
webmaster@1
|
28 /** |
webmaster@1
|
29 * Set the percentage and status message for the progressbar. |
webmaster@1
|
30 */ |
webmaster@1
|
31 Drupal.progressBar.prototype.setProgress = function (percentage, message) { |
webmaster@1
|
32 if (percentage >= 0 && percentage <= 100) { |
webmaster@1
|
33 $('div.filled', this.element).css('width', percentage +'%'); |
webmaster@1
|
34 $('div.percentage', this.element).html(percentage +'%'); |
webmaster@1
|
35 } |
webmaster@1
|
36 $('div.message', this.element).html(message); |
webmaster@1
|
37 if (this.updateCallback) { |
webmaster@1
|
38 this.updateCallback(percentage, message, this); |
webmaster@1
|
39 } |
webmaster@1
|
40 }; |
webmaster@1
|
41 |
webmaster@1
|
42 /** |
webmaster@1
|
43 * Start monitoring progress via Ajax. |
webmaster@1
|
44 */ |
webmaster@1
|
45 Drupal.progressBar.prototype.startMonitoring = function (uri, delay) { |
webmaster@1
|
46 this.delay = delay; |
webmaster@1
|
47 this.uri = uri; |
webmaster@1
|
48 this.sendPing(); |
webmaster@1
|
49 }; |
webmaster@1
|
50 |
webmaster@1
|
51 /** |
webmaster@1
|
52 * Stop monitoring progress via Ajax. |
webmaster@1
|
53 */ |
webmaster@1
|
54 Drupal.progressBar.prototype.stopMonitoring = function () { |
webmaster@1
|
55 clearTimeout(this.timer); |
webmaster@1
|
56 // This allows monitoring to be stopped from within the callback |
webmaster@1
|
57 this.uri = null; |
webmaster@1
|
58 }; |
webmaster@1
|
59 |
webmaster@1
|
60 /** |
webmaster@1
|
61 * Request progress data from server. |
webmaster@1
|
62 */ |
webmaster@1
|
63 Drupal.progressBar.prototype.sendPing = function () { |
webmaster@1
|
64 if (this.timer) { |
webmaster@1
|
65 clearTimeout(this.timer); |
webmaster@1
|
66 } |
webmaster@1
|
67 if (this.uri) { |
webmaster@1
|
68 var pb = this; |
webmaster@1
|
69 // When doing a post request, you need non-null data. Otherwise a |
webmaster@1
|
70 // HTTP 411 or HTTP 406 (with Apache mod_security) error may result. |
webmaster@1
|
71 $.ajax({ |
webmaster@1
|
72 type: this.method, |
webmaster@1
|
73 url: this.uri, |
webmaster@1
|
74 data: '', |
webmaster@1
|
75 dataType: 'json', |
webmaster@1
|
76 success: function (progress) { |
webmaster@1
|
77 // Display errors |
webmaster@1
|
78 if (progress.status == 0) { |
webmaster@1
|
79 pb.displayError(progress.data); |
webmaster@1
|
80 return; |
webmaster@1
|
81 } |
webmaster@1
|
82 // Update display |
webmaster@1
|
83 pb.setProgress(progress.percentage, progress.message); |
webmaster@1
|
84 // Schedule next timer |
webmaster@1
|
85 pb.timer = setTimeout(function() { pb.sendPing(); }, pb.delay); |
webmaster@1
|
86 }, |
webmaster@1
|
87 error: function (xmlhttp) { |
webmaster@1
|
88 pb.displayError(Drupal.ahahError(xmlhttp, pb.uri)); |
webmaster@1
|
89 } |
webmaster@1
|
90 }); |
webmaster@1
|
91 } |
webmaster@1
|
92 }; |
webmaster@1
|
93 |
webmaster@1
|
94 /** |
webmaster@1
|
95 * Display errors on the page. |
webmaster@1
|
96 */ |
webmaster@1
|
97 Drupal.progressBar.prototype.displayError = function (string) { |
webmaster@1
|
98 var error = document.createElement('div'); |
webmaster@1
|
99 error.className = 'error'; |
webmaster@1
|
100 error.innerHTML = string; |
webmaster@1
|
101 |
webmaster@1
|
102 $(this.element).before(error).hide(); |
webmaster@1
|
103 |
webmaster@1
|
104 if (this.errorCallback) { |
webmaster@1
|
105 this.errorCallback(this); |
webmaster@1
|
106 } |
webmaster@1
|
107 }; |