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