
function a11yMenuController(opts){this.controllers=[],this.addMenu=function(opts){var menu=new a11yMenu(opts);return this.controllers.push(menu),menu};var that=this;return document.onkeydown=function(evt){if("key"in(evt=evt||window.event)?"Escape"==evt.key||"Esc"==evt.key:27==evt.keyCode){var lastOpened=!1;for(i=0;i<that.controllers.length;i++)(0==lastOpened||lastOpened.openedAt<that.controllers[i].openedAt)&&(lastOpened=that.controllers[i]);lastOpened.closeMenu()}},this}function a11yMenu(opts){this.control=document.querySelector(opts.control),this.container=document.querySelector(opts.container),this.isOpen=!1,this.openedAt=!1,this.focusableElements="a[href]:not([tabindex='-1']), area[href]:not([tabindex='-1']), input:not([disabled]):not([tabindex='-1']), select:not([disabled]):not([tabindex='-1']),textarea:not([disabled]):not([tabindex='-1']), button:not([disabled]):not([tabindex='-1']), iframe:not([tabindex='-1']), [tabindex]:not([tabindex='-1']),[contentEditable=true]:not([tabindex='-1'])";var that=this;return this.container.style.display="none",this.control.addEventListener("keydown",function(e){32!=e.which&&13!=e.which||(e.stopPropagation(),e.preventDefault(),!0===that.isOpen?that.closeMenu():that.openMenu({mode:"keyboard"}))}),this.control.addEventListener("click",function(e){e.stopPropagation(),e.preventDefault(),!0===that.isOpen?that.closeMenu():that.openMenu({mode:"mouse"})}),this.bindExit=function(){var focusableNodeList=this.container.querySelectorAll(this.focusableElements),focusItemsArray=[].slice.call(focusableNodeList);focusableNodeList[focusableNodeList.length-1].addEventListener("blur",function(e){-1==focusItemsArray.indexOf(e.relatedTarget)&&that.closeMenu()})},this.openMenu=function(opts){this.isOpen||(this.container.style.display="block",this.control.setAttribute("aria-expanded","true"),"keyboard"==opts.mode&&(this.container.querySelector("a:first-child").focus(),this.bindExit()),this.isOpen=!0,this.openedAt=Date.now())},this.closeMenu=function(){this.isOpen&&(this.control.setAttribute("aria-expanded","false"),this.control.focus(),this.container.style.display="none",this.isOpen=!1,this.openedAt=0)},this}
