1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.backLastFocusNode = backLastFocusNode;
|
8 | exports.clearLastFocusNode = clearLastFocusNode;
|
9 | exports.getFocusNodeList = getFocusNodeList;
|
10 | exports.limitTabRange = limitTabRange;
|
11 | exports.saveLastFocusNode = saveLastFocusNode;
|
12 | var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
13 | var _isVisible = _interopRequireDefault(require("./isVisible"));
|
14 | function focusable(node) {
|
15 | var includePositive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
16 | if ((0, _isVisible.default)(node)) {
|
17 | var nodeName = node.nodeName.toLowerCase();
|
18 | var isFocusableElement =
|
19 |
|
20 | ['input', 'select', 'textarea', 'button'].includes(nodeName) ||
|
21 |
|
22 | node.isContentEditable ||
|
23 |
|
24 | nodeName === 'a' && !!node.getAttribute('href');
|
25 |
|
26 |
|
27 | var tabIndexAttr = node.getAttribute('tabindex');
|
28 | var tabIndexNum = Number(tabIndexAttr);
|
29 |
|
30 |
|
31 | var tabIndex = null;
|
32 | if (tabIndexAttr && !Number.isNaN(tabIndexNum)) {
|
33 | tabIndex = tabIndexNum;
|
34 | } else if (isFocusableElement && tabIndex === null) {
|
35 | tabIndex = 0;
|
36 | }
|
37 |
|
38 |
|
39 | if (isFocusableElement && node.disabled) {
|
40 | tabIndex = null;
|
41 | }
|
42 | return tabIndex !== null && (tabIndex >= 0 || includePositive && tabIndex < 0);
|
43 | }
|
44 | return false;
|
45 | }
|
46 | function getFocusNodeList(node) {
|
47 | var includePositive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
48 | var res = (0, _toConsumableArray2.default)(node.querySelectorAll('*')).filter(function (child) {
|
49 | return focusable(child, includePositive);
|
50 | });
|
51 | if (focusable(node, includePositive)) {
|
52 | res.unshift(node);
|
53 | }
|
54 | return res;
|
55 | }
|
56 | var lastFocusElement = null;
|
57 |
|
58 |
|
59 | function saveLastFocusNode() {
|
60 | lastFocusElement = document.activeElement;
|
61 | }
|
62 |
|
63 |
|
64 | function clearLastFocusNode() {
|
65 | lastFocusElement = null;
|
66 | }
|
67 |
|
68 |
|
69 | function backLastFocusNode() {
|
70 | if (lastFocusElement) {
|
71 | try {
|
72 |
|
73 | lastFocusElement.focus();
|
74 |
|
75 |
|
76 | } catch (e) {
|
77 |
|
78 | }
|
79 |
|
80 | }
|
81 | }
|
82 | function limitTabRange(node, e) {
|
83 | if (e.keyCode === 9) {
|
84 | var tabNodeList = getFocusNodeList(node);
|
85 | var lastTabNode = tabNodeList[e.shiftKey ? 0 : tabNodeList.length - 1];
|
86 | var leavingTab = lastTabNode === document.activeElement || node === document.activeElement;
|
87 | if (leavingTab) {
|
88 | var target = tabNodeList[e.shiftKey ? tabNodeList.length - 1 : 0];
|
89 | target.focus();
|
90 | e.preventDefault();
|
91 | }
|
92 | }
|
93 | } |
\ | No newline at end of file |