(function () { function getGroupCheckboxes(group) { return Array.prototype.slice.call(group.querySelectorAll('input[name="permission_keys[]"]')); } function getPermissionKey(checkbox) { return String((checkbox && (checkbox.getAttribute('data-permission-key') || checkbox.value)) || '').trim().toLowerCase(); } function getActionKey(checkbox) { var permissionKey = getPermissionKey(checkbox); var parts = permissionKey.split('.'); if (parts.length !== 2) { return ''; } return String(parts[1] || '').trim().toLowerCase(); } function syncPermissionGroup(group) { var checkboxes = getGroupCheckboxes(group); if (!checkboxes.length) { return; } var readCheckbox = null; var nonReadChecked = false; checkboxes.forEach(function (checkbox) { if (getActionKey(checkbox) === 'read') { readCheckbox = checkbox; return; } if (checkbox.checked) { nonReadChecked = true; } }); if (!readCheckbox) { return; } if (!readCheckbox.checked && nonReadChecked) { readCheckbox.checked = true; } if (!readCheckbox.checked) { checkboxes.forEach(function (checkbox) { if (getActionKey(checkbox) !== 'read') { checkbox.checked = false; } }); } } function handleGroupChange(event) { var checkbox = event.target && event.target.matches ? event.target : null; if (!checkbox || checkbox.tagName !== 'INPUT' || checkbox.type !== 'checkbox') { return; } var actionKey = getActionKey(checkbox); var group = checkbox.closest('.accordion-item'); if (!group) { return; } var checkboxes = getGroupCheckboxes(group); var readCheckbox = checkboxes.find(function (candidate) { return getActionKey(candidate) === 'read'; }) || null; if (!readCheckbox) { return; } if (actionKey === 'read' && !checkbox.checked) { checkboxes.forEach(function (candidate) { if (candidate !== checkbox) { candidate.checked = false; } }); return; } if (actionKey !== 'read' && checkbox.checked) { readCheckbox.checked = true; } } function initPermissionGroups() { document.querySelectorAll('.accordion-item').forEach(function (group) { syncPermissionGroup(group); }); document.addEventListener('change', function (event) { var checkbox = event.target; if (!checkbox || checkbox.tagName !== 'INPUT' || checkbox.type !== 'checkbox') { return; } if (String(checkbox.getAttribute('name') || '') !== 'permission_keys[]') { return; } handleGroupChange(event); syncPermissionGroup(checkbox.closest('.accordion-item')); }); } initPermissionGroups(); }());