KeyboardNavigation.js
11.4 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* *
*
* (c) 2009-2019 Øystein Moseng
*
* Main keyboard navigation handling.
*
* License: www.highcharts.com/license
*
* */
'use strict';
import H from '../../parts/Globals.js';
import KeyboardNavigationHandler from './KeyboardNavigationHandler.js';
var merge = H.merge,
addEvent = H.addEvent,
win = H.win,
doc = win.document;
/**
* The KeyboardNavigation class, containing the overall keyboard navigation
* logic for the chart.
*
* @requires module:modules/accessibility
*
* @private
* @class
* @param {Highcharts.Chart} chart
* Chart object
* @param {object} components
* Map of component names to AccessibilityComponent objects.
* @name Highcharts.KeyboardNavigation
*/
function KeyboardNavigation(chart, components, order) {
this.init(chart, components, order);
}
KeyboardNavigation.prototype = {
/**
* Initialize the class
* @private
* @param {Highcharts.Chart} chart
* Chart object
* @param {object} components
* Map of component names to AccessibilityComponent objects.
*/
init: function (chart, components) {
var keyboardNavigation = this;
this.chart = chart;
this.components = components;
this.modules = [];
this.currentModuleIx = 0;
// Make chart container reachable by tab
if (!chart.container.hasAttribute('tabIndex')) {
chart.container.setAttribute('tabindex', '0');
}
// Add exit anchor for focus
this.addExitAnchor();
// Add keydown event
this.unbindKeydownHandler = addEvent(
chart.renderTo, 'keydown', function (e) {
keyboardNavigation.onKeydown(e);
}
);
// Add mouseup event on doc
this.unbindMouseUpHandler = addEvent(doc, 'mouseup', function () {
keyboardNavigation.onMouseUp();
});
// Run an update to get all modules
this.update();
// Init first module
if (this.modules.length) {
this.modules[0].init(1);
}
},
/**
* Update the modules for the keyboard navigation
* @param {Array<string>} order
* Array specifying the tab order of the components.
*/
update: function (order) {
var a11yOptions = this.chart.options.accessibility,
keyboardOptions = a11yOptions && a11yOptions.keyboardNavigation,
components = this.components;
if (
keyboardOptions && keyboardOptions.enabled && order && order.length
) {
// We (still) have keyboard navigation. Update module list
this.modules = order.reduce(function (modules, componentName) {
var navModules = components[componentName]
.getKeyboardNavigation();
// If we didn't get back a list of modules, just push the one
if (!navModules.length) {
modules.push(navModules);
return modules;
}
// Add all of the modules
return modules.concat(navModules);
}, [
// Add an empty module at the start of list, to allow users to
// tab into the chart.
new KeyboardNavigationHandler(this.chart, {})
]);
} else {
// Clear module list and reset
this.modules = [];
this.currentModuleIx = 0;
}
},
/**
* Reset chart navigation state if we click outside the chart and it's
* not already reset.
* @private
*/
onMouseUp: function () {
if (
!this.keyboardReset &&
!(this.chart.pointer && this.chart.pointer.chartPosition)
) {
var chart = this.chart,
curMod = this.modules &&
this.modules[this.currentModuleIx || 0];
if (curMod && curMod.terminate) {
curMod.terminate();
}
if (chart.focusElement) {
chart.focusElement.removeFocusBorder();
}
this.currentModuleIx = 0;
this.keyboardReset = true;
}
},
/**
* Function to run on keydown
* @private
* @param {global.Event} ev
* Browser keydown event
*/
onKeydown: function (ev) {
var e = ev || win.event,
preventDefault,
curNavModule = this.modules && this.modules.length &&
this.modules[this.currentModuleIx];
// Used for resetting nav state when clicking outside chart
this.keyboardReset = false;
// If there is a nav module for the current index, run it.
// Otherwise, we are outside of the chart in some direction.
if (curNavModule) {
var response = curNavModule.run(e);
if (response === curNavModule.response.success) {
preventDefault = true;
} else if (response === curNavModule.response.prev) {
preventDefault = this.prev();
} else if (response === curNavModule.response.next) {
preventDefault = this.next();
}
if (preventDefault) {
e.preventDefault();
}
}
},
/**
* Go to previous module.
* @private
*/
prev: function () {
return this.move(-1);
},
/**
* Go to next module.
* @private
*/
next: function () {
return this.move(1);
},
/**
* Move to prev/next module.
* @private
* @param {number} direction Direction to move. +1 for next, -1 for prev.
* @return {boolean} True if there was a valid module in direction.
*/
move: function (direction) {
var curModule = this.modules && this.modules[this.currentModuleIx];
if (curModule && curModule.terminate) {
curModule.terminate(direction);
}
// Remove existing focus border if any
if (this.chart.focusElement) {
this.chart.focusElement.removeFocusBorder();
}
this.currentModuleIx += direction;
var newModule = this.modules && this.modules[this.currentModuleIx];
if (newModule) {
if (newModule.validate && !newModule.validate()) {
return this.move(direction); // Invalid module, recurse
}
if (newModule.init) {
newModule.init(direction); // Valid module, init it
return true;
}
}
// No module
this.currentModuleIx = 0; // Reset counter
// Set focus to chart or exit anchor depending on direction
if (direction > 0) {
this.exiting = true;
this.exitAnchor.focus();
} else {
this.chart.renderTo.focus();
}
return false;
},
/**
* Add exit anchor to the chart. We use this to move focus out of chart
* whenever we want, by setting focus to this div and not preventing the
* default tab action. We also use this when users come back into the chart
* by tabbing back, in order to navigate from the end of the chart.
*
* Screen reader users can also use heading-shortcuts to jump out of the
* chart with this.
*
* @private
*/
addExitAnchor: function () {
var chart = this.chart,
exitAnchorWrapper = this.exitAnchorWrapper =
doc.createElement('div'),
exitAnchor = this.exitAnchor = doc.createElement('h6'),
keyboardNavigation = this,
exitAnchorLabel = chart.langFormat(
'accessibility.svgContainerEnd', { chart: chart }
);
exitAnchor.innerHTML = exitAnchorLabel;
exitAnchorWrapper.setAttribute('aria-hidden', 'false');
exitAnchorWrapper.setAttribute(
'class', 'highcharts-exit-anchor-wrapper'
);
exitAnchorWrapper.style.position = 'relative';
exitAnchorWrapper.style.outline = 'none';
exitAnchor.setAttribute('tabindex', '0');
exitAnchor.setAttribute('aria-hidden', false);
// Hide exit anchor
merge(true, exitAnchor.style, {
position: 'absolute',
width: '1px',
height: '1px',
bottom: '5px', // Avoid scrollbars (#10637)
zIndex: 0,
overflow: 'hidden',
outline: 'none'
});
exitAnchorWrapper.appendChild(exitAnchor);
chart.renderTo.appendChild(exitAnchorWrapper);
// Update position on render
this.unbindExitAnchorUpdate = addEvent(chart, 'render', function () {
this.renderTo.appendChild(exitAnchorWrapper);
});
// Handle focus
this.unbindExitAnchorFocus = addEvent(
exitAnchor,
'focus',
function (ev) {
var e = ev || win.event,
curModule;
// If focusing and we are exiting, do nothing once.
if (!keyboardNavigation.exiting) {
// Not exiting, means we are coming in backwards
chart.renderTo.focus();
e.preventDefault();
// Move to last valid keyboard nav module
// Note the we don't run it, just set the index
if (
keyboardNavigation.modules &&
keyboardNavigation.modules.length
) {
keyboardNavigation.currentModuleIx =
keyboardNavigation.modules.length - 1;
curModule = keyboardNavigation.modules[
keyboardNavigation.currentModuleIx
];
// Validate the module
if (
curModule &&
curModule.validate && !curModule.validate()
) {
// Invalid. Try moving backwards to find next valid.
keyboardNavigation.prev();
} else if (curModule) {
// We have a valid module, init it
curModule.init(-1);
}
}
} else {
// Don't skip the next focus, we only skip once.
keyboardNavigation.exiting = false;
}
}
);
},
/**
* Remove all traces of keyboard navigation.
* @private
*/
destroy: function () {
// Remove exit anchor
if (this.unbindExitAnchorFocus) {
this.unbindExitAnchorFocus();
delete this.unbindExitAnchorFocus;
}
if (this.unbindExitAnchorUpdate) {
this.unbindExitAnchorUpdate();
delete this.unbindExitAnchorUpdate;
}
if (this.exitAnchorWrapper && this.exitAnchorWrapper.parentNode) {
this.exitAnchorWrapper.parentNode
.removeChild(this.exitAnchorWrapper);
delete this.exitAnchor;
delete this.exitAnchorWrapper;
}
// Remove keydown handler
if (this.unbindKeydownHandler) {
this.unbindKeydownHandler();
}
// Remove mouseup handler
if (this.unbindMouseUpHandler) {
this.unbindMouseUpHandler();
}
}
};
export default KeyboardNavigation;