/*
 * 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 o from"interactjs";import h from"@typo3/core/document-service.js";import l from"@typo3/backend/storage/persistent.js";import n from"@typo3/core/event/regular-event.js";import d from"@typo3/core/event/debounce-event.js";var i;(function(r){r.resizableContainerIdentifier=".t3js-viewpage-resizeable",r.moduleDocheaderSelector=".t3js-module-docheader",r.moduleBodySelector=".t3js-module-body",r.customSelector=".t3js-preset-custom",r.customWidthSelector=".t3js-preset-custom-width",r.customHeightSelector=".t3js-preset-custom-height",r.changeOrientationSelector=".t3js-change-orientation",r.changePresetSelector=".t3js-change-preset",r.inputWidthSelector=".t3js-viewpage-input-width",r.inputHeightSelector=".t3js-viewpage-input-height",r.currentLabelSelector=".t3js-viewpage-current-label",r.topbarContainerSelector=".t3js-viewpage-topbar",r.refreshSelector=".t3js-viewpage-refresh"})(i||(i={}));class c{constructor(){this.defaultLabel="",this.minimalHeight=300,this.minimalWidth=300,this.storagePrefix="moduleData.page_preview.States.",h.ready().then(()=>{const e=document.querySelector(".t3js-preset-custom-label");this.defaultLabel=e?.textContent.trim()??"",this.iframe=document.getElementById("tx_this_iframe"),this.inputCustomWidth=document.querySelector(i.inputWidthSelector),this.inputCustomHeight=document.querySelector(i.inputHeightSelector),this.customPresetItem=document.querySelector(i.customSelector),this.customPresetItemWidth=document.querySelector(i.customWidthSelector),this.customPresetItemHeight=document.querySelector(i.customHeightSelector),this.currentLabelElement=document.querySelector(i.currentLabelSelector),this.resizableContainer=document.querySelector(i.resizableContainerIdentifier),this.initialize()})}getCurrentWidth(){return this.inputCustomWidth.valueAsNumber}getCurrentHeight(){return this.inputCustomHeight.valueAsNumber}setLabel(e){this.currentLabelElement.textContent=e}getCurrentLabel(){return this.currentLabelElement.textContent}persistChanges(e,t){l.set(e,t)}setSize(e,t){isNaN(t)&&(t=this.calculateContainerMaxHeight()),t<this.minimalHeight&&(t=this.minimalHeight),t=Math.round(t),isNaN(e)&&(e=this.calculateContainerMaxWidth()),e<this.minimalWidth&&(e=this.minimalWidth),e=Math.round(e),this.inputCustomWidth.valueAsNumber=e,this.inputCustomHeight.valueAsNumber=t,this.resizableContainer.style.width=`${e}px`,this.resizableContainer.style.height=`${t}px`,this.resizableContainer.style.left="0"}persistCurrentPreset(){const e={width:this.getCurrentWidth(),height:this.getCurrentHeight(),label:this.getCurrentLabel()};this.persistChanges(this.storagePrefix+"current",e)}persistCustomPreset(){const e={width:this.getCurrentWidth(),height:this.getCurrentHeight()};this.customPresetItem.dataset.width=e.width.toString(10),this.customPresetItem.dataset.height=e.height.toString(10),this.customPresetItemWidth.textContent=e.width.toString(10),this.customPresetItemHeight.textContent=e.height.toString(10),this.persistChanges(this.storagePrefix+"current",e),this.persistChanges(this.storagePrefix+"custom",e)}persistCustomPresetAfterChange(){clearTimeout(this.queueDelayTimer),this.queueDelayTimer=window.setTimeout(()=>{this.persistCustomPreset()},1e3)}initialize(){new n("click",()=>{this.setSize(this.getCurrentHeight(),this.getCurrentWidth()),this.persistCurrentPreset()}).bindTo(document.querySelector(i.changeOrientationSelector)),[this.inputCustomWidth,this.inputCustomHeight].forEach(e=>{new n("input",t=>{const s=t.target;s.valueAsNumber=Math.round(parseInt(s.value,10))}).bindTo(e),new d("change",()=>{this.setSize(this.getCurrentWidth(),this.getCurrentHeight()),this.setLabel(this.defaultLabel),this.persistCustomPresetAfterChange()},50).bindTo(e)}),new n("click",(e,t)=>{this.setSize(parseInt(t.dataset.width,10),parseInt(t.dataset.height,10)),this.setLabel(t.dataset.label),this.persistCurrentPreset()}).delegateTo(document,i.changePresetSelector),new n("click",()=>{this.iframe.contentWindow.location.reload()}).bindTo(document.querySelector(i.refreshSelector)),o(this.resizableContainer).on("resizestart",e=>{const t=document.createElement("div");t.id="viewpage-iframe-cover",t.setAttribute("style","z-index:99;position:absolute;width:100%;top:0;left:0;height:100%;"),e.target.appendChild(t)}).on("resizeend",()=>{document.getElementById("viewpage-iframe-cover").remove(),this.persistCustomPreset()}).resizable({origin:"self",edges:{top:!1,left:!0,bottom:!0,right:!0},listeners:{move:e=>{const t=Math.round(e.rect.width),s=Math.round(e.rect.height);Object.assign(e.target.style,{width:`${t}px`,height:`${s}px`}),this.inputCustomWidth.valueAsNumber=t,this.inputCustomHeight.valueAsNumber=s,this.setLabel(this.defaultLabel)}},modifiers:[o.modifiers.restrictSize({min:{width:this.minimalWidth,height:this.minimalHeight}})]})}calculateContainerMaxHeight(){this.resizableContainer.hidden=!0;const e=document.querySelector(i.moduleDocheaderSelector).getBoundingClientRect().height,t=getComputedStyle(document.querySelector(i.moduleBodySelector)),s=parseFloat(t.getPropertyValue("padding-top"))+parseFloat(t.getPropertyValue("padding-bottom")),a=document.body.getBoundingClientRect().height,u=document.querySelector(i.topbarContainerSelector).getBoundingClientRect().height;return this.resizableContainer.hidden=!1,a-e-s-u-8}calculateContainerMaxWidth(){this.resizableContainer.hidden=!0;const e=getComputedStyle(document.querySelector(i.moduleBodySelector)),t=parseFloat(e.getPropertyValue("padding-left"))+parseFloat(e.getPropertyValue("padding-right")),s=document.body.getBoundingClientRect().width;return this.resizableContainer.hidden=!1,s-t}}var m=new c;export{m as default};
