/*
 * 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 f from"@typo3/backend/icons.js";import p from"@typo3/backend/storage/persistent.js";import d from"@typo3/core/event/regular-event.js";import b from"@typo3/core/document-service.js";import{MultiRecordSelectionSelectors as g}from"@typo3/backend/multi-record-selection.js";import{selector as h}from"@typo3/core/literals.js";class m{constructor(){this.identifier={entity:".t3js-entity",toggle:".t3js-toggle-recordlist",localize:".t3js-action-localize",editMultiple:".t3js-record-edit-multiple",icons:{collapse:"actions-view-list-collapse",expand:"actions-view-list-expand"}},this.toggleClick=(e,i)=>{e.preventDefault();const t=i.dataset.table,n=document.querySelector(i.dataset.bsTarget),a=n.dataset.state==="expanded",s=i.querySelector(".t3js-icon"),r=a?this.identifier.icons.expand:this.identifier.icons.collapse;f.getIcon(r,f.sizes.small).then(c=>{s.replaceWith(document.createRange().createContextualFragment(c))});let o={};p.isset("moduleData.web_list.collapsedTables")&&(o=p.get("moduleData.web_list.collapsedTables"));const l={};l[t]=a?1:0,o=Object.assign(o,l),p.set("moduleData.web_list.collapsedTables",o).then(()=>{n.dataset.state=a?"collapsed":"expanded"})},this.onEditMultiple=(e,i)=>{e.preventDefault();let t="",n="",a=[];const s=[];if(e.type==="multiRecordSelection:action:edit"){const o=e.detail,l=o.configuration;if(n=l.returnUrl||"",a=l.columnsOnly||[],t=l.tableName||"",t==="")return;o.checkboxes.forEach(c=>{const u=c.closest(g.elementSelector);u!==null&&u.dataset[l.idField]&&s.push(u.dataset[l.idField])})}else{const o=i.closest("[data-table]");if(o===null||(t=o.dataset.table||"",t===""))return;n=i.dataset.returnUrl||"",a=JSON.parse(i.dataset.columnsOnly||"{}");const l=o.querySelectorAll(this.identifier.entity+'[data-uid][data-table="'+t+'"] td.col-checkbox input[type="checkbox"]:checked');if(l.length)l.forEach(c=>{s.push(c.closest(this.identifier.entity+h`[data-uid][data-table="${t}"]`).dataset.uid)});else{const c=o.querySelectorAll(this.identifier.entity+h`[data-uid][data-table="${t}"]`);if(!c.length)return;c.forEach(u=>{s.push(u.dataset.uid)})}}if(!s.length)return;let r=top.TYPO3.settings.FormEngine.moduleUrl+"&edit["+t+"]["+s.join(",")+"]=edit&returnUrl="+m.getReturnUrl(n);a.length>0&&(r+=a.map((o,l)=>"&columnsOnly["+t+"]["+l+"]="+o).join("")),window.location.href=r},this.disableButton=(e,i)=>{i.setAttribute("disabled","disabled"),i.classList.add("disabled")},this.registerPaginationEvents=()=>{document.querySelectorAll(".t3js-recordlist-paging").forEach(e=>{e.addEventListener("keyup",i=>{i.preventDefault();let t=Number(e.value);const n=Number(e.min),a=Number(e.max);if(n&&t<n&&(t=n),a&&t>a&&(t=a),e.value=t.toString(10),i.key==="Enter"&&t!==Number(e.dataset.currentpage)){const s=e.closest('form[name^="list-table-form-"]'),r=new URL(s.action,window.origin);r.searchParams.set("pointer",t.toString()),window.location.href=r.toString()}})})},new d("click",this.toggleClick).delegateTo(document,this.identifier.toggle),new d("click",this.onEditMultiple).delegateTo(document,this.identifier.editMultiple),new d("click",this.disableButton).delegateTo(document,this.identifier.localize),b.ready().then(()=>{this.registerPaginationEvents()}),new d("multiRecordSelection:action:edit",this.onEditMultiple).bindTo(document),new d("multiRecordSelection:action:copyMarked",e=>{m.submitClipboardFormWithCommand("copyMarked",e.target)}).bindTo(document),new d("multiRecordSelection:action:removeMarked",e=>{m.submitClipboardFormWithCommand("removeMarked",e.target)}).bindTo(document)}static submitClipboardFormWithCommand(e,i){const t=i.closest("form");if(!t)return;const n=t.querySelector('input[name="cmd"]');n&&(n.value=e,t.submit())}static getReturnUrl(e){return e===""&&(e=top.list_frame.document.location.pathname+top.list_frame.document.location.search),encodeURIComponent(e)}}var y=new m;export{y as default};
