9c8caac76324972a2de870cd6385dff313d2c800
[yaffs-website] / web / core / misc / batch.es6.js
1 /**
2  * @file
3  * Drupal's batch API.
4  */
5
6 (function($, Drupal) {
7   /**
8    * Attaches the batch behavior to progress bars.
9    *
10    * @type {Drupal~behavior}
11    */
12   Drupal.behaviors.batch = {
13     attach(context, settings) {
14       const batch = settings.batch;
15       const $progress = $('[data-drupal-progress]').once('batch');
16       let progressBar;
17
18       // Success: redirect to the summary.
19       function updateCallback(progress, status, pb) {
20         if (progress === '100') {
21           pb.stopMonitoring();
22           window.location = `${batch.uri}&op=finished`;
23         }
24       }
25
26       function errorCallback(pb) {
27         $progress.prepend($('<p class="error"></p>').html(batch.errorMessage));
28         $('#wait').hide();
29       }
30
31       if ($progress.length) {
32         progressBar = new Drupal.ProgressBar(
33           'updateprogress',
34           updateCallback,
35           'POST',
36           errorCallback,
37         );
38         progressBar.setProgress(-1, batch.initMessage);
39         progressBar.startMonitoring(`${batch.uri}&op=do`, 10);
40         // Remove HTML from no-js progress bar.
41         $progress.empty();
42         // Append the JS progressbar element.
43         $progress.append(progressBar.element);
44       }
45     },
46   };
47 })(jQuery, Drupal);