/*
 * 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 m from"@typo3/core/ajax/ajax-request.js";import n from"@typo3/backend/module-menu.js";import p from"@typo3/backend/viewport.js";import d from"@typo3/core/event/regular-event.js";import{ModuleStateStorage as k}from"@typo3/backend/storage/module-state-storage.js";var r;(function(a){a.scaffoldSelector=".t3js-scaffold",a.containerSelector="#typo3-cms-workspaces-backend-toolbaritems-workspaceselectortoolbaritem",a.activeMenuItemLinkSelector=".t3js-workspaces-switchlink.active",a.menuItemLinkSelector=".t3js-workspaces-switchlink",a.toolbarItemSelector=".dropdown-toggle"})(r||(r={}));var c;(function(a){a.workspaceBodyClass="scaffold-in-workspace",a.workspacesTitleInToolbarClass="toolbar-item-name"})(c||(c={}));class s{constructor(){p.Topbar.Toolbar.registerEvent(()=>{this.initializeEvents(),s.updateBackendContext()}),new d("typo3:datahandler:process",e=>{const o=e.detail.payload;o.table==="sys_workspace"&&o.action==="delete"&&o.hasErrors===!1&&p.Topbar.refresh()}).bindTo(document)}static refreshPageTree(){document.dispatchEvent(new CustomEvent("typo3:pagetree:refresh"))}static getWorkspaceState(){const e=document.querySelector([r.containerSelector,r.activeMenuItemLinkSelector].join(" "));if(e===null)return null;const o=parseInt(e.dataset.workspaceid||"0",10);return{id:o,title:e.innerText.trim(),inWorkspace:o!==0}}static updateTopBar(e){const o=document.querySelector(r.containerSelector);if(o.querySelector(r.containerSelector+" ."+c.workspacesTitleInToolbarClass)?.remove(),e.inWorkspace&&e.title){const t=document.createElement("span");t.classList.add(c.workspacesTitleInToolbarClass),t.textContent=e.title,o.querySelector(r.toolbarItemSelector).append(t)}}static updateBackendContext(e=null){if(e??=s.getWorkspaceState(),e===null)return;document.querySelector(r.scaffoldSelector).classList.toggle(c.workspaceBodyClass,e.inWorkspace),e.inWorkspace&&!e.title&&(e.title=TYPO3.lang["Workspaces.workspaceTitle"]),s.updateTopBar(e)}performWorkspaceSwitch(e,o){const t=document.querySelector(r.containerSelector);t.querySelector(r.activeMenuItemLinkSelector).classList.remove("active"),t.querySelector(r.menuItemLinkSelector+'[data-workspaceid="'+e+'"]')?.classList.add("active"),s.updateBackendContext({id:e,title:o,inWorkspace:e!==0})}initializeEvents(){const e=document.querySelector(r.containerSelector);new d("click",(o,t)=>{o.preventDefault(),this.switchWorkspace(parseInt(t.dataset.workspaceid,10))}).delegateTo(e,r.menuItemLinkSelector)}switchWorkspace(e){new m(TYPO3.settings.ajaxUrls.workspace_switch).post({workspaceId:e,pageId:k.current("web").identifier}).then(async o=>{const t=await o.resolve();t.workspaceId||(t.workspaceId=0),this.performWorkspaceSwitch(t.workspaceId,t.title||"");const l=n.App.getCurrentModule();if(t.pageId){let i=TYPO3.Backend.ContentContainer.getUrl();i+=(i.includes("?")?"&":"?")+"id="+t.pageId,p.ContentContainer.setUrl(i)}else l==="workspaces_admin"?n.App.showModule(l,"workspace="+e):l?.startsWith("web_")?n.App.reloadFrames():t.pageModule&&n.App.showModule(t.pageModule);s.refreshPageTree(),n.App.refreshMenu()})}}const u=new s;TYPO3.WorkspacesMenu=u;export{u as default};
