/* * 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 u from"@typo3/core/ajax/ajax-request.js";import p from"@typo3/core/document-service.js";import h from"@typo3/core/event/debounce-event.js";import c from"@typo3/core/event/regular-event.js";var a;(function(n){n.toggleButton=".t3js-form-field-slug-toggle",n.recreateButton=".t3js-form-field-slug-recreate",n.inputField=".t3js-form-field-slug-input",n.readOnlyField=".t3js-form-field-slug-readonly",n.hiddenField=".t3js-form-field-slug-hidden"})(a||(a={}));var o;(function(n){n.AUTO="auto",n.RECREATE="recreate",n.MANUAL="manual"})(o||(o={}));class f{constructor(t,i){this.options=null,this.fullElement=null,this.manuallyChanged=!1,this.readOnlyField=null,this.inputField=null,this.hiddenField=null,this.request=null,this.fieldsToListenOn={},this.options=i,this.fieldsToListenOn=this.options.listenerFieldNames||{},p.ready().then(l=>{this.fullElement=l.querySelector(t),this.inputField=this.fullElement.querySelector(a.inputField),this.readOnlyField=this.fullElement.querySelector(a.readOnlyField),this.hiddenField=this.fullElement.querySelector(a.hiddenField),this.registerEvents()})}registerEvents(){const t=Object.values(this.getAvailableFieldsForProposalGeneration()).map(e=>e.dataset.slugSelector),i=this.fullElement.querySelector(a.recreateButton);t.length>0&&this.options.command==="new"&&new h("input",()=>{this.manuallyChanged||this.sendSlugProposal(o.AUTO)}).delegateTo(document,t.join(",")),t.length>0||this.hasPostModifiersDefined()?new c("click",e=>{e.preventDefault(),this.readOnlyField.classList.contains("hidden")&&(this.readOnlyField.classList.toggle("hidden",!1),this.inputField.classList.toggle("hidden",!0)),this.sendSlugProposal(o.RECREATE)}).bindTo(i):(i.classList.add("disabled"),i.disabled=!0),new h("input",()=>{this.manuallyChanged=!0,this.sendSlugProposal(o.MANUAL)}).bindTo(this.inputField);const l=this.fullElement.querySelector(a.toggleButton);new c("click",e=>{e.preventDefault();const s=this.readOnlyField.classList.contains("hidden");if(this.readOnlyField.classList.toggle("hidden",!s),this.inputField.classList.toggle("hidden",s),!s){this.hiddenField.value=this.inputField.value;return}this.inputField.value!==this.readOnlyField.value?this.readOnlyField.value=this.inputField.value:(this.manuallyChanged=!1,this.fullElement.querySelector(".t3js-form-proposal-accepted").classList.add("hidden"),this.fullElement.querySelector(".t3js-form-proposal-different").classList.add("hidden")),this.hiddenField.value=this.readOnlyField.value}).bindTo(l)}sendSlugProposal(t){const i={};t===o.AUTO||t===o.RECREATE?(Object.entries(this.getAvailableFieldsForProposalGeneration()).forEach(l=>{i[l[0]]=l[1].value}),this.options.includeUidInValues===!0&&(i.uid=this.options.recordId.toString())):i.manual=this.inputField.value,this.request instanceof u&&this.request.abort(),this.request=new u(TYPO3.settings.ajaxUrls.record_slug_suggest),this.request.post({values:i,mode:t,tableName:this.options.tableName,pageId:this.options.pageId,parentPageId:this.options.parentPageId,recordId:this.options.recordId,language:this.options.language,fieldName:this.options.fieldName,command:this.options.command,signature:this.options.signature}).then(async l=>{const e=await l.resolve(),s="/"+e.proposal.replace(/^\//,""),d=this.fullElement.querySelector(".t3js-form-proposal-accepted"),r=this.fullElement.querySelector(".t3js-form-proposal-different");d.classList.toggle("hidden",e.hasConflicts),r.classList.toggle("hidden",!e.hasConflicts),(e.hasConflicts?r:d).querySelector("span").innerText=s,this.hiddenField.value!==e.proposal&&this.fullElement.querySelector("input[data-formengine-input-name]").dispatchEvent(new Event("change",{bubbles:!0,cancelable:!0})),t===o.AUTO||t===o.RECREATE?(this.readOnlyField.value=e.proposal,this.hiddenField.value=e.proposal,this.inputField.value=e.proposal):this.hiddenField.value=e.proposal}).finally(()=>{this.request=null})}getAvailableFieldsForProposalGeneration(){const t={};for(const[i,l]of Object.entries(this.fieldsToListenOn)){const e=['[data-formengine-input-name="'+l+'"]','[name="'+l+'"]'];let s,d="";e.some(r=>(s=document.querySelector(r),d=r,s!==null)),s!==null&&(s.dataset.slugSelector=d,t[i]=s)}return t}hasPostModifiersDefined(){return Array.isArray(this.options.config.generatorOptions.postModifiers)&&this.options.config.generatorOptions.postModifiers.length>0}}export{f as default};