/
focusManager.js
205 lines (165 loc) · 4.98 KB
/
focusManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.focusManager = factory();
}
}(this, function() {
"use strict";
function isAncestor(ancestor, descendant) {
var element = descendant;
while (element) {
if (element === ancestor) {
return true;
}
element = element.parentElement;
}
return false;
}
function isDisabled(element) {
return element.disabled === true;
}
function isFocusable(element) {
return Boolean(element) && element.tabIndex >= 0 && !isDisabled(element);
}
function makeFocusable(element) {
// A tabIndex is needed to make the element focusable
// A tabIndex of -1 means that the element is only programmatically focusable
if (isDisabled(element)) {
element.disabled = false;
}
if (!isFocusable(element)) {
element.tabIndex = -1;
}
}
// Find the first focusable element.
// The candidates are the element and it's descendants.
// The search is performed depth-first.
function findFirstFocusableElement(element) {
if (isFocusable(element)) {
return element;
}
var children = element.children;
var length = children.length;
var child;
var focusableDescendant;
for (var i = 0; i < length; i += 1) {
child = children[i];
focusableDescendant = findFirstFocusableElement(child);
if (focusableDescendant) {
return focusableDescendant;
}
}
return null;
}
// Find the first focusable element.
// The candidates are the element and it's descendants.
// The search is performed depth-first.
function findLastFocusableElement(element) {
var children = element.children;
var length = children.length;
var child;
var focusableDescendant;
for (var i = length -1; i >= 0; i -= 1) {
child = children[i];
focusableDescendant = findLastFocusableElement(child);
if (focusableDescendant) {
return focusableDescendant;
}
}
if (isFocusable(element)) {
return element;
}
return null;
}
function focus(element) {
makeFocusable(element);
element.focus();
state.lastFocus = element;
}
function resolveFocus(parent, defaultFocus) {
var focusElement;
if (defaultFocus) {
focusElement = defaultFocus;
} else {
focusElement = findFirstFocusableElement(parent) || parent;
if (focusElement === state.lastFocus) {
focusElement = findLastFocusableElement(parent) || parent;
}
}
focus(focusElement);
}
function focusFirstInElement(element) {
var focusElement = findFirstFocusableElement(element) || element;
focus(focusElement);
}
function focusLastInElement(element) {
var focusElement = findLastFocusableElement(element) || element;
focus(focusElement);
}
// State is kept is these variables.
// Since only one modal dialog can capture focus at a time the state is a singleton.
var state = {
eventListenerArguments: null,
eventListenerContext: null,
lastFocus: null
};
function releaseModalFocus(focusElement) {
var eventListenerContext = state.eventListenerContext;
var eventListenerArguments = state.eventListenerArguments;
if (eventListenerContext && eventListenerArguments) {
eventListenerContext.removeEventListener.apply(eventListenerContext, eventListenerArguments);
}
// Reset the state object
state.eventListenerContext = null;
state.eventListenerArguments = null;
state.lastFocus = null;
if (focusElement) {
focusElement.focus();
}
}
// Keep focus inside the modal
function restrictFocus(modal, focusedElement) {
if (isAncestor(modal, focusedElement)) {
state.lastFocus = focusedElement;
} else {
resolveFocus(modal);
}
}
// modal, the element in which to contain focus
// focusElement (optional), the element inside the modal to focus when opening
// backgroundElement (optional), All focus events within this element are redirected to the modal. Defaults to document
function captureModalFocus(modal, focusElement, backgroundElement) {
// without a modal there is nothing to capture
if (!modal) {
return null;
}
// If any focus is already being captured, release it now
releaseModalFocus();
// focus the modal so the user knows it was opened
resolveFocus(modal, focusElement);
// Whenever an element outside of the modal is focused, the modal is focused instead
function focusCallback(evnt) {
restrictFocus(modal, evnt.target);
}
// The focus event does not bubble
// however it can be captured on an ancestor element
// by setting useCapture to true
var eventListenerContext = backgroundElement || document;
var eventListenerArguments = ["focus", focusCallback, true];
// Save the eventListener data in the state object so it can be removed later
// by the releaseModalFocus function
state.eventListenerContext = eventListenerContext;
state.eventListenerArguments = eventListenerArguments;
eventListenerContext.addEventListener.apply(eventListenerContext, eventListenerArguments);
}
var focusManager = {
capture: captureModalFocus,
release: releaseModalFocus,
focusFirstInElement: focusFirstInElement,
focusLastInElement: focusLastInElement
};
return focusManager;
}));