batch.js 1.2 KB

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