/*
 * 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 m from"@typo3/backend/icons.js";import b from"@typo3/backend/storage/persistent.js";import u from"@typo3/core/event/regular-event.js";import w from"@typo3/core/document-service.js";import{MultiRecordSelectionSelectors as S}from"@typo3/backend/multi-record-selection.js";import{selector as g}from"@typo3/core/literals.js";import E from"@typo3/backend/notification.js";import x from"@typo3/core/ajax/ajax-request.js";import"@typo3/core/ajax/ajax-response.js";import{sudoModeInterceptor as C}from"@typo3/backend/security/sudo-mode-interceptor.js";class p{constructor(){this.identifier={entity:".t3js-entity",toggle:".t3js-toggle-recordlist",localize:".t3js-action-localize",hide:'button[data-datahandler-action="visibility"]',delete:".t3js-record-delete",editMultiple:".t3js-record-edit-multiple",icons:{collapse:"actions-view-list-collapse",expand:"actions-view-list-expand"}},this.toggleClick=(i,e)=>{i.preventDefault();const t=e.dataset.table,n=document.querySelector(e.dataset.bsTarget),o=n.dataset.state==="expanded",c=e.querySelector(".t3js-icon"),d=o?this.identifier.icons.expand:this.identifier.icons.collapse;m.getIcon(d,m.sizes.small).then(l=>{c.replaceWith(document.createRange().createContextualFragment(l))});let a={};b.isset("moduleData.records.collapsedTables")&&(a=b.get("moduleData.records.collapsedTables"));const s={};s[t]=o?1:0,a=Object.assign(a,s),b.set("moduleData.records.collapsedTables",a).then(()=>{n.dataset.state=o?"collapsed":"expanded"})},this.onEditMultiple=(i,e)=>{i.preventDefault();let t="",n="",o=[];const c=[];if(i.type==="multiRecordSelection:action:edit"){const a=i.detail,s=a.configuration;if(n=s.returnUrl||"",o=s.columnsOnly||[],t=s.tableName||"",t==="")return;a.checkboxes.forEach(l=>{const r=l.closest(S.elementSelector);r!==null&&r.dataset[s.idField]&&c.push(r.dataset[s.idField])})}else{const a=e.closest("[data-table]");if(a===null||(t=a.dataset.table||"",t===""))return;n=e.dataset.returnUrl||"",o=JSON.parse(e.dataset.columnsOnly||"{}");const s=a.querySelectorAll(this.identifier.entity+'[data-uid][data-table="'+t+'"] td.col-checkbox input[type="checkbox"]:checked');if(s.length)s.forEach(l=>{c.push(l.closest(this.identifier.entity+g`[data-uid][data-table="${t}"]`).dataset.uid)});else{const l=a.querySelectorAll(this.identifier.entity+g`[data-uid][data-table="${t}"]`);if(!l.length)return;l.forEach(r=>{c.push(r.dataset.uid)})}}if(!c.length)return;let d=top.TYPO3.settings.FormEngine.moduleUrl+"&edit["+t+"]["+c.join(",")+"]=edit&module="+encodeURIComponent(top.TYPO3.ModuleMenu.App.getCurrentModule())+"&returnUrl="+p.getReturnUrl(n);o.length>0&&(d+=o.map((a,s)=>"&columnsOnly["+t+"]["+s+"]="+a).join("")),window.location.href=d},this.disableButton=(i,e)=>{e.setAttribute("disabled","disabled"),e.classList.add("disabled")},this.toggleVisibility=(i,e)=>{e.disabled=!0;const t=e.querySelector(".t3js-icon"),n=t.cloneNode(!0);m.getIcon("spinner-circle",m.sizes.small).then(l=>{t.replaceWith(document.createRange().createContextualFragment(l))});const o=e.closest("tr[data-uid]"),c=o.dataset.table,d=parseInt(o.dataset.uid,10),s=e.dataset.datahandlerStatus==="visible"?"hide":"show";new x(TYPO3.settings.ajaxUrls.record_toggle_visibility).addMiddleware(C).post({table:c,uid:d,action:s}).then(async l=>{const r=await l.resolve();e.setAttribute("data-datahandler-status",r.isVisible?"visible":"hidden");const h=r.isVisible?e.dataset.datahandlerVisibleLabel:e.dataset.datahandlerHiddenLabel;e.setAttribute("title",h);const y=r.isVisible?"actions-edit-hide":"actions-edit-unhide";m.getIcon(y,m.sizes.small).then(v=>{e.querySelector(".t3js-icon").replaceWith(document.createRange().createContextualFragment(v))}),o.querySelector(".col-icon .t3js-icon").replaceWith(document.createRange().createContextualFragment(r.icon));const f=new u("animationend",()=>{o.classList.remove("record-pulse"),f.release()});f.bindTo(o),o.classList.add("record-pulse"),c==="pages"&&top.document.dispatchEvent(new CustomEvent("typo3:pagetree:refresh"))}).catch(async l=>{e.querySelector(".t3js-icon").replaceWith(n);const r=await l.resolve();for(const h of r.messages)E.error(h.title,h.message)}).finally(()=>{e.disabled=!1})},this.registerPaginationEvents=()=>{document.querySelectorAll(".t3js-recordlist-paging").forEach(i=>{i.addEventListener("keyup",e=>{e.preventDefault();let t=Number(i.value);const n=Number(i.min),o=Number(i.max);if(n&&t<n&&(t=n),o&&t>o&&(t=o),i.value=t.toString(10),e.key==="Enter"&&t!==Number(i.dataset.currentpage)){const c=i.closest('form[name^="list-table-form-"]'),d=new URL(c.action,window.origin);d.searchParams.set("pointer",t.toString()),window.location.href=d.toString()}})})},new u("click",this.toggleClick).delegateTo(document,this.identifier.toggle),new u("click",this.onEditMultiple).delegateTo(document,this.identifier.editMultiple),new u("click",this.disableButton).delegateTo(document,this.identifier.localize),new u("click",this.toggleVisibility).delegateTo(document,this.identifier.hide),w.ready().then(()=>{this.registerPaginationEvents()}),new u("multiRecordSelection:action:edit",this.onEditMultiple).bindTo(document),new u("multiRecordSelection:action:copyMarked",i=>{p.submitClipboardFormWithCommand("copyMarked",i.target)}).bindTo(document),new u("multiRecordSelection:action:removeMarked",i=>{p.submitClipboardFormWithCommand("removeMarked",i.target)}).bindTo(document)}static submitClipboardFormWithCommand(i,e){const t=e.closest("form");if(!t)return;const n=t.querySelector('input[name="cmd"]');n&&(n.value=i,t.submit())}static getReturnUrl(i){return i===""&&(i=top.list_frame.document.location.pathname+top.list_frame.document.location.search),encodeURIComponent(i)}}var I=new p;export{I as default};
