{"version":3,"file":"metismenujs.js","sources":["../src/constant.ts","../src/index.ts"],"sourcesContent":["import { IMMOptions } from './interface';\n\nexport type MetisMenuEvents =\n | 'show.metisMenu'\n | 'shown.metisMenu'\n | 'hide.metisMenu'\n | 'hidden.metisMenu';\n\nexport const Default: IMMOptions = {\n parentTrigger: 'li',\n subMenu: 'ul',\n toggle: true,\n triggerElement: 'a',\n};\n\nexport const ClassName = {\n ACTIVE: 'mm-active',\n COLLAPSE: 'mm-collapse',\n COLLAPSED: 'mm-collapsed',\n COLLAPSING: 'mm-collapsing',\n METIS: 'metismenu',\n SHOW: 'mm-show',\n};\n","/* eslint-disable max-len */\nimport { ClassName, Default, MetisMenuEvents } from './constant';\nimport { IMMOptions } from './interface';\nimport { EventType, SpecificEventListener, CustomEventListener } from './types';\n\nclass MetisMenu {\n protected config: IMMOptions;\n\n protected element: Element;\n\n protected isTransitioning!: boolean;\n\n protected disposed: boolean;\n\n protected triggerArr: Array;\n\n /**\n * Creates an instance of MetisMenu.\n *\n * @constructor\n * @param {Element | string} element\n * @param {IMMOptions} [options]\n * @memberof MetisMenu\n */\n\n constructor(element: Element | string, options?: IMMOptions) {\n this.element = MetisMenu.isElement(element) ? element : document.querySelector(element)!;\n this.config = { ...Default, ...options };\n this.disposed = false;\n this.triggerArr = [];\n this.init();\n }\n\n static attach(el: Element, opt?: IMMOptions) {\n return new MetisMenu(el, opt);\n }\n\n private init(): void {\n const { METIS, ACTIVE, COLLAPSE } = ClassName;\n this.element.classList.add(METIS);\n\n [].slice.call(this.element.querySelectorAll(this.config.subMenu!)).forEach((ul: Element) => {\n ul.classList.add(COLLAPSE!);\n const li = ul.closest(this.config.parentTrigger!);\n\n if (li?.classList.contains(ACTIVE!)) {\n this.show(ul as HTMLElement);\n } else {\n this.hide(ul);\n }\n\n const a = li?.querySelector(this.config.triggerElement!);\n if (a?.getAttribute('aria-disabled') === 'true') {\n return;\n }\n\n a?.setAttribute('aria-expanded', 'false');\n\n a?.addEventListener('click', this.clickEvent.bind(this));\n this.triggerArr.push(a!);\n });\n }\n\n private clickEvent(evt?: Event) {\n if (!this.disposed) {\n const target = evt?.currentTarget as Element | null;\n if (target && target.tagName === 'A') {\n evt!.preventDefault();\n }\n\n const li = target!.closest(this.config.parentTrigger!);\n const ul = li?.querySelector(this.config.subMenu!);\n this.toggle(ul!);\n }\n }\n\n update() {\n this.disposed = false;\n this.init();\n }\n\n dispose() {\n this.triggerArr.forEach((arr) => {\n arr.removeEventListener('click', this.clickEvent.bind(this));\n });\n this.disposed = true;\n }\n\n on(evtType: K, handler: SpecificEventListener, options?: AddEventListenerOptions | boolean): MetisMenu;\n\n on(evtType: E, handler: CustomEventListener, options?: AddEventListenerOptions | boolean): MetisMenu;\n\n on(evtType: MetisMenuEvents, handler: EventListener, options?: AddEventListenerOptions | boolean) {\n this.element.addEventListener(evtType, handler, options);\n return this;\n }\n\n off(evtType: K, handler: SpecificEventListener, options?: AddEventListenerOptions | boolean): MetisMenu;\n\n off(evtType: E, handler: CustomEventListener, options?: AddEventListenerOptions | boolean): MetisMenu;\n\n off(evtType: MetisMenuEvents, handler: EventListener, options?: AddEventListenerOptions | boolean): MetisMenu {\n this.element.removeEventListener(evtType, handler, options);\n return this;\n }\n\n emit(evtType: string, evtData: T, shouldBubble = false): void {\n const evt = new CustomEvent(evtType, {\n bubbles: shouldBubble,\n detail: evtData,\n });\n this.element.dispatchEvent(evt);\n }\n\n toggle(ul: Element) {\n const li = ul.closest(this.config.parentTrigger!);\n if (li?.classList.contains(ClassName.ACTIVE)) {\n this.hide(ul);\n } else {\n this.show(ul);\n }\n }\n\n show(el: Element) {\n const ul = el as HTMLElement;\n const {\n ACTIVE, COLLAPSE, COLLAPSED, COLLAPSING, SHOW,\n } = ClassName;\n\n if (this.isTransitioning || ul.classList.contains(COLLAPSING!)) {\n return;\n }\n const complete = () => {\n ul.classList.remove(COLLAPSING!);\n ul.style.height = '';\n ul.removeEventListener('transitionend', complete);\n this.setTransitioning(false);\n this.emit('shown.metisMenu', {\n shownElement: ul,\n });\n };\n\n const li = ul.closest(this.config.parentTrigger!);\n li?.classList.add(ACTIVE!);\n\n const a = li?.querySelector(this.config.triggerElement!);\n a?.setAttribute('aria-expanded', 'true');\n a?.classList.remove(COLLAPSED!);\n\n ul.style.height = '0px';\n ul.classList.remove(COLLAPSE!);\n ul.classList.remove(SHOW!);\n ul.classList.add(COLLAPSING!);\n const eleParentSiblins = [].slice\n .call(li?.parentNode?.children)\n .filter((c) => c !== li);\n if (this.config.toggle && eleParentSiblins.length > 0) {\n eleParentSiblins.forEach((sibli: Element) => {\n const sibUl = sibli.querySelector(this.config.subMenu!);\n if (sibUl) {\n this.hide(sibUl);\n }\n });\n }\n\n this.setTransitioning(true);\n\n ul.classList.add(COLLAPSE!);\n ul.classList.add(SHOW!);\n ul.style.height = `${ul.scrollHeight}px`;\n this.emit('show.metisMenu', {\n showElement: ul,\n });\n ul.addEventListener('transitionend', complete);\n }\n\n hide(el: Element) {\n const {\n ACTIVE, COLLAPSE, COLLAPSED, COLLAPSING, SHOW,\n } = ClassName;\n const ul = el as HTMLElement;\n if (this.isTransitioning || !ul.classList.contains(SHOW!)) {\n return;\n }\n this.emit('hide.metisMenu', {\n hideElement: ul,\n });\n\n const li = ul.closest(this.config.parentTrigger!);\n li?.classList.remove(ACTIVE!);\n\n const complete = () => {\n ul.classList.remove(COLLAPSING!);\n ul.classList.add(COLLAPSE!);\n ul.style.height = '';\n ul.removeEventListener('transitionend', complete);\n this.setTransitioning(false);\n this.emit('hidden.metisMenu', {\n hiddenElement: ul,\n });\n };\n\n ul.style.height = `${ul.getBoundingClientRect().height}px`;\n ul.style.height = `${ul.offsetHeight}px`;\n\n ul.classList.add(COLLAPSING!);\n ul.classList.remove(COLLAPSE!);\n ul.classList.remove(SHOW!);\n this.setTransitioning(true);\n\n ul.addEventListener('transitionend', complete);\n\n ul.style.height = '0px';\n\n const a = li?.querySelector(this.config.triggerElement!);\n a?.setAttribute('aria-expanded', 'false');\n a?.classList.add(COLLAPSED!);\n }\n\n private setTransitioning(isTransitioning: boolean) {\n this.isTransitioning = isTransitioning;\n }\n\n static isElement(element: unknown): element is Element {\n return Boolean((element as Element).classList);\n }\n}\n\nexport default MetisMenu;\n"],"names":[],"mappings":";;;;;;;;;;;;;;IAQO,MAAM,OAAO,GAAe;QACjC,aAAa,EAAE,IAAI;QACnB,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,IAAI;QACZ,cAAc,EAAE,GAAG;KACpB,CAAC;IAEK,MAAM,SAAS,GAAG;QACvB,MAAM,EAAE,WAAW;QACnB,QAAQ,EAAE,aAAa;QACvB,SAAS,EAAE,cAAc;QACzB,UAAU,EAAE,eAAe;QAC3B,KAAK,EAAE,WAAW;QAClB,IAAI,EAAE,SAAS;KAChB;;ICtBD;IAKA,MAAM,SAAS;;;;;;;;;QAoBb,YAAY,OAAyB,EAAE,OAAoB;YACzD,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,OAAO,GAAG,QAAQ,CAAC,aAAa,CAAc,OAAO,CAAE,CAAC;YACtG,IAAI,CAAC,MAAM,mCAAQ,OAAO,GAAK,OAAO,CAAE,CAAC;YACzC,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;YACtB,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;YACrB,IAAI,CAAC,IAAI,EAAE,CAAC;SACb;QAED,OAAO,MAAM,CAAC,EAAW,EAAE,GAAgB;YACzC,OAAO,IAAI,SAAS,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;SAC/B;QAEO,IAAI;YACV,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC;YAC9C,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YAElC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAc,IAAI,CAAC,MAAM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