Newer
Older
Angie Byron
committed
(function ($) {
/**
* A progressbar object. Initialized with the given id. Must be inserted into
* the DOM afterwards through progressBar.element.
*
* method is the function which will perform the HTTP request to get the
* progress bar state. Either "GET" or "POST".
* e.g. pb = new progressBar('myProgressBar');
* some_element.appendChild(pb.element);
*/
Angie Byron
committed
Drupal.progressBar = function (id, updateCallback, method, errorCallback) {
var pb = this;
this.id = id;
Angie Byron
committed
this.method = method || 'GET';
this.updateCallback = updateCallback;
this.errorCallback = errorCallback;
Angie Byron
committed
this.element = $('<div class="progress"></div>').attr('id', id);
this.element.html('<div class="bar"><div class="filled"></div></div>' +
'<div class="percentage"></div>' +
'<div class="message"> </div>');
/**
* Set the percentage and status message for the progressbar.
*/
Angie Byron
committed
Drupal.progressBar.prototype.setProgress = function (percentage, message) {
if (percentage >= 0 && percentage <= 100) {
Angie Byron
committed
$('div.filled', this.element).css('width', percentage + '%');
$('div.percentage', this.element).html(percentage + '%');
$('div.message', this.element).html(message);
if (this.updateCallback) {
this.updateCallback(percentage, message, this);
/**
* Start monitoring progress via Ajax.
*/
Angie Byron
committed
Drupal.progressBar.prototype.startMonitoring = function (uri, delay) {
this.delay = delay;
this.uri = uri;
this.sendPing();
/**
* Stop monitoring progress via Ajax.
*/
Angie Byron
committed
Drupal.progressBar.prototype.stopMonitoring = function () {
clearTimeout(this.timer);
// This allows monitoring to be stopped from within the callback.
Steven Wittens
committed
this.uri = null;
/**
* Request progress data from server.
*/
Angie Byron
committed
Drupal.progressBar.prototype.sendPing = function () {
if (this.timer) {
clearTimeout(this.timer);
}
Steven Wittens
committed
if (this.uri) {
// When doing a post request, you need non-null data. Otherwise a
// HTTP 411 or HTTP 406 (with Apache mod_security) error may result.
$.ajax({
type: this.method,
url: this.uri,
Angie Byron
committed
success: function (progress) {
// Display errors.
if (progress.status == 0) {
pb.displayError(progress.data);
return;
}
// Update display.
pb.setProgress(progress.percentage, progress.message);
// Schedule next timer.
Angie Byron
committed
pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay);
Angie Byron
committed
error: function (xmlhttp) {
pb.displayError(Drupal.ahahError(xmlhttp, pb.uri));
/**
* Display errors on the page.
*/
Angie Byron
committed
Drupal.progressBar.prototype.displayError = function (string) {
Angie Byron
committed
var error = $('<div class="error"></div>').html(string);
$(this.element).before(error).hide();
if (this.errorCallback) {
this.errorCallback(this);
}
})(jQuery);