-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathfocus-manager.js
More file actions
361 lines (320 loc) · 8.23 KB
/
focus-manager.js
File metadata and controls
361 lines (320 loc) · 8.23 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
var $ = jQuery;
/**
* wp.media.view.FocusManager
*
* @memberOf wp.media.view
*
* @class
* @augments wp.media.View
* @augments wp.Backbone.View
* @augments Backbone.View
*/
var FocusManager = wp.media.View.extend(/** @lends wp.media.view.FocusManager.prototype */{
events: {
'keydown': 'focusManagementMode'
},
/**
* Initializes the Focus Manager.
*
* @param {Object} options The Focus Manager options.
*
* @since 5.3.0
*
* @return {void}
*/
initialize: function( options ) {
this.mode = options.mode || 'constrainTabbing';
this.tabsAutomaticActivation = options.tabsAutomaticActivation || false;
},
/**
* Determines which focus management mode to use.
*
* @since 5.3.0
*
* @param {Object} event jQuery event object.
*
* @return {void}
*/
focusManagementMode: function( event ) {
if ( this.mode === 'constrainTabbing' ) {
this.constrainTabbing( event );
}
if ( this.mode === 'tabsNavigation' ) {
this.tabsNavigation( event );
}
},
/**
* Gets all the tabbable elements.
*
* @since 5.3.0
*
* @return {Object} A jQuery collection of tabbable elements.
*/
getTabbables: function() {
// Skip the file input added by Plupload.
return this.$( ':tabbable' ).not( '.moxie-shim input[type="file"]' );
},
/**
* Moves focus to the modal dialog.
*
* @since 3.5.0
*
* @return {void}
*/
focus: function() {
this.$( '.media-modal' ).trigger( 'focus' );
},
/**
* Constrains navigation with the Tab key within the media view element.
*
* @since 4.0.0
*
* @param {Object} event A keydown jQuery event.
*
* @return {void}
*/
constrainTabbing: function( event ) {
var tabbables;
// Look for the tab key.
if ( 'Tab' !== event.key ) {
return;
}
tabbables = this.getTabbables();
// Keep tab focus within media modal while it's open.
if ( tabbables.last()[0] === event.target && ! event.shiftKey ) {
tabbables.first().focus();
return false;
} else if ( tabbables.first()[0] === event.target && event.shiftKey ) {
tabbables.last().focus();
return false;
}
},
/**
* Hides from assistive technologies all the body children.
*
* Sets an `aria-hidden="true"` attribute on all the body children except
* the provided element and other elements that should not be hidden.
*
* The reason why we use `aria-hidden` is that `aria-modal="true"` is buggy
* in Safari 11.1 and support is spotty in other browsers. Also, `aria-modal="true"`
* prevents the `wp.a11y.speak()` ARIA live regions to work as they're outside
* of the modal dialog and get hidden from assistive technologies.
*
* @since 5.2.3
*
* @param {Object} visibleElement The jQuery object representing the element that should not be hidden.
*
* @return {void}
*/
setAriaHiddenOnBodyChildren: function( visibleElement ) {
var bodyChildren,
self = this;
if ( this.isBodyAriaHidden ) {
return;
}
// Get all the body children.
bodyChildren = document.body.children;
// Loop through the body children and hide the ones that should be hidden.
_.each( bodyChildren, function( element ) {
// Don't hide the modal element.
if ( element === visibleElement[0] ) {
return;
}
// Determine the body children to hide.
if ( self.elementShouldBeHidden( element ) ) {
element.setAttribute( 'aria-hidden', 'true' );
// Store the hidden elements.
self.ariaHiddenElements.push( element );
}
} );
this.isBodyAriaHidden = true;
},
/**
* Unhides from assistive technologies all the body children.
*
* Makes visible again to assistive technologies all the body children
* previously hidden and stored in this.ariaHiddenElements.
*
* @since 5.2.3
*
* @return {void}
*/
removeAriaHiddenFromBodyChildren: function() {
_.each( this.ariaHiddenElements, function( element ) {
element.removeAttribute( 'aria-hidden' );
} );
this.ariaHiddenElements = [];
this.isBodyAriaHidden = false;
},
/**
* Determines if the passed element should not be hidden from assistive technologies.
*
* @since 5.2.3
*
* @param {Object} element The DOM element that should be checked.
*
* @return {boolean} Whether the element should not be hidden from assistive technologies.
*/
elementShouldBeHidden: function( element ) {
var role = element.getAttribute( 'role' ),
liveRegionsRoles = [ 'alert', 'status', 'log', 'marquee', 'timer' ];
/*
* Don't hide scripts, elements that already have `aria-hidden`, and
* ARIA live regions.
*/
return ! (
element.tagName === 'SCRIPT' ||
element.hasAttribute( 'aria-hidden' ) ||
element.hasAttribute( 'aria-live' ) ||
liveRegionsRoles.indexOf( role ) !== -1
);
},
/**
* Whether the body children are hidden from assistive technologies.
*
* @since 5.2.3
*/
isBodyAriaHidden: false,
/**
* Stores an array of DOM elements that should be hidden from assistive
* technologies, for example when the media modal dialog opens.
*
* @since 5.2.3
*/
ariaHiddenElements: [],
/**
* Holds the jQuery collection of ARIA tabs.
*
* @since 5.3.0
*/
tabs: $(),
/**
* Sets up tabs in an ARIA tabbed interface.
*
* @since 5.3.0
*
* @param {Object} event jQuery event object.
*
* @return {void}
*/
setupAriaTabs: function() {
this.tabs = this.$( '[role="tab"]' );
// Set up initial attributes.
this.tabs.attr( {
'aria-selected': 'false',
tabIndex: '-1'
} );
// Set up attributes on the initially active tab.
this.tabs.filter( '.active' )
.removeAttr( 'tabindex' )
.attr( 'aria-selected', 'true' );
},
/**
* Enables arrows navigation within the ARIA tabbed interface.
*
* @since 5.3.0
*
* @param {Object} event jQuery event object.
*
* @return {void}
*/
tabsNavigation: function( event ) {
var orientation = 'horizontal',
keys = [ 32, 35, 36, 37, 38, 39, 40 ];
// Return if not Spacebar, End, Home, or Arrow keys.
if ( keys.indexOf( event.which ) === -1 ) {
return;
}
// Determine navigation direction.
if ( this.$el.attr( 'aria-orientation' ) === 'vertical' ) {
orientation = 'vertical';
}
// Make Up and Down arrow keys do nothing with horizontal tabs.
if ( orientation === 'horizontal' && [ 38, 40 ].indexOf( event.which ) !== -1 ) {
return;
}
// Make Left and Right arrow keys do nothing with vertical tabs.
if ( orientation === 'vertical' && [ 37, 39 ].indexOf( event.which ) !== -1 ) {
return;
}
this.switchTabs( event, this.tabs );
},
/**
* Switches tabs in the ARIA tabbed interface.
*
* @since 5.3.0
*
* @param {Object} event jQuery event object.
*
* @return {void}
*/
switchTabs: function( event ) {
var key = event.which,
index = this.tabs.index( $( event.target ) ),
newIndex;
switch ( key ) {
// Space bar: Activate current targeted tab.
case 32: {
this.activateTab( this.tabs[ index ] );
break;
}
// End key: Activate last tab.
case 35: {
event.preventDefault();
this.activateTab( this.tabs[ this.tabs.length - 1 ] );
break;
}
// Home key: Activate first tab.
case 36: {
event.preventDefault();
this.activateTab( this.tabs[ 0 ] );
break;
}
// Left and up keys: Activate previous tab.
case 37:
case 38: {
event.preventDefault();
newIndex = ( index - 1 ) < 0 ? this.tabs.length - 1 : index - 1;
this.activateTab( this.tabs[ newIndex ] );
break;
}
// Right and down keys: Activate next tab.
case 39:
case 40: {
event.preventDefault();
newIndex = ( index + 1 ) === this.tabs.length ? 0 : index + 1;
this.activateTab( this.tabs[ newIndex ] );
break;
}
}
},
/**
* Sets a single tab to be focusable and semantically selected.
*
* @since 5.3.0
*
* @param {Object} tab The tab DOM element.
*
* @return {void}
*/
activateTab: function( tab ) {
if ( ! tab ) {
return;
}
// The tab is a DOM element: no need for jQuery methods.
tab.focus();
// Handle automatic activation.
if ( this.tabsAutomaticActivation ) {
tab.removeAttribute( 'tabindex' );
tab.setAttribute( 'aria-selected', 'true' );
tab.click();
return;
}
// Handle manual activation.
$( tab ).on( 'click', function() {
tab.removeAttribute( 'tabindex' );
tab.setAttribute( 'aria-selected', 'true' );
} );
}
});
module.exports = FocusManager;