mirror of https://github.com/ElemeFE/element
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
import Utils from '../aria-utils';
|
|
|
|
var Menu = function(parent, domNode) {
|
|
this.domNode = domNode;
|
|
this.parent = parent;
|
|
this.subMenuItems = [];
|
|
this.subIndex = 0;
|
|
this.init();
|
|
};
|
|
|
|
Menu.prototype.init = function() {
|
|
this.subMenuItems = this.domNode.querySelectorAll('li');
|
|
this.addListeners();
|
|
};
|
|
|
|
Menu.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;
|
|
};
|
|
|
|
Menu.prototype.addListeners = function() {
|
|
const keys = Utils.keys;
|
|
const parentNode = this.parent.domNode;
|
|
Array.prototype.forEach.call(this.subMenuItems, el => {
|
|
el.addEventListener('keydown', event => {
|
|
let 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:
|
|
Utils.triggerEvent(parentNode, 'mouseleave');
|
|
break;
|
|
case keys.enter:
|
|
case keys.space:
|
|
prevdef = true;
|
|
event.currentTarget.click();
|
|
break;
|
|
}
|
|
if (prevdef) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
return false;
|
|
});
|
|
});
|
|
};
|
|
|
|
export default Menu;
|