announcer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Announcer that allows messages to be spoken by assistive
  16. * technologies.
  17. */
  18. goog.provide('goog.a11y.aria.Announcer');
  19. goog.require('goog.Disposable');
  20. goog.require('goog.Timer');
  21. goog.require('goog.a11y.aria');
  22. goog.require('goog.a11y.aria.LivePriority');
  23. goog.require('goog.a11y.aria.State');
  24. goog.require('goog.dom');
  25. goog.require('goog.dom.TagName');
  26. goog.require('goog.object');
  27. /**
  28. * Class that allows messages to be spoken by assistive technologies that the
  29. * user may have active.
  30. *
  31. * @param {goog.dom.DomHelper=} opt_domHelper DOM helper.
  32. * @constructor
  33. * @extends {goog.Disposable}
  34. * @final
  35. */
  36. goog.a11y.aria.Announcer = function(opt_domHelper) {
  37. goog.a11y.aria.Announcer.base(this, 'constructor');
  38. /**
  39. * @type {goog.dom.DomHelper}
  40. * @private
  41. */
  42. this.domHelper_ = opt_domHelper || goog.dom.getDomHelper();
  43. /**
  44. * Map of priority to live region elements to use for communicating updates.
  45. * Elements are created on demand.
  46. * @type {Object<goog.a11y.aria.LivePriority, !Element>}
  47. * @private
  48. */
  49. this.liveRegions_ = {};
  50. };
  51. goog.inherits(goog.a11y.aria.Announcer, goog.Disposable);
  52. /** @override */
  53. goog.a11y.aria.Announcer.prototype.disposeInternal = function() {
  54. goog.object.forEach(
  55. this.liveRegions_, this.domHelper_.removeNode, this.domHelper_);
  56. this.liveRegions_ = null;
  57. this.domHelper_ = null;
  58. goog.a11y.aria.Announcer.base(this, 'disposeInternal');
  59. };
  60. /**
  61. * Announce a message to be read by any assistive technologies the user may
  62. * have active.
  63. * @param {string} message The message to announce to screen readers.
  64. * @param {goog.a11y.aria.LivePriority=} opt_priority The priority of the
  65. * message. Defaults to POLITE.
  66. */
  67. goog.a11y.aria.Announcer.prototype.say = function(message, opt_priority) {
  68. var priority = opt_priority || goog.a11y.aria.LivePriority.POLITE;
  69. var liveRegion = this.getLiveRegion_(priority);
  70. // Resets text content to force a DOM mutation (so that the setTextContent
  71. // post-timeout function will be noticed by the screen reader). This is to
  72. // avoid the problem of when the same message is "said" twice, which doesn't
  73. // trigger a DOM mutation.
  74. goog.dom.setTextContent(liveRegion, '');
  75. // Uses non-zero timer to make VoiceOver and NVDA work
  76. goog.Timer.callOnce(function() {
  77. goog.dom.setTextContent(liveRegion, message);
  78. }, 1);
  79. };
  80. /**
  81. * Returns an aria-live region that can be used to communicate announcements.
  82. * @param {!goog.a11y.aria.LivePriority} priority The required priority.
  83. * @return {!Element} A live region of the requested priority.
  84. * @private
  85. */
  86. goog.a11y.aria.Announcer.prototype.getLiveRegion_ = function(priority) {
  87. var liveRegion = this.liveRegions_[priority];
  88. if (liveRegion) {
  89. // Make sure the live region is not aria-hidden.
  90. goog.a11y.aria.removeState(liveRegion, goog.a11y.aria.State.HIDDEN);
  91. return liveRegion;
  92. }
  93. liveRegion = this.domHelper_.createElement(goog.dom.TagName.DIV);
  94. // Note that IE has a habit of declaring things that aren't display:none as
  95. // invisible to third-party tools like JAWs, so we can't just use height:0.
  96. liveRegion.style.position = 'absolute';
  97. liveRegion.style.top = '-1000px';
  98. liveRegion.style.height = '1px';
  99. liveRegion.style.overflow = 'hidden';
  100. goog.a11y.aria.setState(liveRegion, goog.a11y.aria.State.LIVE, priority);
  101. goog.a11y.aria.setState(liveRegion, goog.a11y.aria.State.ATOMIC, 'true');
  102. this.domHelper_.getDocument().body.appendChild(liveRegion);
  103. this.liveRegions_[priority] = liveRegion;
  104. return liveRegion;
  105. };