/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
import{ModuleUtility as u,flushModuleCache as h,ModuleSelector as C}from"@typo3/backend/module.js";import d from"@typo3/backend/storage/persistent.js";import m from"@typo3/backend/viewport.js";import I from"@typo3/backend/event/client-request.js";import f from"@typo3/backend/event/trigger-request.js";import E from"@typo3/core/ajax/ajax-request.js";import c from"@typo3/core/event/regular-event.js";import{ModuleStateStorage as v}from"@typo3/backend/storage/module-state-storage.js";import{selector as b}from"@typo3/core/literals.js";import y from"@typo3/core/document-service.js";import{Collapse as S}from"bootstrap";import{KeyTypesEnum as a}from"@typo3/backend/enum/key-types.js";var l;(function(p){p.menu="[data-modulemenu]",p.item="[data-modulemenu-identifier]",p.collapsible='[data-modulemenu-collapsible="true"]'})(l||(l={}));class o{constructor(){this.loadedModule=null,y.ready().then(()=>{this.initialize()})}static getModuleMenuItemFromElement(e){return{identifier:e.dataset.modulemenuIdentifier,level:e.parentElement.dataset.modulemenuLevel?parseInt(e.parentElement.dataset.modulemenuLevel,10):null,collapsible:e.dataset.modulemenuCollapsible==="true",expanded:e.attributes.getNamedItem("aria-expanded")?.value==="true",element:e}}static getCollapsedMainMenuItems(){return d.isset("modulemenu")?JSON.parse(d.get("modulemenu")):{}}static addCollapsedMainMenuItem(e){const n=o.getCollapsedMainMenuItems();n[e]=!0,d.set("modulemenu",JSON.stringify(n))}static removeCollapseMainMenuItem(e){const n=this.getCollapsedMainMenuItems();delete n[e],d.set("modulemenu",JSON.stringify(n))}static includeId(e,n){if(!e.navigationComponentId||n.includes("id"))return n;let t="";e.navigationComponentId==="@typo3/backend/tree/page-tree-element"?t="web":t=e.name.split("_")[0];const i=v.current(t);return i.identifier&&(n="id="+encodeURIComponent(i.identifier)+"&"+n),n}static toggleModuleGroup(e,n){const t=o.getModuleMenuItemFromElement(e),i=t.element.closest(".modulemenu-group"),r=i.querySelector(".modulemenu-group-container"),s=S.getOrCreateInstance(r,{toggle:!1});if(n===void 0)n=!t.expanded;else if(n===t.expanded)return;n?(o.removeCollapseMainMenuItem(t.identifier),s.show()):(o.addCollapsedMainMenuItem(t.identifier),s.hide()),i.classList.toggle("modulemenu-group-collapsed",!n),i.classList.toggle("modulemenu-group-expanded",n),e.setAttribute("aria-expanded",n.toString())}static highlightModule(e){document.querySelector(l.menu).querySelectorAll(l.item).forEach(i=>{i.classList.remove("modulemenu-action-active"),i.removeAttribute("aria-current")});const t=u.getFromName(e);this.highlightModuleMenuItem(t,!0)}static highlightModuleMenuItem(e,n=!0){const i=document.querySelector(l.menu).querySelectorAll(l.item+b`[data-modulemenu-identifier="${e.name}"]`);i.forEach(r=>{r.classList.add("modulemenu-action-active"),n&&r.setAttribute("aria-current","location")}),i.length>0&&(n=!1),e.parent!==""&&this.highlightModuleMenuItem(u.getFromName(e.parent),n)}static getPreviousItem(e){const n=e.parentElement.previousElementSibling;return n===null?o.getLastItem(e):n.firstElementChild}static getNextItem(e){const n=e.parentElement.nextElementSibling;return n===null?o.getFirstItem(e):n.firstElementChild}static getFirstItem(e){return e.parentElement.parentElement.firstElementChild.firstElementChild}static getLastItem(e){return e.parentElement.parentElement.lastElementChild.firstElementChild}static getParentItem(e){return e.parentElement.parentElement.parentElement.firstElementChild}static getFirstChildItem(e){return e.nextElementSibling.firstElementChild.firstElementChild}refreshMenu(){return new E(TYPO3.settings.ajaxUrls.modulemenu).get().then(async e=>{const n=await e.resolve();document.getElementById("modulemenu").outerHTML=n.menu,h(),this.initializeModuleMenuEvents(),this.loadedModule&&o.highlightModule(this.loadedModule)})}getCurrentModule(){return this.loadedModule}reloadFrames(){m.ContentContainer.refresh()}showModule(e,n,t=null,i=null){n=n||"";const r=u.getFromName(e);return this.loadModuleComponents(r,i,n,new I("typo3.showModule",t))}initialize(){document.querySelector(l.menu)!==null&&(this.initializeModuleMenuEvents(),this.initializeModuleLoadListeners())}keyboardNavigation(e,n){const t=o.getModuleMenuItemFromElement(n);let i=null;switch(e.key){case a.UP:i=o.getPreviousItem(t.element);break;case a.DOWN:i=o.getNextItem(t.element);break;case a.LEFT:t.collapsible&&o.toggleModuleGroup(t.element,!1),t.level>1&&(i=o.getParentItem(t.element));break;case a.RIGHT:t.collapsible&&(o.toggleModuleGroup(t.element,!0),i=o.getFirstChildItem(t.element));break;case a.HOME:if(e.ctrlKey&&t.level>1){i=document.querySelector(l.menu+" "+l.item);break}i=o.getFirstItem(t.element);break;case a.END:e.ctrlKey&&t.level>1?i=o.getLastItem(document.querySelector(l.menu+" "+l.item)):i=o.getLastItem(t.element);break;case a.SPACE:case a.ENTER:if(e.preventDefault(),e.repeat)break;t.collapsible?(o.toggleModuleGroup(t.element,!0),i=o.getFirstChildItem(t.element)):t.element.click();break;case a.ESCAPE:t.level>1?i=o.getParentItem(t.element):t.level===1&&t.collapsible&&(i=t.element),i!==null&&o.toggleModuleGroup(i,!1);break;default:i=null}i!==null&&(e.preventDefault(),i.focus())}initializeModuleMenuEvents(){const e=document.querySelector(l.menu);new c("keydown",this.keyboardNavigation).delegateTo(e,l.item),new c("click",(n,t)=>{n.preventDefault();const i=u.getRouteFromElement(t);this.showModule(i.identifier,i.params,n)}).delegateTo(e,C.link),new c("click",(n,t)=>{n.preventDefault(),o.toggleModuleGroup(t)}).delegateTo(e,l.collapsible),new c("shown.bs.collapse",(n,t)=>{t.scrollIntoView({behavior:"smooth",block:"nearest",inline:"start"})}).delegateTo(e,".modulemenu-group")}initializeModuleLoadListeners(){const e=n=>{const t=n.detail.module;if(!t||this.loadedModule===t)return;const i=u.getFromName(t);i.link&&(o.highlightModule(t),this.loadedModule=t,i.navigationComponentId?m.NavigationContainer.showComponent(i.navigationComponentId):m.NavigationContainer.hide())};document.addEventListener("typo3-module-load",e),document.addEventListener("typo3-module-loaded",e)}loadModuleComponents(e,n,t,i){const r=e.name,s=m.ContentContainer.beforeSetUrl(i);return s.then(()=>{e.navigationComponentId?m.NavigationContainer.showComponent(e.navigationComponentId):m.NavigationContainer.hide(),o.highlightModule(r),this.loadedModule=r,t=o.includeId(e,t),this.openInContentContainer(r,n??e.link,t,new f("typo3.loadModuleComponents",i))}),s}openInContentContainer(e,n,t,i){const r=n+(t?(n.includes("?")?"&":"?")+t:"");return m.ContentContainer.setUrl(r,new f("typo3.openInContentFrame",i),e)}}let g=top?.TYPO3?.ModuleMenu;g||(g={App:new o},top.TYPO3!==void 0&&(top.TYPO3.ModuleMenu=g));var k=g;export{k as default};
