aria-menuitem.js
1.28 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
import Utils from '../aria-utils';
import SubMenu from './aria-submenu';
const MenuItem = function(domNode) {
this.domNode = domNode;
this.submenu = null;
this.init();
};
MenuItem.prototype.init = function() {
this.domNode.setAttribute('tabindex', '0');
let menuChild = this.domNode.querySelector('.el-menu');
if (menuChild) {
this.submenu = new SubMenu(this, menuChild);
}
this.addListeners();
};
MenuItem.prototype.addListeners = function() {
const keys = Utils.keys;
this.domNode.addEventListener('keydown', event => {
let prevDef = false;
switch (event.keyCode) {
case keys.down:
Utils.triggerEvent(event.currentTarget, 'mouseenter');
this.submenu && this.submenu.gotoSubIndex(0);
prevDef = true;
break;
case keys.up:
Utils.triggerEvent(event.currentTarget, 'mouseenter');
this.submenu && this.submenu.gotoSubIndex(this.submenu.subMenuItems.length - 1);
prevDef = true;
break;
case keys.tab:
Utils.triggerEvent(event.currentTarget, 'mouseleave');
break;
case keys.enter:
case keys.space:
prevDef = true;
event.currentTarget.click();
break;
}
if (prevDef) {
event.preventDefault();
}
});
};
export default MenuItem;