UNPKG

2.9 kBJavaScriptView Raw
1import $ from './jquery';
2import { getMessageLogger } from './internal/deprecation';
3import { recomputeStyle } from './internal/animation';
4import globalize from './internal/globalize';
5
6var overflowEl;
7var _hiddenByAui = [];
8
9/**
10 * Dims the screen using a blanket div
11 * @param useShim deprecated, it is calculated by dim() now
12 */
13function dim (useShim, zIndex) {
14
15 //if we're blanketing the page it means we want to hide the whatever is under the blanket from the screen readers as well
16 function hasAriaHidden(element) {
17 return element.hasAttribute('aria-hidden');
18 }
19
20 function isAuiLayer(element) {
21 return element.classList && element.classList.contains('aui-layer');
22 }
23
24 Array.prototype.forEach.call(document.body.children, function (element) {
25 if (!hasAriaHidden(element) && !isAuiLayer(element)) {
26 element.setAttribute('aria-hidden', 'true');
27 _hiddenByAui.push(element);
28 }
29 });
30
31 if (!overflowEl) {
32 overflowEl = document.body;
33 }
34
35 if (useShim === true) {
36 useShimDeprecationLogger();
37 }
38
39 var isBlanketShowing = (!!dim.$dim) && !dim.$dim[0].hasAttribute('hidden');
40
41 if (!!dim.$dim) {
42 dim.$dim.remove();
43 dim.$dim = null;
44 }
45
46 dim.$dim = $('<div aria-hidden="true"></div>').addClass('aui-blanket');
47 dim.$dim.attr('tabindex', '0'); //required, or the last element's focusout event will go to the browser
48 dim.$dim.appendTo(document.body);
49
50 if (!isBlanketShowing) {
51 //recompute after insertion and before setting aria-hidden=false to ensure we calculate a difference in
52 //computed styles
53 recomputeStyle(dim.$dim);
54
55 dim.cachedOverflow = {
56 overflow: overflowEl.style.overflow,
57 overflowX: overflowEl.style.overflowX,
58 overflowY: overflowEl.style.overflowY
59 };
60
61 overflowEl.style.overflowX = 'hidden';
62 overflowEl.style.overflowY = 'hidden';
63 overflowEl.style.overflow = 'hidden';
64 }
65
66 dim.$dim.removeAttr('hidden');
67
68 if (zIndex) {
69 dim.$dim.css({zIndex: zIndex});
70 }
71
72 return dim.$dim;
73}
74
75/**
76 * Removes semitransparent DIV
77 * @see dim
78 */
79function undim () {
80 _hiddenByAui.forEach(function (element) {
81 element.removeAttribute('aria-hidden');
82 });
83
84 _hiddenByAui = [];
85
86 if (dim.$dim) {
87 dim.$dim[0].setAttribute('hidden', '');
88
89 if (overflowEl) {
90 overflowEl.style.overflow = dim.cachedOverflow.overflow;
91 overflowEl.style.overflowX = dim.cachedOverflow.overflowX;
92 overflowEl.style.overflowY = dim.cachedOverflow.overflowY;
93 }
94 }
95}
96
97var useShimDeprecationLogger = getMessageLogger('useShim', {
98 extraInfo: 'useShim has no alternative as it is now calculated by dim().'
99});
100
101globalize('dim', dim);
102globalize('undim', undim);
103
104export {
105 dim,
106 undim
107};