/*
 * 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/backend/sortable-table.js";import l from"@typo3/core/event/regular-event.js";import d from"@typo3/backend/modal.js";import g from"@typo3/backend/icons.js";import{MessageUtility as h}from"@typo3/backend/utility/message-utility.js";import f from"@typo3/backend/storage/persistent.js";import p from"@typo3/backend/date-time-picker.js";import{MultiRecordSelectionSelectors as y}from"@typo3/backend/multi-record-selection.js";import T from"@typo3/backend/severity.js";import v from"@typo3/core/document-service.js";import k from"@typo3/backend/form/submit-interceptor.js";import u,{ModifierKeys as C}from"@typo3/backend/hotkeys.js";class i{constructor(){v.ready().then(()=>{this.initializeSubmitInterceptor(),this.initializeEvents(),this.registerHotKeys(),this.initializeDefaultStates(),this.initializeCloseConfirm()})}static updateClearableInputs(){const e=document.querySelectorAll(".t3js-clearable");e.length>0&&import("@typo3/backend/input/clearable.js").then(function(){e.forEach(t=>t.clearable())})}static updateDateTimePickers(){document.querySelectorAll("#tx_scheduler_form .t3js-datetimepicker").forEach(e=>p.initialize(e))}static updateElementBrowserTriggers(){document.querySelectorAll(".t3js-element-browser").forEach(t=>{const a=document.getElementById(t.dataset.triggerFor);t.dataset.params=a.name+"|||pages"})}static resolveDefaultNumberOfDays(){const e=document.getElementById("task_tableGarbageCollection_numberOfDays");return e===null||typeof e.dataset.defaultNumberOfDays>"u"?null:JSON.parse(e.dataset.defaultNumberOfDays)}static storeCollapseState(e,t){let a={};f.isset("moduleData.scheduler_manage")&&(a=f.get("moduleData.scheduler_manage"));const n={};n[e]=t?1:0,a={...a,...n},f.set("moduleData.scheduler_manage",a)}toggleTaskSettingFields(e){let t=e.value;t=t.toLowerCase().replace(/\\/g,"-");for(const a of document.querySelectorAll(".extraFields")){const n=a.classList.contains("extra_fields_"+t);a.querySelectorAll("input, textarea, select").forEach(o=>{o.disabled=!n}),a.hidden=!n}}actOnChangeSchedulerTableGarbageCollectionAllTables(e){const t=document.querySelector("#task_tableGarbageCollection_numberOfDays"),a=document.querySelector("#task_tableGarbageCollection_table");if(e.checked)a.disabled=!0,t.disabled=!0;else{let n=parseInt(t.value,10);if(n<1){const o=a.value,s=i.resolveDefaultNumberOfDays();s!==null&&(n=s[o])}a.disabled=!1,n>0&&(t.disabled=!1)}}actOnChangeSchedulerTableGarbageCollectionTable(e){const t=document.querySelector("#task_tableGarbageCollection_numberOfDays"),a=i.resolveDefaultNumberOfDays();a!==null&&a[e.value]>0?(t.disabled=!1,t.value=a[e.value].toString(10)):(t.disabled=!0,t.value="0")}toggleFieldsByTaskType(e){e=parseInt(e+"",10);const t=e===2;document.querySelector("#task_end_col").hidden=!t,document.querySelector("#task_frequency_row").hidden=!t,document.querySelector("#task_multiple_row").hidden=!t}initializeSubmitInterceptor(){const e=document.querySelector("form[name=tx_scheduler_form]");e&&new k(e)}initializeEvents(){const e=document.querySelector("#task_class");e&&new l("change",s=>{this.toggleTaskSettingFields(s.target)}).bindTo(e);const t=document.querySelector("#task_type");t&&new l("change",s=>{this.toggleFieldsByTaskType(s.target.value)}).bindTo(t);const a=document.querySelector("#task_tableGarbageCollection_allTables");a&&new l("change",s=>{this.actOnChangeSchedulerTableGarbageCollectionAllTables(s.target)}).bindTo(a);const n=document.querySelector("#task_tableGarbageCollection_table");n&&new l("change",s=>{this.actOnChangeSchedulerTableGarbageCollectionTable(s.target)}).bindTo(n);const o=document.querySelector("[data-update-task-frequency]");o&&new l("change",s=>{const r=s.target,c=document.querySelector("#task_frequency");c.value=r.value,r.value="",r.blur()}).bindTo(o),document.querySelectorAll("[data-scheduler-table]").forEach(s=>{new b(s)}),new l("click",(s,r)=>{s.preventDefault();const c=new URL(r.href,window.origin);c.searchParams.set("mode",r.dataset.mode),c.searchParams.set("bparams",r.dataset.params),d.advanced({type:d.types.iframe,content:c.toString(),size:d.sizes.large})}).delegateTo(document,".t3js-element-browser"),new l("show.bs.collapse",this.toggleCollapseIcon.bind(this)).bindTo(document),new l("hide.bs.collapse",this.toggleCollapseIcon.bind(this)).bindTo(document),new l("multiRecordSelection:action:go",this.executeTasks.bind(this)).bindTo(document),new l("multiRecordSelection:action:go_cron",this.executeTasks.bind(this)).bindTo(document),window.addEventListener("message",this.listenOnElementBrowser.bind(this)),new l("click",(s,r)=>{s.preventDefault(),this.saveDocument(r)}).delegateTo(document,"button[form]")}saveDocument(e){const t=this.getTaskEditForm();if(!t)return;const a=document.createElement("input");a.type="hidden",a.value="save",a.name="CMD",t.append(a),t.requestSubmit(e)}saveAndCloseDocument(e){const t=this.getTaskEditForm();if(!t)return;const a=document.createElement("input");a.type="hidden",a.value="saveclose",a.name="CMD",t.append(a),t.requestSubmit(e)}registerHotKeys(){const e=this.getTaskEditForm();if(e===null)return;let t=null;e.CMD instanceof HTMLElement?t=e.CMD:e.CMD instanceof NodeList&&(t=e.CMD.item(0)),u.setScope("scheduler/edit-task"),u.register([u.normalizedCtrlModifierKey,"s"],a=>{a.preventDefault(),this.saveDocument(t)},{scope:"scheduler/edit-task",allowOnEditables:!0,bindElement:t}),u.register([u.normalizedCtrlModifierKey,C.SHIFT,"s"],a=>{a.preventDefault(),this.saveAndCloseDocument(t)},{scope:"scheduler/edit-task",allowOnEditables:!0})}initializeDefaultStates(){const e=document.querySelector("#task_type");e!==null&&this.toggleFieldsByTaskType(e.value);const t=document.querySelector("#task_class");t!==null&&(this.toggleTaskSettingFields(t),i.updateClearableInputs(),i.updateDateTimePickers(),i.updateElementBrowserTriggers())}listenOnElementBrowser(e){if(!h.verifyOrigin(e.origin))throw"Denied message sent by "+e.origin;if(e.data.actionName==="typo3:elementBrowser:elementAdded"){if(typeof e.data.fieldName>"u")throw"fieldName not defined in message";if(typeof e.data.value>"u")throw"value not defined in message";const t=document.querySelector('input[name="'+e.data.fieldName+'"]');t.value=e.data.value.split("_").pop()}}toggleCollapseIcon(e){const t=e.type==="hide.bs.collapse",a=document.querySelector('.t3js-toggle-table[data-bs-target="#'+e.target.id+'"] .t3js-icon');a!==null&&g.getIcon(t?"actions-view-list-expand":"actions-view-list-collapse",g.sizes.small).then(n=>{a.replaceWith(document.createRange().createContextualFragment(n))}),i.storeCollapseState(e.target.dataset.table,t)}executeTasks(e){const t=document.querySelector('[data-multi-record-selection-form="'+e.detail.identifier+'"]');if(t===null)return;const a=[];if(e.detail.checkboxes.forEach(n=>{const o=n.closest(y.elementSelector);o!==null&&o.dataset.taskId&&a.push(o.dataset.taskId)}),a.length){if(e.type==="multiRecordSelection:action:go_cron"){const n=document.createElement("input");n.setAttribute("type","hidden"),n.setAttribute("name","scheduleCron"),n.setAttribute("value",a.join(",")),t.append(n)}else{const n=document.createElement("input");n.setAttribute("type","hidden"),n.setAttribute("name","execute"),n.setAttribute("value",a.join(",")),t.append(n)}t.submit()}}initializeCloseConfirm(){const e=this.getTaskEditForm();if(!e)return;const t=new FormData(e);new l("click",a=>{const n=new FormData(e),o=Object.fromEntries(t.entries()),s=Object.fromEntries(n.entries());if(JSON.stringify(o)!==JSON.stringify(s)||e.querySelector('input[value="add"]')){a.preventDefault();const c=a.currentTarget.href;d.confirm(TYPO3.lang["label.confirm.close_without_save.title"]||"Do you want to close without saving?",TYPO3.lang["label.confirm.close_without_save.content"]||"You currently have unsaved changes. Are you sure you want to discard these changes?",T.warning,[{text:TYPO3.lang["buttons.confirm.close_without_save.no"]||"No, I will continue editing",btnClass:"btn-default",name:"no",trigger:()=>d.dismiss()},{text:TYPO3.lang["buttons.confirm.close_without_save.yes"]||"Yes, discard my changes",btnClass:"btn-default",name:"yes",trigger:()=>{d.dismiss(),window.location.href=c}},{text:TYPO3.lang["buttons.confirm.save_and_close"]||"Save and close",btnClass:"btn-primary",name:"save",active:!0,trigger:()=>{d.dismiss();const m=document.createElement("input");m.type="hidden",m.value="saveclose",m.name="CMD",e.append(m),e.submit()}}])}}).bindTo(document.querySelector(".t3js-scheduler-close"))}getTaskEditForm(){return document.querySelector("form[name=tx_scheduler_form]")}}var S=new i;export{S as default};
