Files
itflow/js/role_form.js

66 lines
2.8 KiB
JavaScript

// New Role and Edit Role (admin/modals/role/role_form.php). An admin role
// opens everything, so its levels are hidden while Admin is on. Picking a
// module's level says, under its name, what that level lets its agents do -
// each level takes in the ones before it. Each client's pages need Clients, so a role
// given other modules with Clients at None is warned. The server checks it
// all again.
//
// Wrapped in an IIFE because a modal can be opened, closed and opened again
// in one page load, which re-runs this file.
(function () {
document.querySelectorAll('[data-role-form]:not([data-wired])').forEach(function (form) {
form.setAttribute('data-wired', '1');
const admin = form.querySelector('[data-role-admin]');
const levels = form.querySelector('[data-role-levels]');
const note = form.querySelector('[data-role-admin-note]');
const needsClients = form.querySelector('[data-role-needs-clients]');
function levelOf(name) {
const picked = form.querySelector('[data-role-module="' + name + '"] input[type=radio]:checked');
return picked ? parseInt(picked.value, 10) : 0;
}
function checkClients() {
needsClients.hidden = levelOf('module_client') > 0
|| !['module_support', 'module_credential', 'module_sales', 'module_financial'].some(function (name) {
return levelOf(name) > 0;
});
}
function showLevels() {
levels.hidden = admin.checked;
note.hidden = !admin.checked;
}
admin.addEventListener('change', showLevels);
form.querySelectorAll('[data-role-module]').forEach(function (module) {
module.addEventListener('change', function (e) {
if (e.target.type !== 'radio') {
return;
}
const level = parseInt(e.target.value, 10);
const allows = JSON.parse(module.dataset.roleAllows || '{}');
const text = Object.keys(allows).filter(function (value) {
return parseInt(value, 10) <= level;
}).map(function (value) {
return allows[value];
}).join(' · ') || 'Nothing';
const can = module.querySelector('[data-role-can]');
can.title = text;
can.classList.toggle('text-body', level > 0);
can.classList.toggle('text-secondary', level === 0);
can.replaceChildren();
const icon = document.createElement('i');
icon.className = 'fas fa-fw me-1 ' + (level > 0 ? 'fa-check text-success' : 'fa-times');
icon.setAttribute('aria-hidden', 'true');
can.append(icon, text);
checkClients();
});
});
});
})();