From 7207858094cee11e81fa2d0a4808a662bcdac2ec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?"J.=20Rene=CC=81e=20Beach"?= <splendidnoise@gmail.com>
Date: Wed, 13 Feb 2013 15:00:59 -0500
Subject: [PATCH] Issue #1915302 by jessebeach: Consolidate the various uses
 of the aria-live region into an API that modules developers
 can leverage
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: J. Renée Beach <splendidnoise@gmail.com>
---
 core/misc/drupal.js |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/core/misc/drupal.js b/core/misc/drupal.js
index 627e264..d848a83 100644
--- a/core/misc/drupal.js
+++ b/core/misc/drupal.js
@@ -259,6 +259,76 @@ Drupal.t = function (str, args, options) {
 };
 
 /**
+ * Adds an HTML element and method to trigger audio UAs to read system messages.
+ */
+(function (document, Drupal) {
+
+  var liveElement;
+  var ariaLiveAttr;
+  var ariaBusyAttr;
+
+  Drupal.behaviors.drupalAnnounce = {
+    attach: function (settings, context) {
+      liveElement = document.createElement('div');
+      liveElement.id = "drupal-live-announce";
+      liveElement.classList.add('element-invisible');
+
+      ariaLiveAttr = document.createAttribute('aria-live');
+      ariaLiveAttr.value = 'polite';
+      liveElement.setAttributeNode(ariaLiveAttr);
+
+      ariaBusyAttr = document.createAttribute('aria-busy');
+      ariaBusyAttr.value = false;
+      liveElement.setAttributeNode(ariaBusyAttr);
+
+      document.body.appendChild(liveElement);
+    }
+  };
+
+  /**
+   * Triggers audio UAs to read the supplied text.
+   *
+   * @param {String} text
+   *   - A string to be read by the UA.
+   *
+   * @param {String} priority
+   *   - A string to indicate the priority of the message. Can be either
+   *   'polite' or 'assertive'. Polite is the default.
+   *
+   * @see http://www.w3.org/WAI/PF/aria-practices/#liveprops
+   */
+  Drupal.announce = function (text, priority) {
+    if (typeof text === 'string') {
+      // Clear the liveElement so that repeated strings will be read.
+      liveElement.innerHTML = '';
+
+      // Set the busy state to true until the node changes are complete.
+      ariaBusyAttr.value = true;
+      liveElement.setAttributeNode(ariaBusyAttr);
+
+      // Set the priority to assertive.
+      if (priority === 'assertive' && ariaLiveAttr.value !== 'assertive') {
+        ariaLiveAttr.value = 'assertive';
+        liveElement.setAttributeNode(ariaLiveAttr);
+      }
+      // Set the priority back to 'polite' if it not already and it is not
+      // 'assertive'.
+      else if (ariaLiveAttr.value !== 'polite') {
+        ariaLiveAttr.value = 'polite';
+        liveElement.setAttributeNode(ariaLiveAttr);
+      }
+
+      // Print the text to the live region.
+      liveElement.innerHTML = Drupal.checkPlain(text);
+
+      // The live text area is updated. Allow the AT to announce the text.
+      ariaBusyAttr.value = false;
+      liveElement.setAttributeNode(ariaBusyAttr);
+    }
+  };
+}(document, Drupal));
+
+/**
  * Returns the URL to a Drupal page.
  */
 Drupal.url = function (path) {
-- 
1.7.10.4

