batch.es6.js 1.2 KB

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