batch.es6.js 1.2 KB

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