sigd/public/assets/libs/metismenujs/metismenujs.esm.min.js

10 lines
4.0 KiB
JavaScript
Executable File

/*!
* metismenujs - v1.4.0
* MetisMenu: Collapsible menu plugin with Vanilla-JS
* https://github.com/onokumus/metismenujs#readme
*
* Made by Osman Nuri Okumus <onokumus@gmail.com> (https://github.com/onokumus)
* Under MIT License
*/
const Default={parentTrigger:"li",subMenu:"ul",toggle:!0,triggerElement:"a"},ClassName={ACTIVE:"mm-active",COLLAPSE:"mm-collapse",COLLAPSED:"mm-collapsed",COLLAPSING:"mm-collapsing",METIS:"metismenu",SHOW:"mm-show"};class MetisMenu{constructor(t,e){this.element=MetisMenu.isElement(t)?t:document.querySelector(t),this.config=Object.assign(Object.assign({},Default),e),this.disposed=!1,this.triggerArr=[],this.boundEventHandler=this.clickEvent.bind(this),this.init()}static attach(t,e){return new MetisMenu(t,e)}init(){const{METIS:t,ACTIVE:e,COLLAPSE:s}=ClassName;this.element.classList.add(t);const i=[...this.element.querySelectorAll(this.config.subMenu)];0!==i.length&&i.forEach((t=>{t.classList.add(s);const i=t.closest(this.config.parentTrigger);(null==i?void 0:i.classList.contains(e))?this.show(t):this.hide(t);const n=null==i?void 0:i.querySelector(this.config.triggerElement);"true"!==(null==n?void 0:n.getAttribute("aria-disabled"))&&(null==n||n.setAttribute("aria-expanded","false"),null==n||n.addEventListener("click",this.boundEventHandler),this.triggerArr.push(n))}))}clickEvent(t){if(!this.disposed){const e=null==t?void 0:t.currentTarget;e&&"A"===e.tagName&&t.preventDefault();const s=e.closest(this.config.parentTrigger),i=null==s?void 0:s.querySelector(this.config.subMenu);this.toggle(i)}}update(){this.disposed=!1,this.init()}dispose(){this.triggerArr.forEach((t=>{t.removeEventListener("click",this.boundEventHandler)})),this.disposed=!0}on(t,e,s){return this.element.addEventListener(t,e,s),this}off(t,e,s){return this.element.removeEventListener(t,e,s),this}emit(t,e,s=!1){const i=new CustomEvent(t,{bubbles:s,detail:e});this.element.dispatchEvent(i)}toggle(t){const e=t.closest(this.config.parentTrigger);(null==e?void 0:e.classList.contains(ClassName.ACTIVE))?this.hide(t):this.show(t)}show(t){var e;const s=t,{ACTIVE:i,COLLAPSE:n,COLLAPSED:l,COLLAPSING:r,SHOW:o}=ClassName;if(this.isTransitioning||s.classList.contains(r))return;const a=()=>{s.classList.remove(r),s.style.height="",s.removeEventListener("transitionend",a),this.setTransitioning(!1),this.emit("shown.metisMenu",{shownElement:s})},c=s.closest(this.config.parentTrigger);null==c||c.classList.add(i);const h=null==c?void 0:c.querySelector(this.config.triggerElement);null==h||h.setAttribute("aria-expanded","true"),null==h||h.classList.remove(l),s.style.height="0px",s.classList.remove(n),s.classList.remove(o),s.classList.add(r);const d=[].slice.call(null===(e=null==c?void 0:c.parentNode)||void 0===e?void 0:e.children).filter((t=>t!==c));this.config.toggle&&d.length>0&&d.forEach((t=>{const e=t.querySelector(this.config.subMenu);e&&this.hide(e)})),this.setTransitioning(!0),s.classList.add(n),s.classList.add(o),s.style.height=`${s.scrollHeight}px`,this.emit("show.metisMenu",{showElement:s}),s.addEventListener("transitionend",a)}hide(t){const{ACTIVE:e,COLLAPSE:s,COLLAPSED:i,COLLAPSING:n,SHOW:l}=ClassName,r=t;if(this.isTransitioning||!r.classList.contains(l))return;this.emit("hide.metisMenu",{hideElement:r});const o=r.closest(this.config.parentTrigger);null==o||o.classList.remove(e);const a=()=>{r.classList.remove(n),r.classList.add(s),r.style.height="",r.removeEventListener("transitionend",a),this.setTransitioning(!1),this.emit("hidden.metisMenu",{hiddenElement:r})};r.style.height=`${r.getBoundingClientRect().height}px`,r.style.height=`${r.offsetHeight}px`,r.classList.add(n),r.classList.remove(s),r.classList.remove(l),this.setTransitioning(!0),r.addEventListener("transitionend",a),r.style.height="0px";const c=null==o?void 0:o.querySelector(this.config.triggerElement);null==c||c.setAttribute("aria-expanded","false"),null==c||c.classList.add(i)}setTransitioning(t){this.isTransitioning=t}static isElement(t){return Boolean(t.classList)}}export{MetisMenu,MetisMenu as default};
//# sourceMappingURL=metismenujs.esm.min.js.map