progress.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file
  3. * Progress bar.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Theme function for the progress bar.
  9. *
  10. * @param {string} id
  11. * The id for the progress bar.
  12. *
  13. * @return {string}
  14. * The HTML for the progress bar.
  15. */
  16. Drupal.theme.progressBar = function (id) {
  17. return '<div id="' + id + '" class="progress" aria-live="polite">' +
  18. '<div class="progress__label">&nbsp;</div>' +
  19. '<div class="progress__track"><div class="progress__bar"></div></div>' +
  20. '<div class="progress__percentage"></div>' +
  21. '<div class="progress__description">&nbsp;</div>' +
  22. '</div>';
  23. };
  24. /**
  25. * A progressbar object. Initialized with the given id. Must be inserted into
  26. * the DOM afterwards through progressBar.element.
  27. *
  28. * Method is the function which will perform the HTTP request to get the
  29. * progress bar state. Either "GET" or "POST".
  30. *
  31. * @example
  32. * pb = new Drupal.ProgressBar('myProgressBar');
  33. * some_element.appendChild(pb.element);
  34. *
  35. * @constructor
  36. *
  37. * @param {string} id
  38. * The id for the progressbar.
  39. * @param {function} updateCallback
  40. * Callback to run on update.
  41. * @param {string} method
  42. * HTTP method to use.
  43. * @param {function} errorCallback
  44. * Callback to call on error.
  45. */
  46. Drupal.ProgressBar = function (id, updateCallback, method, errorCallback) {
  47. this.id = id;
  48. this.method = method || 'GET';
  49. this.updateCallback = updateCallback;
  50. this.errorCallback = errorCallback;
  51. // The WAI-ARIA setting aria-live="polite" will announce changes after
  52. // users
  53. // have completed their current activity and not interrupt the screen
  54. // reader.
  55. this.element = $(Drupal.theme('progressBar', id));
  56. };
  57. $.extend(Drupal.ProgressBar.prototype, /** @lends Drupal.ProgressBar# */{
  58. /**
  59. * Set the percentage and status message for the progressbar.
  60. *
  61. * @param {number} percentage
  62. * The progress percentage.
  63. * @param {string} message
  64. * The message to show the user.
  65. * @param {string} label
  66. * The text for the progressbar label.
  67. */
  68. setProgress: function (percentage, message, label) {
  69. if (percentage >= 0 && percentage <= 100) {
  70. $(this.element).find('div.progress__bar').css('width', percentage + '%');
  71. $(this.element).find('div.progress__percentage').html(percentage + '%');
  72. }
  73. $('div.progress__description', this.element).html(message);
  74. $('div.progress__label', this.element).html(label);
  75. if (this.updateCallback) {
  76. this.updateCallback(percentage, message, this);
  77. }
  78. },
  79. /**
  80. * Start monitoring progress via Ajax.
  81. *
  82. * @param {string} uri
  83. * The URI to use for monitoring.
  84. * @param {number} delay
  85. * The delay for calling the monitoring URI.
  86. */
  87. startMonitoring: function (uri, delay) {
  88. this.delay = delay;
  89. this.uri = uri;
  90. this.sendPing();
  91. },
  92. /**
  93. * Stop monitoring progress via Ajax.
  94. */
  95. stopMonitoring: function () {
  96. clearTimeout(this.timer);
  97. // This allows monitoring to be stopped from within the callback.
  98. this.uri = null;
  99. },
  100. /**
  101. * Request progress data from server.
  102. */
  103. sendPing: function () {
  104. if (this.timer) {
  105. clearTimeout(this.timer);
  106. }
  107. if (this.uri) {
  108. var pb = this;
  109. // When doing a post request, you need non-null data. Otherwise a
  110. // HTTP 411 or HTTP 406 (with Apache mod_security) error may result.
  111. var uri = this.uri;
  112. if (uri.indexOf('?') === -1) {
  113. uri += '?';
  114. }
  115. else {
  116. uri += '&';
  117. }
  118. uri += '_format=json';
  119. $.ajax({
  120. type: this.method,
  121. url: uri,
  122. data: '',
  123. dataType: 'json',
  124. success: function (progress) {
  125. // Display errors.
  126. if (progress.status === 0) {
  127. pb.displayError(progress.data);
  128. return;
  129. }
  130. // Update display.
  131. pb.setProgress(progress.percentage, progress.message, progress.label);
  132. // Schedule next timer.
  133. pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay);
  134. },
  135. error: function (xmlhttp) {
  136. var e = new Drupal.AjaxError(xmlhttp, pb.uri);
  137. pb.displayError('<pre>' + e.message + '</pre>');
  138. }
  139. });
  140. }
  141. },
  142. /**
  143. * Display errors on the page.
  144. *
  145. * @param {string} string
  146. * The error message to show the user.
  147. */
  148. displayError: function (string) {
  149. var error = $('<div class="messages messages--error"></div>').html(string);
  150. $(this.element).before(error).hide();
  151. if (this.errorCallback) {
  152. this.errorCallback(this);
  153. }
  154. }
  155. });
  156. })(jQuery, Drupal);