forked from ibm-js/delite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11yclick.js
95 lines (84 loc) · 2.98 KB
/
a11yclick.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
/**
* @module delite/a11yclick
*/
define([
"./on"
], function (on) {
// TODO: add functional tests
function clickKey(/*Event*/ e) {
// Test if this keyboard event should be tracked as the start (if keydown) or end (if keyup) of a click event.
// Only track for nodes marked to be tracked, and not for buttons or inputs,
// since buttons handle keyboard click natively, and text inputs should not
// prevent typing spaces or newlines.
if ((e.key === "Enter" || e.key === "Spacebar") &&
!/^(input|button|textarea)$/i.test(e.target.nodeName)) {
// Test if a node or its ancestor has been marked with the d-keyboard-click property
// to indicate special processing
for (var node = e.target; node; node = node.parentNode) {
if (node.hasAttribute && node.hasAttribute("d-keyboard-click")) {
return true;
}
}
}
}
var lastKeyDownNode;
on(document, "keydown", function (e) {
//console.log("a11yclick: onkeydown, e.target = ", e.target, ", lastKeyDownNode was ",
// lastKeyDownNode, ", equality is ", (e.target === lastKeyDownNode));
if (!e.defaultPrevented && clickKey(e)) {
// needed on IE for when focus changes between keydown and keyup - otherwise dropdown menus do not work
lastKeyDownNode = e.target;
// prevent scroll
e.preventDefault();
} else {
lastKeyDownNode = null;
}
});
on(document, "keyup", function (e) {
//console.log("a11yclick: onkeyup, e.target = ", e.target, ", lastKeyDownNode was ",
// lastKeyDownNode, ", equality is ", (e.target === lastKeyDownNode));
if (clickKey(e) && e.target === lastKeyDownNode) {
// need reset here or have problems in FF when focus returns to trigger element after closing popup/alert
lastKeyDownNode = null;
// prevent scroll
e.preventDefault();
var doc = e.target.ownerDocument,
clickEvent = doc.createEvent("MouseEvents");
clickEvent.initMouseEvent(
"click",
true,
true,
doc.defaultView,
0,
0,
0,
0,
0,
e.ctrlKey,
e.altKey,
e.shiftKey,
e.metaKey,
0,
doc.body // relatedTarget, for mouseout events
);
e.target.dispatchEvent(clickEvent);
}
});
/**
* When this module is loaded, pressing SPACE or ENTER while focused on an Element with a `d-keyboard-click`
* attribute will fire a synthetic click event on that Element. Also works if the event target's ancestor
* has that attribute set.
*
* Usually this functionality is not necessary. Rather, you should just make the focused Element a `<button>`,
* and then the browser does the same thing natively.
* This module is usually only needed when a custom element itself (ex: `<d-my-checkbox>`)
* gets the focus rather than an Element inside of a custom element.
*
* Returns a convenience function to set `d-keyboard-click` on an Element.
* @param {Element} node - Element that can be "clicked" via SPACE/ENTER key (when focused).
* @function module:delite/a11yclick
*/
return function (node) {
node.setAttribute("d-keyboard-click", "true");
};
});