[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/misc/ -> progress.js (source)

   1  
   2  /**
   3   * A progressbar object. Initialized with the given id. Must be inserted into
   4   * the DOM afterwards through progressBar.element.
   5   *
   6   * method is the function which will perform the HTTP request to get the
   7   * progress bar state. Either "GET" or "POST".
   8   *
   9   * e.g. pb = new progressBar('myProgressBar');
  10   *      some_element.appendChild(pb.element);
  11   */
  12  Drupal.progressBar = function (id, updateCallback, method, errorCallback) {
  13    var pb = this;
  14    this.id = id;
  15    this.method = method || "GET";
  16    this.updateCallback = updateCallback;
  17    this.errorCallback = errorCallback;
  18  
  19    this.element = document.createElement('div');
  20    this.element.id = id;
  21    this.element.className = 'progress';
  22    $(this.element).html('<div class="bar"><div class="filled"></div></div>'+
  23                         '<div class="percentage"></div>'+
  24                         '<div class="message">&nbsp;</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.ahahError(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 = document.createElement('div');
  98    error.className = 'error';
  99    error.innerHTML = string;
 100  
 101    $(this.element).before(error).hide();
 102  
 103    if (this.errorCallback) {
 104      this.errorCallback(this);
 105    }
 106  };


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7