/*
 * 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 r from"@typo3/core/event/regular-event.js";import c from"@typo3/core/ajax/ajax-request.js";class i{constructor(){this.options={containerSelector:"#typo3-permissionList",editControllerSelector:"#PermissionControllerEdit"},this.ajaxUrl=TYPO3.settings.ajaxUrls.user_access_permissions,this.initializeCheckboxGroups(),this.initializeEvents()}static setPermissionCheckboxes(e,o){const t=document.querySelectorAll(`input[type="checkbox"][name^="${e}"]`);for(const a of t){const s=parseInt(a.value,10);a.checked=(o&s)===s}}static updatePermissionValue(e,o){let t=0;const a=document.querySelectorAll(`input[type="checkbox"][name^="${e}"]:checked`);for(const s of a)t|=parseInt(s.value,10);document.forms.namedItem("editform")[o].value=t|(e==="check[perms_user]"?1:0)}setPermissions(e){const o=e.dataset.page,t=e.dataset.who,a=e.dataset.bits;new c(this.ajaxUrl).post({page:o,who:t,permissions:e.dataset.permissions,mode:e.dataset.mode,bits:e.dataset.bits}).then(async s=>{const n=await s.resolve(),p=e.parentElement;p.innerHTML=n,p.querySelector('button[data-bits="'+a+'"]').focus()})}toggleEditLock(e){const o=e.dataset.page;new c(this.ajaxUrl).post({action:"toggle_edit_lock",page:o,editLockState:e.dataset.lockstate}).then(async t=>{const a=await t.resolve(),s=e.parentElement;e.outerHTML=a,s.querySelector('button[data-page="'+o+'"]').focus()})}changeOwner(e){const o=e.dataset.page,t=document.getElementById("o_"+o);new c(this.ajaxUrl).post({action:"change_owner",page:o,ownerUid:e.dataset.owner,newOwnerUid:t.getElementsByTagName("select")[0].value}).then(async a=>{t.outerHTML=await a.resolve()})}showChangeOwnerSelector(e){const o=e.dataset.page;new c(this.ajaxUrl).post({action:"show_change_owner_selector",page:o,ownerUid:e.dataset.owner,username:e.dataset.username}).then(async t=>{document.getElementById("o_"+o).outerHTML=await t.resolve()})}restoreOwner(e){const o=e.dataset.page,t=e.dataset.username??e.dataset.ifNotSet,a=document.createElement("span");a.setAttribute("id",`o_${o}`);const s=document.createElement("button");s.classList.add("ug_selector","changeowner","btn","btn-sm","btn-link"),s.setAttribute("type","button"),s.setAttribute("data-page",o),s.setAttribute("data-owner",e.dataset.owner),s.setAttribute("data-username",t),s.innerText=t,a.appendChild(s);const n=document.getElementById("o_"+o);n.parentNode.replaceChild(a,n)}restoreGroup(e){const o=e.dataset.page,t=e.dataset.groupname??e.dataset.ifNotSet,a=document.createElement("span");a.setAttribute("id",`g_${o}`);const s=document.createElement("button");s.classList.add("ug_selector","changegroup","btn","btn-sm","btn-link"),s.setAttribute("type","button"),s.setAttribute("data-page",o),s.setAttribute("data-group-id",e.dataset.groupId),s.setAttribute("data-groupname",t),s.innerText=t,a.appendChild(s);const n=document.getElementById("g_"+o);n.parentNode.replaceChild(a,n)}changeGroup(e){const o=e.dataset.page,t=document.getElementById("g_"+o);new c(this.ajaxUrl).post({action:"change_group",page:o,groupUid:e.dataset.groupId,newGroupUid:t.getElementsByTagName("select")[0].value}).then(async a=>{t.outerHTML=await a.resolve()})}showChangeGroupSelector(e){const o=e.dataset.page;new c(this.ajaxUrl).post({action:"show_change_group_selector",page:o,groupUid:e.dataset.groupId,groupname:e.dataset.groupname}).then(async t=>{document.getElementById("g_"+o).outerHTML=await t.resolve()})}initializeCheckboxGroups(){document.querySelectorAll("[data-checkbox-group]").forEach(o=>{const t=o.dataset.checkboxGroup,a=parseInt(o.value,10);i.setPermissionCheckboxes(t,a)})}initializeEvents(){const e=document.querySelector(this.options.containerSelector),o=document.querySelector(this.options.editControllerSelector);e!==null&&(new r("click",(t,a)=>{t.preventDefault(),this.setPermissions(a)}).delegateTo(e,".change-permission"),new r("click",(t,a)=>{t.preventDefault(),this.toggleEditLock(a)}).delegateTo(e,".editlock"),new r("click",(t,a)=>{t.preventDefault(),this.showChangeOwnerSelector(a)}).delegateTo(e,".changeowner"),new r("click",(t,a)=>{t.preventDefault(),this.showChangeGroupSelector(a)}).delegateTo(e,".changegroup"),new r("click",(t,a)=>{t.preventDefault(),this.restoreOwner(a)}).delegateTo(e,".restoreowner"),new r("click",(t,a)=>{t.preventDefault(),this.changeOwner(a)}).delegateTo(e,".saveowner"),new r("click",(t,a)=>{t.preventDefault(),this.restoreGroup(a)}).delegateTo(e,".restoregroup"),new r("click",(t,a)=>{t.preventDefault(),this.changeGroup(a)}).delegateTo(e,".savegroup")),o!==null&&new r("click",(t,a)=>{const s=a.dataset.checkChangePermissions.split(",").map(n=>n.trim());i.updatePermissionValue.apply(this,s)}).delegateTo(o,"[data-check-change-permissions]")}}var l=new i;export{l as default};
