/*
 * 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 s from"@typo3/backend/form-engine.js";import a from"@typo3/backend/modal.js";import r from"@typo3/backend/severity.js";import l from"@typo3/core/document-service.js";import"@typo3/backend/element/icon-element.js";import i from"~labels/backend.alt_doc";class c{constructor(t={}){this.closeButton=null,this.fullscreenButton=null,this.options=t,l.ready().then(()=>{this.closeButton=document.querySelector(".t3js-contextual-close"),this.fullscreenButton=document.querySelector(".t3js-contextual-fullscreen"),this.initialize()})}initialize(){this.initializeCloseButton(),this.initializeFullscreenButton(),this.initializeEscapeKey(),this.initializeParentMessages(),this.handlePostSaveState(),this.options.justSaved||this.focusFirstElement()}initializeCloseButton(){this.closeButton&&this.closeButton.addEventListener("click",()=>{this.requestClose()})}initializeFullscreenButton(){this.fullscreenButton&&this.fullscreenButton.addEventListener("click",t=>{t.preventDefault();const e=()=>{document.querySelectorAll(".has-change").forEach(o=>o.classList.remove("has-change")),top?.TYPO3?.Backend?.ContentContainer?.setUrl(this.fullscreenButton.href),window.parent.postMessage({actionName:"typo3:editform:navigate"},window.location.origin)};s.hasChange()?a.confirm(i.get("label.confirm.close_without_save.title"),i.get("label.confirm.close_without_save.content"),r.warning,[{text:i.get("buttons.confirm.close_without_save.no"),btnClass:"btn-default",trigger:(o,n)=>{n.hideModal()}},{text:i.get("buttons.confirm.close_without_save.yes"),btnClass:"btn-warning",trigger:(o,n)=>{n.hideModal(),e()}}]):e()})}initializeEscapeKey(){document.addEventListener("keydown",t=>{t.key==="Escape"&&(t.preventDefault(),this.requestClose())})}initializeParentMessages(){window.addEventListener("message",t=>{t.origin===window.location.origin&&t.data?.actionName==="typo3:editform:requestclose"&&this.requestClose()})}handlePostSaveState(){this.options.justSaved&&(window.parent.postMessage({actionName:"typo3:editform:saved",recordTitle:this.options.savedRecordTitle??""},window.location.origin),this.showSavedIndicator()),this.options.closed&&this.notifyParentClose()}requestClose(){s.hasChange()?a.confirm(i.get("label.confirm.close_without_save.title"),i.get("label.confirm.close_without_save.content"),r.warning,[{text:i.get("buttons.confirm.close_without_save.no"),btnClass:"btn-default",trigger:(t,e)=>{e.hideModal()}},{text:i.get("buttons.confirm.close_without_save.yes"),btnClass:"btn-default",trigger:(t,e)=>{e.hideModal(),this.notifyParentClose()}},{text:i.get("buttons.confirm.save_and_close"),btnClass:"btn-primary",active:!0,trigger:(t,e)=>{e.hideModal(),s.saveAndCloseDocument()}}]):this.notifyParentClose()}showSavedIndicator(){const t=document.querySelector(".contextual-record-edit-actions");if(!t)return;const e=document.createElement("span");e.className="contextual-record-edit-saved-indicator",e.innerHTML='<typo3-backend-icon identifier="actions-check" size="small"></typo3-backend-icon> '+i.get("notification.record_saved.title.singular"),t.prepend(e),setTimeout(()=>{e.style.opacity="0",e.addEventListener("transitionend",()=>e.remove())},2e3)}focusFirstElement(){(this.fullscreenButton??this.closeButton)?.focus()}notifyParentClose(){window.parent.postMessage({actionName:"typo3:editform:closed"},window.location.origin)}}export{c as default};
