summaryrefslogtreecommitdiff
path: root/misc/batch.js
blob: 4d1a4d551fbd4c4ce100d5b5f7739db3f113062e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// $Id$

/**
 * Attaches the batch behavior to progress bars.
 */
Drupal.behaviors.batch = {
  attach: function(context) {
    // This behavior attaches by ID, so is only valid once on a page.
    if ($('#progress.batch-processed').size()) {
      return;
    }
    $('#progress', context).addClass('batch-processed').each(function () {
      var holder = this;
      var uri = Drupal.settings.batch.uri;
      var initMessage = Drupal.settings.batch.initMessage;
      var errorMessage = Drupal.settings.batch.errorMessage;

      // Success: redirect to the summary.
      var updateCallback = function (progress, status, pb) {
        if (progress == 100) {
          pb.stopMonitoring();
          window.location = uri+'&op=finished';
        }
      };

      var errorCallback = function (pb) {
        var div = document.createElement('p');
        div.className = 'error';
        $(div).html(errorMessage);
        $(holder).prepend(div);
        $('#wait').hide();
      };

      var progress = new Drupal.progressBar('updateprogress', updateCallback, "POST", errorCallback);
      progress.setProgress(-1, initMessage);
      $(holder).append(progress.element);
      progress.startMonitoring(uri+'&op=do', 10);
    });
  }
};