progress.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. Drupal.theme.progressBar = function (id) {
  9. return '<div id="' + id + '" class="progress" aria-live="polite">' + '<div class="progress__label">&nbsp;</div>' + '<div class="progress__track"><div class="progress__bar"></div></div>' + '<div class="progress__percentage"></div>' + '<div class="progress__description">&nbsp;</div>' + '</div>';
  10. };
  11. Drupal.ProgressBar = function (id, updateCallback, method, errorCallback) {
  12. this.id = id;
  13. this.method = method || 'GET';
  14. this.updateCallback = updateCallback;
  15. this.errorCallback = errorCallback;
  16. this.element = $(Drupal.theme('progressBar', id));
  17. };
  18. $.extend(Drupal.ProgressBar.prototype, {
  19. setProgress: function setProgress(percentage, message, label) {
  20. if (percentage >= 0 && percentage <= 100) {
  21. $(this.element).find('div.progress__bar').css('width', percentage + '%');
  22. $(this.element).find('div.progress__percentage').html(percentage + '%');
  23. }
  24. $('div.progress__description', this.element).html(message);
  25. $('div.progress__label', this.element).html(label);
  26. if (this.updateCallback) {
  27. this.updateCallback(percentage, message, this);
  28. }
  29. },
  30. startMonitoring: function startMonitoring(uri, delay) {
  31. this.delay = delay;
  32. this.uri = uri;
  33. this.sendPing();
  34. },
  35. stopMonitoring: function stopMonitoring() {
  36. clearTimeout(this.timer);
  37. this.uri = null;
  38. },
  39. sendPing: function sendPing() {
  40. if (this.timer) {
  41. clearTimeout(this.timer);
  42. }
  43. if (this.uri) {
  44. var pb = this;
  45. var uri = this.uri;
  46. if (uri.indexOf('?') === -1) {
  47. uri += '?';
  48. } else {
  49. uri += '&';
  50. }
  51. uri += '_format=json';
  52. $.ajax({
  53. type: this.method,
  54. url: uri,
  55. data: '',
  56. dataType: 'json',
  57. success: function success(progress) {
  58. if (progress.status === 0) {
  59. pb.displayError(progress.data);
  60. return;
  61. }
  62. pb.setProgress(progress.percentage, progress.message, progress.label);
  63. pb.timer = setTimeout(function () {
  64. pb.sendPing();
  65. }, pb.delay);
  66. },
  67. error: function error(xmlhttp) {
  68. var e = new Drupal.AjaxError(xmlhttp, pb.uri);
  69. pb.displayError('<pre>' + e.message + '</pre>');
  70. }
  71. });
  72. }
  73. },
  74. displayError: function displayError(string) {
  75. var error = $('<div class="messages messages--error"></div>').html(string);
  76. $(this.element).before(error).hide();
  77. if (this.errorCallback) {
  78. this.errorCallback(this);
  79. }
  80. }
  81. });
  82. })(jQuery, Drupal);