announce.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file
  3. * Adds an HTML element and method to trigger audio UAs to read system messages.
  4. *
  5. * Use {@link Drupal.announce} to indicate to screen reader users that an
  6. * element on the page has changed state. For instance, if clicking a link
  7. * loads 10 more items into a list, one might announce the change like this.
  8. *
  9. * @example
  10. * $('#search-list')
  11. * .on('itemInsert', function (event, data) {
  12. * // Insert the new items.
  13. * $(data.container.el).append(data.items.el);
  14. * // Announce the change to the page contents.
  15. * Drupal.announce(Drupal.t('@count items added to @container',
  16. * {'@count': data.items.length, '@container': data.container.title}
  17. * ));
  18. * });
  19. */
  20. (function (Drupal, debounce) {
  21. 'use strict';
  22. var liveElement;
  23. var announcements = [];
  24. /**
  25. * Builds a div element with the aria-live attribute and add it to the DOM.
  26. *
  27. * @type {Drupal~behavior}
  28. *
  29. * @prop {Drupal~behaviorAttach} attach
  30. * Attaches the behavior for drupalAnnouce.
  31. */
  32. Drupal.behaviors.drupalAnnounce = {
  33. attach: function (context) {
  34. // Create only one aria-live element.
  35. if (!liveElement) {
  36. liveElement = document.createElement('div');
  37. liveElement.id = 'drupal-live-announce';
  38. liveElement.className = 'visually-hidden';
  39. liveElement.setAttribute('aria-live', 'polite');
  40. liveElement.setAttribute('aria-busy', 'false');
  41. document.body.appendChild(liveElement);
  42. }
  43. }
  44. };
  45. /**
  46. * Concatenates announcements to a single string; appends to the live region.
  47. */
  48. function announce() {
  49. var text = [];
  50. var priority = 'polite';
  51. var announcement;
  52. // Create an array of announcement strings to be joined and appended to the
  53. // aria live region.
  54. var il = announcements.length;
  55. for (var i = 0; i < il; i++) {
  56. announcement = announcements.pop();
  57. text.unshift(announcement.text);
  58. // If any of the announcements has a priority of assertive then the group
  59. // of joined announcements will have this priority.
  60. if (announcement.priority === 'assertive') {
  61. priority = 'assertive';
  62. }
  63. }
  64. if (text.length) {
  65. // Clear the liveElement so that repeated strings will be read.
  66. liveElement.innerHTML = '';
  67. // Set the busy state to true until the node changes are complete.
  68. liveElement.setAttribute('aria-busy', 'true');
  69. // Set the priority to assertive, or default to polite.
  70. liveElement.setAttribute('aria-live', priority);
  71. // Print the text to the live region. Text should be run through
  72. // Drupal.t() before being passed to Drupal.announce().
  73. liveElement.innerHTML = text.join('\n');
  74. // The live text area is updated. Allow the AT to announce the text.
  75. liveElement.setAttribute('aria-busy', 'false');
  76. }
  77. }
  78. /**
  79. * Triggers audio UAs to read the supplied text.
  80. *
  81. * The aria-live region will only read the text that currently populates its
  82. * text node. Replacing text quickly in rapid calls to announce results in
  83. * only the text from the most recent call to {@link Drupal.announce} being
  84. * read. By wrapping the call to announce in a debounce function, we allow for
  85. * time for multiple calls to {@link Drupal.announce} to queue up their
  86. * messages. These messages are then joined and append to the aria-live region
  87. * as one text node.
  88. *
  89. * @param {string} text
  90. * A string to be read by the UA.
  91. * @param {string} [priority='polite']
  92. * A string to indicate the priority of the message. Can be either
  93. * 'polite' or 'assertive'.
  94. *
  95. * @return {function}
  96. * The return of the call to debounce.
  97. *
  98. * @see http://www.w3.org/WAI/PF/aria-practices/#liveprops
  99. */
  100. Drupal.announce = function (text, priority) {
  101. // Save the text and priority into a closure variable. Multiple simultaneous
  102. // announcements will be concatenated and read in sequence.
  103. announcements.push({
  104. text: text,
  105. priority: priority
  106. });
  107. // Immediately invoke the function that debounce returns. 200 ms is right at
  108. // the cusp where humans notice a pause, so we will wait
  109. // at most this much time before the set of queued announcements is read.
  110. return (debounce(announce, 200)());
  111. };
  112. }(Drupal, Drupal.debounce));