progress.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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" aria-live="polite"></div>').attr('id', id);
  21. this.element.html('<div class="bar"><div class="filled"></div></div>' +
  22. '<div class="percentage"></div>' +
  23. '<div class="message">&nbsp;</div>');
  24. };
  25. /**
  26. * Set the percentage and status message for the progressbar.
  27. */
  28. Drupal.progressBar.prototype.setProgress = function (percentage, message) {
  29. if (percentage >= 0 && percentage <= 100) {
  30. $('div.filled', this.element).css('width', percentage + '%');
  31. $('div.percentage', this.element).html(percentage + '%');
  32. }
  33. $('div.message', this.element).html(message);
  34. if (this.updateCallback) {
  35. this.updateCallback(percentage, message, this);
  36. }
  37. };
  38. /**
  39. * Start monitoring progress via Ajax.
  40. */
  41. Drupal.progressBar.prototype.startMonitoring = function (uri, delay) {
  42. this.delay = delay;
  43. this.uri = uri;
  44. this.sendPing();
  45. };
  46. /**
  47. * Stop monitoring progress via Ajax.
  48. */
  49. Drupal.progressBar.prototype.stopMonitoring = function () {
  50. clearTimeout(this.timer);
  51. // This allows monitoring to be stopped from within the callback.
  52. this.uri = null;
  53. };
  54. /**
  55. * Request progress data from server.
  56. */
  57. Drupal.progressBar.prototype.sendPing = function () {
  58. if (this.timer) {
  59. clearTimeout(this.timer);
  60. }
  61. if (this.uri) {
  62. var pb = this;
  63. // When doing a post request, you need non-null data. Otherwise a
  64. // HTTP 411 or HTTP 406 (with Apache mod_security) error may result.
  65. $.ajax({
  66. type: this.method,
  67. url: this.uri,
  68. data: '',
  69. dataType: 'json',
  70. success: function (progress) {
  71. // Display errors.
  72. if (progress.status == 0) {
  73. pb.displayError(progress.data);
  74. return;
  75. }
  76. // Update display.
  77. pb.setProgress(progress.percentage, progress.message);
  78. // Schedule next timer.
  79. pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay);
  80. },
  81. error: function (xmlhttp) {
  82. pb.displayError(Drupal.ajaxError(xmlhttp, pb.uri));
  83. }
  84. });
  85. }
  86. };
  87. /**
  88. * Display errors on the page.
  89. */
  90. Drupal.progressBar.prototype.displayError = function (string) {
  91. var error = $('<div class="messages error"></div>').html(string);
  92. $(this.element).before(error).hide();
  93. if (this.errorCallback) {
  94. this.errorCallback(this);
  95. }
  96. };
  97. })(jQuery);