/*
 * 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/notification.js";import m from"@typo3/core/document-service.js";import h from"@typo3/core/event/regular-event.js";import{selector as u}from"@typo3/core/literals.js";var a;(function(s){s.actionsSelector=".t3js-multi-record-selection-actions",s.checkboxSelector=".t3js-multi-record-selection-check",s.checkboxActionsSelector=".t3js-multi-record-selection-check-actions",s.checkboxActionsToggleSelector=".t3js-multi-record-selection-check-actions-toggle",s.elementSelector="[data-multi-record-selection-element]"})(a||(a={}));var b;(function(s){s.actionButton="button[data-multi-record-selection-action]",s.checkboxActionButton="button[data-multi-record-selection-check-action]"})(b||(b={}));var d;(function(s){s.checkAll="check-all",s.checkNone="check-none",s.toggle="toggle"})(d||(d={}));var r;(function(s){s.any="",s.checked=":checked",s.unchecked=":not(:checked)"})(r||(r={}));class c{static{this.activeClass="active"}constructor(){this.lastChecked=null,m.ready().then(()=>{c.restoreTemporaryState(),this.registerActions(),this.registerActionsEventHandlers(),this.registerCheckboxActions(),this.registerCheckboxKeyboardActions(),this.registerCheckboxTableRowSelectionAction(),this.registerToggleCheckboxActions(),this.registerDispatchCheckboxStateChangedEvent(),this.registerCheckboxStateChangedEventHandler()})}static getCheckboxes(e=r.any,t=""){return document.querySelectorAll(c.getCombinedSelector(a.checkboxSelector+e,t))}static getCombinedSelector(e,t){return t!==""?[u`[data-multi-record-selection-identifier="${t}"]`,e].join(" "):e}static getIdentifier(e){return e.closest("[data-multi-record-selection-identifier]")?.dataset.multiRecordSelectionIdentifier||""}static changeCheckboxState(e,t){e.disabled||e.checked===t||e.dataset.manuallyChanged||(e.checked=t,e.dispatchEvent(new Event("change",{bubbles:!0})))}static restoreTemporaryState(){const e=c.getCheckboxes(r.checked);if(!e.length)return;let t=!1;const o=[];e.forEach(n=>{n.closest(a.elementSelector)?.classList.add(c.activeClass);const i=c.getIdentifier(n);i!==""&&!o.includes(i)&&(o.push(i),t=!0,c.toggleActionsState(i))}),t||c.toggleActionsState()}static toggleActionsState(e=""){const t=document.querySelectorAll(c.getCombinedSelector(a.actionsSelector,e));if(!t.length)return;if(!c.getCheckboxes(r.checked,e).length){t.forEach(n=>c.changeActionContainerVisibility(n,!1));return}t.forEach(n=>c.changeActionContainerVisibility(n));const o=document.querySelectorAll([c.getCombinedSelector(a.actionsSelector,e),b.actionButton].join(" "));o.length&&o.forEach(n=>{if(!n.dataset.multiRecordSelectionActionConfig)return;const i=JSON.parse(n.dataset.multiRecordSelectionActionConfig);if(!i.idField)return;n.disabled=!0;const l=c.getCheckboxes(r.checked,e);for(let g=0;g<l.length;g++)if(l[g].closest(a.elementSelector)?.dataset[i.idField]){n.disabled=!1;break}})}static changeActionContainerVisibility(e,t=!0){const o=e.closest(".multi-record-selection-panel")?.children;if(t){if(o)for(let n=0;n<o.length;n++)o[n].classList.add("hidden");e.classList.remove("hidden")}else{if(o)for(let n=0;n<o.length;n++)o[n].classList.remove("hidden");e.classList.add("hidden")}}static unsetManuallyChangedAttribute(e){c.getCheckboxes(r.any,e).forEach(t=>{t.removeAttribute("data-manually-changed")})}registerActions(){new h("click",(e,t)=>{t.dataset.multiRecordSelectionAction;const o=c.getIdentifier(t),n=JSON.parse(t.dataset.multiRecordSelectionActionConfig||"{}"),i=c.getCheckboxes(r.checked,o);i.length&&t.dispatchEvent(new CustomEvent("multiRecordSelection:action:"+t.dataset.multiRecordSelectionAction,{detail:{identifier:o,checkboxes:i,configuration:n},bubbles:!0,cancelable:!1}))}).delegateTo(document,[a.actionsSelector,b.actionButton].join(" "))}registerActionsEventHandlers(){new h("multiRecordSelection:actions:show",e=>{const t=e.detail?.identifier||"";document.querySelectorAll(c.getCombinedSelector(a.actionsSelector,t)).forEach(n=>c.changeActionContainerVisibility(n))}).bindTo(document),new h("multiRecordSelection:actions:hide",e=>{const t=e.detail?.identifier||"";document.querySelectorAll(c.getCombinedSelector(a.actionsSelector,t)).forEach(n=>c.changeActionContainerVisibility(n,!1))}).bindTo(document),new h("multiRecordSelection:checkboxes:check",e=>{const t=e.detail?.identifier||"";c.getCheckboxes(r.any,t).forEach(o=>c.changeCheckboxState(o,!0))}).bindTo(document),new h("multiRecordSelection:checkboxes:uncheck",e=>{const t=e.detail?.identifier||"";c.getCheckboxes(r.any,t).forEach(o=>c.changeCheckboxState(o,!1))}).bindTo(document)}registerCheckboxActions(){new h("click",(e,t)=>{if(e.preventDefault(),!t.dataset.multiRecordSelectionCheckAction)return;const o=c.getIdentifier(t),n=c.getCheckboxes(r.any,o);if(n.length){switch(c.unsetManuallyChangedAttribute(o),t.dataset.multiRecordSelectionCheckAction){case d.checkAll:n.forEach(i=>{c.changeCheckboxState(i,!0)});break;case d.checkNone:n.forEach(i=>{c.changeCheckboxState(i,!1)});break;case d.toggle:n.forEach(i=>{c.changeCheckboxState(i,!i.checked)});break;default:f.warning("Unknown checkbox action")}c.unsetManuallyChangedAttribute(o)}}).delegateTo(document,[a.checkboxActionsSelector,b.checkboxActionButton].join(" "))}registerCheckboxKeyboardActions(){new h("click",(e,t)=>this.handleCheckboxKeyboardActions(e,t)).delegateTo(document,a.checkboxSelector)}registerCheckboxTableRowSelectionAction(){new h("click",(e,t)=>{const o=e.target.tagName;if(o!=="TH"&&o!=="TD")return;const n=t.querySelector(a.checkboxSelector);n!==null&&(c.changeCheckboxState(n,!n.checked),this.handleCheckboxKeyboardActions(e,n,!1))}).delegateTo(document,a.elementSelector),new h("mousedown",e=>(e.shiftKey||e.altKey||e.ctrlKey)&&e.preventDefault()).delegateTo(document,a.elementSelector)}registerDispatchCheckboxStateChangedEvent(){new h("change",(e,t)=>{t.dispatchEvent(new CustomEvent("multiRecordSelection:checkbox:state:changed",{detail:{identifier:c.getIdentifier(t)},bubbles:!0,cancelable:!1}))}).delegateTo(document,a.checkboxSelector)}registerCheckboxStateChangedEventHandler(){new h("multiRecordSelection:checkbox:state:changed",e=>{const t=e.target,o=e.detail?.identifier||"";t.checked?t.closest(a.elementSelector).classList.add(c.activeClass):t.closest(a.elementSelector).classList.remove(c.activeClass),c.toggleActionsState(o)}).bindTo(document)}registerToggleCheckboxActions(){new h("click",(e,t)=>{const o=c.getIdentifier(t),n=document.querySelector([c.getCombinedSelector(a.checkboxActionsSelector,o),'button[data-multi-record-selection-check-action="'+d.checkAll+'"]'].join(" "));n!==null&&(n.disabled=!c.getCheckboxes(r.unchecked,o).length);const i=document.querySelector([c.getCombinedSelector(a.checkboxActionsSelector,o),'button[data-multi-record-selection-check-action="'+d.checkNone+'"]'].join(" "));i!==null&&(i.disabled=!c.getCheckboxes(r.checked,o).length);const l=document.querySelector([c.getCombinedSelector(a.checkboxActionsSelector,o),'button[data-multi-record-selection-check-action="'+d.toggle+'"]'].join(" "));l!==null&&(l.disabled=!c.getCheckboxes(r.any,o).length)}).delegateTo(document,a.checkboxActionsToggleSelector)}handleCheckboxKeyboardActions(e,t,o=!0){const n=c.getIdentifier(t);if(!this.lastChecked||!document.body.contains(this.lastChecked)||c.getIdentifier(this.lastChecked)!==n||!e.shiftKey&&!e.altKey&&!e.ctrlKey){this.lastChecked=t;return}if(o&&c.unsetManuallyChangedAttribute(n),e.shiftKey){const i=Array.from(c.getCheckboxes(r.any,n)),l=i.indexOf(t),g=i.indexOf(this.lastChecked);i.slice(Math.min(l,g),Math.max(l,g)+1).forEach(k=>{k!==t&&c.changeCheckboxState(k,t.checked)})}this.lastChecked=t,(e.altKey||e.ctrlKey)&&c.getCheckboxes(r.any,n).forEach(i=>{i!==t&&c.changeCheckboxState(i,!i.checked)}),c.unsetManuallyChangedAttribute(n)}}var C=new c;export{a as MultiRecordSelectionSelectors,C as default};
