/*
 * 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{SeverityEnum as h}from"@typo3/backend/enum/severity.js";import p from"@typo3/core/document-service.js";import u from"@typo3/backend/modal.js";import v from"@typo3/backend/utility.js";import S from"@typo3/workspaces/workspaces.js";import l from"@typo3/core/event/throttle-event.js";import"@typo3/workspaces/renderable/send-to-stage-form.js";import a from"@typo3/core/event/regular-event.js";var e;(function(s){s.topbar=".t3js-workspace-topbar",s.stageSliderContainer=".t3js-stage-slider-container",s.stageSlider=".t3js-stage-slider",s.liveView=".t3js-workspace-view-live",s.workspaceView=".t3js-workspace-view-workspace",s.sendToStageAction='[data-action="send-to-stage"]',s.discardAction='[data-action="discard"]',s.stageButtonsContainer=".t3js-stage-buttons",s.previewModeContainer=".t3js-preview-mode",s.activePreviewMode=".t3js-active-preview-mode",s.workspacePreview=".t3js-workspace-preview"})(e||(e={}));class P extends S{constructor(){super(),this.ajaxRoute="workspace_preview",this.currentSlidePosition=100,this.elements={},p.ready().then(()=>{this.getElements(),this.resizeViews(),this.registerEvents(),this.initStageButtons()})}getElements(){this.elements.liveView=document.querySelector(e.liveView),this.elements.stageSliderContainer=document.querySelector(e.stageSliderContainer),this.elements.stageSlider=document.querySelector(e.stageSlider),this.elements.workspaceView=document.querySelector(e.workspaceView),this.elements.stageButtonsContainer=document.querySelector(e.stageButtonsContainer),this.elements.previewModeContainer=document.querySelector(e.previewModeContainer),this.elements.activePreviewMode=document.querySelector(e.activePreviewMode),this.elements.workspacePreview=document.querySelector(e.workspacePreview)}registerEvents(){new l("resize",()=>{this.resizeViews()},50).bindTo(window),new a("click",this.renderDiscardWindow.bind(this)).delegateTo(document,e.discardAction),new a("click",this.renderSendPageToStageWindow.bind(this)).delegateTo(document,e.sendToStageAction),new a("click",()=>{window.top.document.querySelectorAll(".t3js-workspace-recipient:not([disabled])").forEach(t=>{t.checked=!0})}).delegateTo(document,".t3js-workspace-recipients-selectall"),new a("click",()=>{window.top.document.querySelectorAll(".t3js-workspace-recipient:not([disabled])").forEach(t=>{t.checked=!1})}).delegateTo(document,".t3js-workspace-recipients-deselectall"),new l("input",this.updateSlidePosition.bind(this),10).bindTo(document.querySelector(e.stageSlider)),new a("click",this.changePreviewMode.bind(this)).delegateTo(this.elements.previewModeContainer,"[data-preview-mode]")}initStageButtons(){this.sendRemoteRequest([this.generateRemoteActionsPayload("updateStageChangeButtons",[TYPO3.settings.Workspaces.id])],e.topbar).then(async t=>{this.renderStageButtons((await t.resolve())[0].result)})}renderStageButtons(t){this.elements.stageButtonsContainer.innerHTML=t}updateSlidePosition(t){this.currentSlidePosition=parseInt(t.target.value,10),this.resizeViews()}resizeViews(){this.elements.activePreviewMode.dataset.activePreviewMode==="slider"&&(this.elements.liveView.style.height=100-this.currentSlidePosition+"%")}renderDiscardWindow(){const t=u.confirm(TYPO3.lang["window.discardAll.title"],TYPO3.lang["window.discardAll.message"],h.warning,[{text:TYPO3.lang.cancel,active:!0,btnClass:"btn-default",name:"cancel",trigger:()=>{t.hideModal()}},{text:TYPO3.lang.ok,btnClass:"btn-warning",name:"ok"}]);t.addEventListener("button.clicked",i=>{i.target.name==="ok"&&this.sendRemoteRequest([this.generateRemoteActionsPayload("discardStagesFromPage",[TYPO3.settings.Workspaces.id]),this.generateRemoteActionsPayload("updateStageChangeButtons",[TYPO3.settings.Workspaces.id])],e.topbar).then(async o=>{t.hideModal(),this.renderStageButtons((await o.resolve())[1].result),this.elements.workspaceView.setAttribute("src",this.elements.workspaceView.getAttribute("src"))})})}renderSendPageToStageWindow(t,i){const o=i.dataset.direction;let n;if(o==="prev")n="sendPageToPreviousStage";else if(o==="next")n="sendPageToNextStage";else throw"Invalid direction "+o+" requested.";this.sendRemoteRequest(this.generateRemoteActionsPayload(n,[TYPO3.settings.Workspaces.id]),e.topbar).then(async w=>{const c=await w.resolve(),r=this.renderSendToStageWindow(c);r.addEventListener("button.clicked",m=>{if(m.target.name==="ok"){const d=v.convertFormToObject(r.querySelector("form"));d.affects=c[0].result.affects,d.stageId=parseInt(i.dataset.stageId,10),this.sendRemoteRequest([this.generateRemoteActionsPayload("sentCollectionToStage",[d]),this.generateRemoteActionsPayload("updateStageChangeButtons",[TYPO3.settings.Workspaces.id])],e.topbar).then(async g=>{r.hideModal(),this.renderStageButtons((await g.resolve())[1].result)})}})})}changePreviewMode(t,i){t.preventDefault();const o=this.elements.activePreviewMode.dataset.activePreviewMode,n=i.dataset.previewMode;this.elements.activePreviewMode.textContent=i.textContent,this.elements.activePreviewMode.dataset.activePreviewMode=n,this.elements.workspacePreview.classList.remove("typo3-workspace-preview-"+o),this.elements.workspacePreview.classList.add("typo3-workspace-preview-"+n),n==="slider"?(this.elements.stageSliderContainer.style.display="",this.resizeViews()):(this.elements.stageSliderContainer.style.display="none",this.elements.liveView.style.height="")}}var k=new P;export{k as default};
