/*
 * 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{ScaffoldIdentifierEnum as E}from"@typo3/backend/enum/viewport/scaffold-identifier.js";import{ModuleSelector as g,ModuleUtility as u,flushModuleCache as b}from"@typo3/backend/module.js";import f from"@typo3/backend/storage/persistent.js";import s from"@typo3/backend/viewport.js";import v from"@typo3/backend/event/client-request.js";import C from"@typo3/backend/event/trigger-request.js";import y from"@typo3/core/ajax/ajax-request.js";import m from"@typo3/core/event/regular-event.js";import{ModuleStateStorage as S}from"@typo3/backend/storage/module-state-storage.js";import{selector as I}from"@typo3/core/literals.js";import k from"@typo3/core/document-service.js";import{Collapse as w}from"bootstrap";import{KeyTypesEnum as a}from"@typo3/backend/enum/key-types.js";var r;(function(h){h.menu="[data-modulemenu]",h.item="[data-modulemenu-identifier]",h.collapsible='[data-modulemenu-collapsible="true"]'})(r||(r={}));class l{constructor(){this.loadedModule=null,k.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 f.isset("modulemenu")?JSON.parse(f.get("modulemenu")):{}}static addCollapsedMainMenuItem(e){const o=l.getCollapsedMainMenuItems();o[e]=!0,f.set("modulemenu",JSON.stringify(o))}static removeCollapseMainMenuItem(e){const o=this.getCollapsedMainMenuItems();delete o[e],f.set("modulemenu",JSON.stringify(o))}static includeId(e,o){if(!e.navigationComponentId)return o;let t="";e.navigationComponentId==="@typo3/backend/tree/page-tree-element"?t="web":t=e.name.split("_")[0];const n=S.current(t);return n.identifier&&(o="id="+encodeURIComponent(n.identifier)+"&"+o),o}static toggleMenu(e){const o=document.querySelector(E.scaffold),t="scaffold-modulemenu-expanded";typeof e>"u"&&(e=o.classList.contains(t)),o.classList.toggle(t,!e),e||o.classList.remove("scaffold-toolbar-expanded"),f.set("BackendComponents.States.typo3-module-menu",{collapsed:e})}static toggleModuleGroup(e,o){const t=l.getModuleMenuItemFromElement(e),n=t.element.closest(".modulemenu-group"),i=n.querySelector(".modulemenu-group-container"),d=w.getOrCreateInstance(i,{toggle:!1});if(o===void 0)o=!t.expanded;else if(o===t.expanded)return;o?(l.removeCollapseMainMenuItem(t.identifier),d.show()):(l.addCollapsedMainMenuItem(t.identifier),d.hide()),n.classList.toggle("modulemenu-group-collapsed",!o),n.classList.toggle("modulemenu-group-expanded",o),e.setAttribute("aria-expanded",o.toString())}static highlightModule(e){document.querySelector(r.menu).querySelectorAll(r.item).forEach(i=>{i.classList.remove("modulemenu-action-active"),i.removeAttribute("aria-current")}),document.querySelector(".t3js-scaffold-toolbar").querySelectorAll(g.link+".dropdown-item").forEach(i=>{i.classList.remove("active"),i.removeAttribute("aria-current")});const n=u.getFromName(e);this.highlightModuleMenuItem(n,!0)}static highlightModuleMenuItem(e,o=!0){const n=document.querySelector(r.menu).querySelectorAll(r.item+I`[data-modulemenu-identifier="${e.name}"]`);n.forEach(c=>{c.classList.add("modulemenu-action-active"),o&&c.setAttribute("aria-current","location")});const d=document.querySelector(".t3js-scaffold-toolbar").querySelectorAll(g.link+I`[data-moduleroute-identifier="${e.name}"].dropdown-item`);d.forEach(c=>{c.classList.add("active"),o&&c.setAttribute("aria-current","location")}),(n.length>0||d.length>0)&&(o=!1),e.parent!==""&&this.highlightModuleMenuItem(u.getFromName(e.parent),o)}static getPreviousItem(e){const o=e.parentElement.previousElementSibling;return o===null?l.getLastItem(e):o.firstElementChild}static getNextItem(e){const o=e.parentElement.nextElementSibling;return o===null?l.getFirstItem(e):o.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 y(TYPO3.settings.ajaxUrls.modulemenu).get().then(async e=>{const o=await e.resolve();document.getElementById("modulemenu").outerHTML=o.menu,b(),this.initializeModuleMenuEvents(),this.loadedModule&&l.highlightModule(this.loadedModule)})}getCurrentModule(){return this.loadedModule}reloadFrames(){s.ContentContainer.refresh()}showModule(e,o,t=null){o=o||"";const n=u.getFromName(e);return this.loadModuleComponents(n,o,new v("typo3.showModule",t))}initialize(){document.querySelector(r.menu)!==null&&(this.initializeModuleMenuEvents(),s.Topbar.Toolbar.registerEvent(()=>{document.querySelector(".t3js-scaffold-toolbar")&&this.initializeTopBarEvents()}))}keyboardNavigation(e,o){const t=l.getModuleMenuItemFromElement(o);let n=null;switch(e.key){case a.UP:n=l.getPreviousItem(t.element);break;case a.DOWN:n=l.getNextItem(t.element);break;case a.LEFT:t.collapsible&&l.toggleModuleGroup(t.element,!1),t.level>1&&(n=l.getParentItem(t.element));break;case a.RIGHT:t.collapsible&&(l.toggleModuleGroup(t.element,!0),n=l.getFirstChildItem(t.element));break;case a.HOME:if(e.ctrlKey&&t.level>1){n=document.querySelector(r.menu+" "+r.item);break}n=l.getFirstItem(t.element);break;case a.END:e.ctrlKey&&t.level>1?n=l.getLastItem(document.querySelector(r.menu+" "+r.item)):n=l.getLastItem(t.element);break;case a.SPACE:case a.ENTER:if(e.preventDefault(),e.repeat)break;t.collapsible?(l.toggleModuleGroup(t.element,!0),n=l.getFirstChildItem(t.element)):t.element.click();break;case a.ESCAPE:t.level>1?n=l.getParentItem(t.element):t.level===1&&t.collapsible&&(n=t.element),n!==null&&l.toggleModuleGroup(n,!1);break;default:n=null}n!==null&&(e.preventDefault(),n.focus())}initializeModuleMenuEvents(){const e=document.querySelector(r.menu);new m("keydown",this.keyboardNavigation).delegateTo(e,r.item),new m("click",(o,t)=>{o.preventDefault();const n=u.getRouteFromElement(t);this.showModule(n.identifier,n.params,o)}).delegateTo(e,g.link),new m("click",(o,t)=>{o.preventDefault(),l.toggleModuleGroup(t)}).delegateTo(e,r.collapsible),new m("shown.bs.collapse",(o,t)=>{t.scrollIntoView({behavior:"smooth",block:"nearest",inline:"start"})}).delegateTo(e,".modulemenu-group")}initializeTopBarEvents(){const e=document.querySelector(".t3js-scaffold-toolbar");new m("click",(t,n)=>{t.preventDefault();const i=u.getRouteFromElement(n);this.showModule(i.identifier,i.params,t)}).delegateTo(e,g.link),new m("click",t=>{t.preventDefault(),l.toggleMenu()}).bindTo(document.querySelector(".t3js-topbar-button-modulemenu")),new m("click",t=>{t.preventDefault(),l.toggleMenu(!0)}).bindTo(document.querySelector(".t3js-scaffold-content-overlay"));const o=t=>{const n=t.detail.module;if(!n||this.loadedModule===n)return;const i=u.getFromName(n);i.link&&(l.highlightModule(n),this.loadedModule=n,i.navigationComponentId?s.NavigationContainer.showComponent(i.navigationComponentId):s.NavigationContainer.hide())};document.addEventListener("typo3-module-load",o),document.addEventListener("typo3-module-loaded",o)}loadModuleComponents(e,o,t){const n=e.name,i=s.ContentContainer.beforeSetUrl(t);return i.then(()=>{e.navigationComponentId?s.NavigationContainer.showComponent(e.navigationComponentId):s.NavigationContainer.hide(),l.highlightModule(n),this.loadedModule=n,o=l.includeId(e,o),this.openInContentContainer(n,e.link,o,new C("typo3.loadModuleComponents",t))}),i}openInContentContainer(e,o,t,n){const i=o+(t?(o.includes("?")?"&":"?")+t:"");return s.ContentContainer.setUrl(i,new C("typo3.openInContentFrame",n),e)}}let p=top?.TYPO3?.ModuleMenu;p||(p={App:new l},top.TYPO3!==void 0&&(top.TYPO3.ModuleMenu=p));var L=p;export{L as default};
