| 1 |
// Copyright 2011 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 |
/** |
| 16 |
* @fileoverview Helper object used by modal elements to control aria |
| 17 |
* visibility of the rest of the page. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.ui.ModalAriaVisibilityHelper'); |
| 21 |
|
| 22 |
goog.require('goog.a11y.aria'); |
| 23 |
goog.require('goog.a11y.aria.State'); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Helper object to control aria visibility of the rest of the page (background) |
| 29 |
* for a given element. Example usage is to restrict screenreader focus to |
| 30 |
* a modal popup while it is visible. |
| 31 |
* |
| 32 |
* WARNING: This will work only if the element is rendered directly in the |
| 33 |
* 'body' element. |
| 34 |
* |
| 35 |
* @param {!Element} element The given element. |
| 36 |
* @param {!goog.dom.DomHelper} domHelper DomHelper for the page. |
| 37 |
* @constructor |
| 38 |
*/ |
| 39 |
goog.ui.ModalAriaVisibilityHelper = function(element, domHelper) { |
| 40 |
/** |
| 41 |
* @private {!Element} |
| 42 |
*/ |
| 43 |
this.element_ = element; |
| 44 |
|
| 45 |
/** |
| 46 |
* @private {!goog.dom.DomHelper} |
| 47 |
*/ |
| 48 |
this.dom_ = domHelper; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* The elements set to aria-hidden when the popup was made visible. |
| 54 |
* @type {Array<!Element>} |
| 55 |
* @private |
| 56 |
*/ |
| 57 |
goog.ui.ModalAriaVisibilityHelper.prototype.hiddenElements_; |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Sets aria-hidden on the rest of the page to restrict screen reader focus. |
| 62 |
* Top-level elements with an explicit aria-hidden state are not altered. |
| 63 |
* @param {boolean} hide Whether to hide or show the rest of the page. |
| 64 |
*/ |
| 65 |
goog.ui.ModalAriaVisibilityHelper.prototype.setBackgroundVisibility = function( |
| 66 |
hide) { |
| 67 |
if (hide) { |
| 68 |
if (!this.hiddenElements_) { |
| 69 |
this.hiddenElements_ = []; |
| 70 |
} |
| 71 |
var topLevelChildren = this.dom_.getChildren(this.dom_.getDocument().body); |
| 72 |
for (var i = 0; i < topLevelChildren.length; i++) { |
| 73 |
var child = topLevelChildren[i]; |
| 74 |
if (child != this.element_ && |
| 75 |
!goog.a11y.aria.getState(child, goog.a11y.aria.State.HIDDEN)) { |
| 76 |
goog.a11y.aria.setState(child, goog.a11y.aria.State.HIDDEN, true); |
| 77 |
this.hiddenElements_.push(child); |
| 78 |
} |
| 79 |
} |
| 80 |
} else if (this.hiddenElements_) { |
| 81 |
for (var i = 0; i < this.hiddenElements_.length; i++) { |
| 82 |
goog.a11y.aria.removeState( |
| 83 |
this.hiddenElements_[i], goog.a11y.aria.State.HIDDEN); |
| 84 |
} |
| 85 |
this.hiddenElements_ = null; |
| 86 |
} |
| 87 |
}; |