_progress.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. (function ($) {
  2. /**
  3. * A progressbar object. Initialized with the given id. Must be inserted into
  4. * the DOM afterwards through progressBar.element.
  5. *
  6. * method is the function which will perform the HTTP request to get the
  7. * progress bar state. Either "GET" or "POST".
  8. *
  9. * e.g. pb = new progressBar('myProgressBar');
  10. * some_element.appendChild(pb.element);
  11. */
  12. Drupal.progressBar = function (id, updateCallback, method, errorCallback) {
  13. var pb = this;
  14. this.id = id;
  15. this.method = method || 'GET';
  16. this.updateCallback = updateCallback;
  17. this.errorCallback = errorCallback;
  18. // The WAI-ARIA setting aria-live="polite" will announce changes after users
  19. // have completed their current activity and not interrupt the screen reader.
  20. this.element = $('<div class="progress-wrapper" aria-live="polite"></div>');
  21. this.element.html('<div id ="' + id + '" class="progress">' +
  22. '<div class="progress-bar bg-primary progress-bar-animated progress-bar-striped" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">' +
  23. '<div class="percentage sr-only"></div>' +
  24. '</div></div>' +
  25. '</div><div class="percentage pull-right"></div>' +
  26. '<div class="message">&nbsp;</div>');
  27. };
  28. /**
  29. * Set the percentage and status message for the progressbar.
  30. */
  31. Drupal.progressBar.prototype.setProgress = function (percentage, message) {
  32. if (percentage >= 0 && percentage <= 100) {
  33. $('div.progress-bar', this.element).css('width', percentage + '%');
  34. $('div.progress-bar', this.element).attr('aria-valuenow', percentage);
  35. $('div.percentage', this.element).html(percentage + '%');
  36. }
  37. $('div.message', this.element).html(message);
  38. if (this.updateCallback) {
  39. this.updateCallback(percentage, message, this);
  40. }
  41. if(percentage == 100) {
  42. $('div.progress-bar', this.element).removeClass('progress-bar-animated');
  43. }
  44. };
  45. /**
  46. * Start monitoring progress via Ajax.
  47. */
  48. Drupal.progressBar.prototype.startMonitoring = function (uri, delay) {
  49. this.delay = delay;
  50. this.uri = uri;
  51. this.sendPing();
  52. };
  53. /**
  54. * Stop monitoring progress via Ajax.
  55. */
  56. Drupal.progressBar.prototype.stopMonitoring = function () {
  57. clearTimeout(this.timer);
  58. // This allows monitoring to be stopped from within the callback.
  59. this.uri = null;
  60. };
  61. /**
  62. * Request progress data from server.
  63. */
  64. Drupal.progressBar.prototype.sendPing = function () {
  65. if (this.timer) {
  66. clearTimeout(this.timer);
  67. }
  68. if (this.uri) {
  69. var pb = this;
  70. // When doing a post request, you need non-null data. Otherwise a
  71. // HTTP 411 or HTTP 406 (with Apache mod_security) error may result.
  72. $.ajax({
  73. type: this.method,
  74. url: this.uri,
  75. data: '',
  76. dataType: 'json',
  77. success: function (progress) {
  78. // Display errors.
  79. if (progress.status == 0) {
  80. pb.displayError(progress.data);
  81. return;
  82. }
  83. // Update display.
  84. pb.setProgress(progress.percentage, progress.message);
  85. // Schedule next timer.
  86. pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay);
  87. },
  88. error: function (xmlhttp) {
  89. pb.displayError(Drupal.ajaxError(xmlhttp, pb.uri));
  90. }
  91. });
  92. }
  93. };
  94. /**
  95. * Display errors on the page.
  96. */
  97. Drupal.progressBar.prototype.displayError = function (string) {
  98. var error = $('<div class="alert alert-block alert-error"><a class="close" data-dismiss="alert" href="#">&times;</a><h4>Error message</h4></div>').append(string);
  99. $(this.element).before(error).hide();
  100. if (this.errorCallback) {
  101. this.errorCallback(this);
  102. }
  103. };
  104. })(jQuery);