123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- (function ($) {
- Drupal.progressBar = function (id, updateCallback, method, errorCallback) {
- var pb = this;
- this.id = id;
- this.method = method || 'GET';
- this.updateCallback = updateCallback;
- this.errorCallback = errorCallback;
-
-
- this.element = $('<div class="progress" aria-live="polite"></div>').attr('id', id);
- this.element.html('<div class="bar"><div class="filled"></div></div>' +
- '<div class="percentage"></div>' +
- '<div class="message"> </div>');
- };
- Drupal.progressBar.prototype.setProgress = function (percentage, message) {
- if (percentage >= 0 && percentage <= 100) {
- $('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);
- }
- };
- Drupal.progressBar.prototype.startMonitoring = function (uri, delay) {
- this.delay = delay;
- this.uri = uri;
- this.sendPing();
- };
- Drupal.progressBar.prototype.stopMonitoring = function () {
- clearTimeout(this.timer);
-
- this.uri = null;
- };
- Drupal.progressBar.prototype.sendPing = function () {
- if (this.timer) {
- clearTimeout(this.timer);
- }
- if (this.uri) {
- var pb = this;
-
-
- $.ajax({
- type: this.method,
- url: this.uri,
- data: '',
- dataType: 'json',
- success: function (progress) {
-
- if (progress.status == 0) {
- pb.displayError(progress.data);
- return;
- }
-
- pb.setProgress(progress.percentage, progress.message);
-
- pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay);
- },
- error: function (xmlhttp) {
- pb.displayError(Drupal.ajaxError(xmlhttp, pb.uri));
- }
- });
- }
- };
- Drupal.progressBar.prototype.displayError = function (string) {
- var error = $('<div class="messages error"></div>').html(string);
- $(this.element).before(error).hide();
- if (this.errorCallback) {
- this.errorCallback(this);
- }
- };
- })(jQuery);
|