announce.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, debounce) {
  8. var liveElement = void 0;
  9. var announcements = [];
  10. Drupal.behaviors.drupalAnnounce = {
  11. attach: function attach(context) {
  12. if (!liveElement) {
  13. liveElement = document.createElement('div');
  14. liveElement.id = 'drupal-live-announce';
  15. liveElement.className = 'visually-hidden';
  16. liveElement.setAttribute('aria-live', 'polite');
  17. liveElement.setAttribute('aria-busy', 'false');
  18. document.body.appendChild(liveElement);
  19. }
  20. }
  21. };
  22. function announce() {
  23. var text = [];
  24. var priority = 'polite';
  25. var announcement = void 0;
  26. var il = announcements.length;
  27. for (var i = 0; i < il; i++) {
  28. announcement = announcements.pop();
  29. text.unshift(announcement.text);
  30. if (announcement.priority === 'assertive') {
  31. priority = 'assertive';
  32. }
  33. }
  34. if (text.length) {
  35. liveElement.innerHTML = '';
  36. liveElement.setAttribute('aria-busy', 'true');
  37. liveElement.setAttribute('aria-live', priority);
  38. liveElement.innerHTML = text.join('\n');
  39. liveElement.setAttribute('aria-busy', 'false');
  40. }
  41. }
  42. Drupal.announce = function (text, priority) {
  43. announcements.push({
  44. text: text,
  45. priority: priority
  46. });
  47. return debounce(announce, 200)();
  48. };
  49. })(Drupal, Drupal.debounce);