/* * 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{AjaxResponse as I}from"@typo3/core/ajax/ajax-response.js";import M from"@typo3/core/ajax/ajax-request.js";import N from"@typo3/backend/context-menu-actions.js";import"@typo3/backend/element/spinner-element.js";import{state as m,queryAll as b,customElement as k}from"lit/decorators.js";import{LitElement as C,nothing as x,html as f}from"lit";import{delay as S}from"@typo3/core/lit-helper.js";import{styleMap as v}from"lit/directives/style-map.js";import{unsafeHTML as y}from"lit/directives/unsafe-html.js";import{Task as T,initialState as E}from"@lit/task";import w from"@typo3/backend/notification.js";var d=function(h,t,e,n){var i=arguments.length,o=i<3?t:n===null?n=Object.getOwnPropertyDescriptor(t,e):n,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(h,t,e,n);else for(var r=h.length-1;r>=0;r--)(s=h[r])&&(o=(i<3?s(o):i>3?s(t,e,o):s(t,e))||o);return i>3&&o&&Object.defineProperty(t,e,o),o},p;(function(h){h.open="typo3:contextmenu:open",h.close="typo3:contextmenu:close"})(p||(p={}));class F{constructor(){document.addEventListener("click",t=>{this.handleTriggerEvent(t)}),document.addEventListener("contextmenu",t=>{this.handleTriggerEvent(t)})}show(t,e,n,i,o,s=null,r=null){const a=new CustomEvent(p.open,{detail:{table:t,uid:e,context:n,eventSource:s,originalEvent:r},bubbles:!0,composed:!0});top.document.dispatchEvent(a)}handleTriggerEvent(t){if(!(t.target instanceof Element))return;const e=t.target.closest("[data-contextmenu-trigger]");if(e instanceof HTMLElement){this.handleContextMenuEvent(t,e);return}}handleContextMenuEvent(t,e){const n=e.dataset.contextmenuTrigger;if(n==="click"||n===t.type){if(t.preventDefault(),(e.dataset.contextmenuTable??"")==="")throw console.error("Referenced element misses data-contextmenu-table",e),new Error("No data-contextmenu-table attribute provided in contextmenu trigger markup");if((e.dataset.contextmenuUid??"")==="")throw console.error("Referenced element misses data-contextmenu-uid",e),new Error("No data-contextmenu-uid attribute provided in contextmenu trigger markup");this.show(e.dataset.contextmenuTable??"",e.dataset.contextmenuUid??"",e.dataset.contextmenuContext??"","","",e,t)}}}var A=new F;let l=class extends C{constructor(){super(...arguments),this.open=!1,this.table="",this.uid="",this.context="",this.rootPositionY=0,this.rootPositionX=0,this.eventSource=null,this.rootIdentifier="root",this.focusFirstElement=!1,this.fetchTask=new T(this,{autoRun:!1,args:()=>[this.table,this.uid,this.context,this.open],task:async([t,e,n,i],{signal:o})=>{if(!i)return E;const s=new URLSearchParams;if(t!==""&&s.set("table",t),e!==""&&s.set("uid",e.toString()),(n??"")!==""&&s.set("context",n),s.size===0)return E;const r=TYPO3.settings.ajaxUrls.contextmenu,a=await new M(r).withQueryArguments(s).get({signal:o}),c=Object.values(await a.resolve());return c.length===0?E:this.enhanceNodes("root",c)},onComplete:()=>{this.focusFirstElement=!0},onError:t=>{t instanceof I?w.error("",t.response.status+" "+t.response.statusText,5):w.error("",t.message)}}),this.show=t=>{const e=t.detail;if(this.open=!0,this.table=e.table,this.uid=e.uid,this.context=e.context,this.eventSource=e.eventSource,e.originalEvent!==null){const n=this.calculateIframeOffset(e.originalEvent.view,window);let i,o;if(e.originalEvent.pointerId===-1&&e.originalEvent.target){const s=e.originalEvent.target.getBoundingClientRect();o=s.top+s.height/2,i=s.left}else o=e.originalEvent.clientY,i=e.originalEvent.clientX;this.rootPositionY=o+n.y,this.rootPositionX=i+n.x}this.fetchTask.run()},this.hide=async()=>{this.open&&(this.open=!1,this.fetchTask.run(),await this.updateComplete,this.eventSource?.focus())}}get nodes(){return this.fetchTask.value??[]}connectedCallback(){super.connectedCallback(),window.addEventListener("resize",this.hide),document.addEventListener(p.open,this.show),document.addEventListener(p.close,this.hide)}disconnectedCallback(){super.disconnectedCallback(),window.removeEventListener("resize",this.hide),document.removeEventListener(p.open,this.show),document.removeEventListener(p.close,this.hide)}updated(){this.focusFirstElement&&(this.contextMenuItemElements.length>0&&Array.from(this.contextMenuItemElements).at(0).focus(),this.focusFirstElement=!1),this.updateContextMenuPositions()}createRenderRoot(){return this}render(){return this.fetchTask.render({initial:()=>x,pending:()=>[this.renderOverlay(),S(80,()=>this.renderMenu(null,this.rootIdentifier,this.rootPositionY,this.rootPositionX))],complete:t=>[this.renderOverlay(),this.renderMenu(t,this.rootIdentifier,this.rootPositionY,this.rootPositionX),this.flattenMenuItems(t).filter(e=>e.type==="submenu").map(e=>this.isNodeExpanded(e)?this.renderMenu(e.childItems,this.getNodeIdentifier(e),this.rootPositionY,this.rootPositionX):x)]})}renderOverlay(){return f`
`}renderMenu(t,e,n,i){const o={top:n+"px",insetInlineStart:i+"px"};return t===null?f``:t.length===0?x:f``}renderMenuItem(t){return t.type==="divider"?f`