995d1c51d12435aa9874a73f51cdfda673f79a24
[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('updateprogress', updateCallback, 'POST', errorCallback);
33         progressBar.setProgress(-1, batch.initMessage);
34         progressBar.startMonitoring(`${batch.uri}&op=do`, 10);
35         // Remove HTML from no-js progress bar.
36         $progress.empty();
37         // Append the JS progressbar element.
38         $progress.append(progressBar.element);
39       }
40     },
41   };
42 }(jQuery, Drupal));