/* * 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 B from"@typo3/core/document-service.js";import{SeverityEnum as q}from"@typo3/backend/enum/severity.js";import"bootstrap";import A from"@typo3/backend/modal.js";import G from"@typo3/core/security-utility.js";import{property as w,customElement as z}from"lit/decorators.js";import{LitElement as F,html as l,nothing as p}from"lit";import{classMap as O}from"lit/directives/class-map.js";import{styleMap as P}from"lit/directives/style-map.js";import{createRef as U,ref as L}from"lit/directives/ref.js";import{CodeMirrorElement as j}from"@typo3/backend/code-editor/element/code-mirror-element.js";import o from"~labels/core.wizards";import I from"~labels/core.common";import W from"~labels/backend.alt_doc";var f=function(d,e,t,i){var r=arguments.length,n=r<3?e:i===null?i=Object.getOwnPropertyDescriptor(e,t):i,s;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(d,e,t,i);else for(var a=d.length-1;a>=0;a--)(s=d[a])&&(n=(r<3?s(n):r>3?s(e,t,n):s(e,t))||n);return r>3&&n&&Object.defineProperty(e,t,n),n},c,m;(function(d){d.none="",d.slide="slide",d.collect="collect",d.collectReverse="collectReverse"})(m||(m={}));let u=c=class extends F{constructor(){super(...arguments),this.colCount=1,this.rowCount=1,this.readOnly=!1,this.fieldName="",this.data=[],this.codeMirrorConfig={},this.previewAreaRef=U(),this.codeMirrorRef=U(),this.defaultCell={spanned:0,rowspan:1,colspan:1,name:"",colpos:"",column:void 0,identifier:"",slideMode:m.none},this.modalButtonClickHandler=e=>{const t=e.target,i=e.currentTarget;t.name==="cancel"?i.hideModal():t.name==="ok"&&(this.setName(i.querySelector(".t3js-grideditor-field-name").value,i.userData.col,i.userData.row),this.setColumn(parseInt(i.querySelector(".t3js-grideditor-field-colpos").value,10),i.userData.col,i.userData.row),this.setIdentifier(i.querySelector(".t3js-grideditor-field-identifier").value,i.userData.col,i.userData.row),this.setSlideMode(i.querySelector(".t3js-grideditor-field-slide-mode").value,i.userData.col,i.userData.row),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord()),i.hideModal())},this.addColumnHandler=e=>{e.preventDefault(),this.addColumn(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.removeColumnHandler=e=>{e.preventDefault(),this.removeColumn(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.addRowTopHandler=e=>{e.preventDefault(),this.addRowTop(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.addRowBottomHandler=e=>{e.preventDefault(),this.addRowBottom(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.removeRowTopHandler=e=>{e.preventDefault(),this.removeRowTop(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.removeRowBottomHandler=e=>{e.preventDefault(),this.removeRowBottom(),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.linkEditorHandler=e=>{e.preventDefault();const t=e.currentTarget;this.showOptions(Number(t.dataset.col),Number(t.dataset.row))},this.linkExpandRightHandler=e=>{e.preventDefault();const t=e.currentTarget;this.addColspan(Number(t.dataset.col),Number(t.dataset.row)),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.linkShrinkLeftHandler=e=>{e.preventDefault();const t=e.currentTarget;this.removeColspan(Number(t.dataset.col),Number(t.dataset.row)),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.linkExpandDownHandler=e=>{e.preventDefault();const t=e.currentTarget;this.addRowspan(Number(t.dataset.col),Number(t.dataset.row)),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())},this.linkShrinkUpHandler=e=>{e.preventDefault();const t=e.currentTarget;this.removeRowspan(Number(t.dataset.col),Number(t.dataset.row)),this.requestUpdate(),this.writeConfig(this.export2LayoutRecord())}}static stripMarkup(e){return new G().stripHtml(e)}async connectedCallback(){await B.ready(),this.field=document.querySelector('input[name="'+this.fieldName+'"]'),this.addVisibilityObserver(this),super.connectedCallback()}firstUpdated(){this.writeConfig(this.export2LayoutRecord())}createRenderRoot(){return this}render(){return l`
${this.readOnly?p:this.renderControls("top",!1)}
${this.renderEditorGrid()}
${this.readOnly?p:this.renderControls("right",!0)} ${this.readOnly?p:this.renderControls("bottom",!1)}
${this.renderPreview()}
`}renderControls(e,t){const i={top:this.addRowTopHandler,right:this.addColumnHandler,bottom:this.addRowBottomHandler},r={top:o.get("grid_addRow"),right:o.get("grid_addColumn"),bottom:o.get("grid_addRow")},n={top:this.removeRowTopHandler,right:this.removeColumnHandler,bottom:this.removeRowBottomHandler},s={top:o.get("grid_removeRow"),right:o.get("grid_removeColumn"),bottom:o.get("grid_removeRow")};return l`
`}renderEditorGrid(){const e=[];for(let t=0;t${e}`}renderGridCell(e,t,i){const r={"--grideditor-cell-col":t+1,"--grideditor-cell-colspan":i.colspan,"--grideditor-cell-row":e+1,"--grideditor-cell-rowspan":i.rowspan};return l`
${this.readOnly?p:l` ${this.cellCanSpanRight(t,e)?l``:p} ${this.cellCanShrinkLeft(t,e)?l``:p} ${this.cellCanSpanDown(t,e)?l``:p} ${this.cellCanShrinkUp(t,e)?l``:p}`}
${o.get("grid_name")}: ${i.name?c.stripMarkup(i.name):o.get("grid_notSet")}
${o.get("grid_column")}: ${typeof i.column>"u"||isNaN(i.column)?o.get("grid_notSet"):i.column} ${i.identifier?.length?l`
${o.get("grid_identifier")}: ${i.identifier}`:""} ${(i.slideMode?.toString()||"")!==""?l`
${o.get("grid_slideMode")}: ${i.slideMode.toString()}`:""}
`}renderPreview(){return Object.keys(this.codeMirrorConfig).length===0?l`
`:l``}getNewCell(){return structuredClone(this.defaultCell)}writeConfig(e){this.field.value=e;const t=e.split(` `);let i="";for(const a of t)a&&(i+=" "+a+` `);const r=`mod.web_layout.BackendLayouts { exampleKey { title = Example icon = content-container-columns-2 config { `+i.replace(new RegExp("\\t","g")," ")+` } } } `,n=this.previewAreaRef.value;n instanceof HTMLTextAreaElement&&(n.value=r);const s=this.codeMirrorRef.value;s instanceof j&&s.setContent(r)}addRowTop(){const e=[];for(let t=0;t1&&this.removeRowspan(e,t-1),!0):!1}removeColumn(){if(this.colCount<=1)return!1;const e=[];for(let t=0;t1&&this.removeColspan(e-1,t),!0):!1}addColumn(){for(let e=0;e{const T=D!=="none"?D:"",E=m[D],y=document.createElement("option");y.value=E,y.text=T,y.selected=E===i.slideMode?.toString(),C.appendChild(y)}),H.append($,C),n.append(S,N,_,H);const M=A.show(o.get("grid_windowTitle"),n,q.notice,[{active:!0,btnClass:"btn-default",name:"cancel",text:I.get("cancel")},{btnClass:"btn-primary",name:"ok",text:I.get("ok")}]);return M.userData.col=e,M.userData.row=t,M.addEventListener("button.clicked",this.modalButtonClickHandler),!0}getCell(e,t){return e>this.colCount-1||t>this.rowCount-1?!1:this.data.length>t-1&&this.data[t].length>e-1?this.data[t][e]:null}cellCanSpanRight(e,t){if(e===this.colCount-1)return!1;const i=this.getCell(e,t);if(!i)return!1;let r;if(i.rowspan>1){for(let n=t;n1||r.rowspan>1)return!1}else if(r=this.getCell(e+i.colspan,t),!r||i.spanned===1||r.spanned===1||r.colspan>1||r.rowspan>1)return!1;return!0}cellCanSpanDown(e,t){if(t===this.rowCount-1)return!1;const i=this.getCell(e,t);if(!i)return!1;let r;if(i.colspan>1){for(let n=e;n1||r.rowspan>1)return!1}else if(r=this.getCell(e,t+i.rowspan),!r||i.spanned===1||r.spanned===1||r.colspan>1||r.rowspan>1)return!1;return!0}cellCanShrinkLeft(e,t){return this.data[t][e].colspan>1}cellCanShrinkUp(e,t){return this.data[t][e].rowspan>1}addColspan(e,t){const i=this.getCell(e,t);if(!i||!this.cellCanSpanRight(e,t))return!1;for(let r=t;r1&&(e+=" colspan = "+n.colspan+` `),n.rowspan>1&&(e+=" rowspan = "+n.rowspan+` `),typeof n.column=="number"&&(e+=" colPos = "+n.column+` `),typeof n.identifier=="string"&&n.identifier.length&&(e+=" identifier = "+n.identifier+` `),n.slideMode!==void 0&&n.slideMode!==m.none&&(e+=" slideMode = "+n.slideMode.toString()+` `),e+=` } `}}e+=` } `,e+=` } `}return e+=` } } `,e}addVisibilityObserver(e){e.offsetParent===null&&new IntersectionObserver(t=>{t.forEach(i=>{const r=this.codeMirrorRef.value;i.intersectionRatio>0&&r instanceof j&&r.requestUpdate()})}).observe(e)}};f([w({type:Number})],u.prototype,"colCount",void 0),f([w({type:Number})],u.prototype,"rowCount",void 0),f([w({type:Boolean})],u.prototype,"readOnly",void 0),f([w({type:String})],u.prototype,"fieldName",void 0),f([w({type:Array})],u.prototype,"data",void 0),f([w({type:Object})],u.prototype,"codeMirrorConfig",void 0),u=c=f([z("typo3-backend-grid-editor")],u);export{u as GridEditor};