// The users table in Settings: the role, the machines a user may use (a // dialog with a list to tick), the password sign-in and whether the user // may switch it themselves. Each change is saved at once. (function () { 'use strict'; var table = document.querySelector('table.users'); if (!table) return; var $ = function (id) { return document.getElementById(id); }; function save(id, body) { return fetch('/api/panel/users/' + id, {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(body)}) .then(function (r) { return r.json(); }) .catch(function () { return {ok: false, error: 'ошибка сети'}; }); } function note(tr, text, bad) { var n = tr.querySelector('.u-note'); if (!n) { n = document.createElement('div'); n.className = 'u-note'; tr.cells[0].appendChild(n); } n.textContent = text; n.classList.toggle('bad', !!bad); n.classList.remove('fade'); void n.offsetWidth; n.classList.add('fade'); } table.addEventListener('change', function (ev) { var tr = ev.target.closest('tr'), id = tr && tr.dataset.id, el = ev.target; if (!id) return; var body = {}, undo; if (el.classList.contains('u-role')) { body.role = el.value; undo = function () { el.value = el.value === 'user' ? 'admin' : 'user'; }; } else if (el.classList.contains('u-pw')) { body.password_off = !el.checked; undo = function () { el.checked = !el.checked; }; } else if (el.classList.contains('u-lock')) { body.pw_locked = el.checked; undo = function () { el.checked = !el.checked; }; } else { return; } save(id, body).then(function (r) { if (!r.ok) { undo(); note(tr, r.error || 'не сохранилось', true); return; } note(tr, 'сохранено'); if (body.role) { var user = body.role === 'user'; tr.querySelector('.u-agents').hidden = !user; tr.querySelector('.u-count').hidden = !user; } }); }); // ---- the machines of a user ---- var dlg = $('uadlg'), list = $('ualist'), cur = null; function render(filter) { var f = (filter || '').toLowerCase(); list.querySelectorAll('label').forEach(function (l) { l.hidden = f && l.textContent.toLowerCase().indexOf(f) < 0; }); } table.addEventListener('click', function (ev) { var b = ev.target.closest('.u-agents'); if (!b) return; var tr = b.closest('tr'); cur = tr; $('uaname').textContent = tr.dataset.name; list.innerHTML = '