aria-submenu.js
1.69 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
'use strict';
exports.__esModule = true;
var _ariaUtils = require('../aria-utils');
var _ariaUtils2 = _interopRequireDefault(_ariaUtils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var SubMenu = function SubMenu(parent, domNode) {
this.domNode = domNode;
this.parent = parent;
this.subMenuItems = [];
this.subIndex = 0;
this.init();
};
SubMenu.prototype.init = function () {
this.subMenuItems = this.domNode.querySelectorAll('li');
this.addListeners();
};
SubMenu.prototype.gotoSubIndex = function (idx) {
if (idx === this.subMenuItems.length) {
idx = 0;
} else if (idx < 0) {
idx = this.subMenuItems.length - 1;
}
this.subMenuItems[idx].focus();
this.subIndex = idx;
};
SubMenu.prototype.addListeners = function () {
var _this = this;
var keys = _ariaUtils2.default.keys;
var parentNode = this.parent.domNode;
Array.prototype.forEach.call(this.subMenuItems, function (el) {
el.addEventListener('keydown', function (event) {
var prevDef = false;
switch (event.keyCode) {
case keys.down:
_this.gotoSubIndex(_this.subIndex + 1);
prevDef = true;
break;
case keys.up:
_this.gotoSubIndex(_this.subIndex - 1);
prevDef = true;
break;
case keys.tab:
_ariaUtils2.default.triggerEvent(parentNode, 'mouseleave');
break;
case keys.enter:
case keys.space:
prevDef = true;
event.currentTarget.click();
break;
}
if (prevDef) {
event.preventDefault();
event.stopPropagation();
}
return false;
});
});
};
exports.default = SubMenu;