Пользователи: цвет при создании и редактировании
This commit is contained in:
+18
-1
@@ -5,6 +5,9 @@ async function init() {
|
||||
document.getElementById('addUserBtn').addEventListener('click', () => openModal(null));
|
||||
document.getElementById('uCancel').addEventListener('click', closeModal);
|
||||
document.getElementById('uSave').addEventListener('click', saveUser);
|
||||
document.getElementById('uColorRandom').addEventListener('click', () => {
|
||||
document.getElementById('uColor').value = randomUserColor();
|
||||
});
|
||||
document.getElementById('uRole').addEventListener('change', () => {
|
||||
document.getElementById('uBranchesField').style.display = document.getElementById('uRole').value === 'admin' ? 'none' : '';
|
||||
});
|
||||
@@ -13,6 +16,16 @@ async function init() {
|
||||
|
||||
let branches = [];
|
||||
let editingId = null;
|
||||
const USER_COLORS = ['#2563eb', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#d97706', '#16a34a', '#0d9488', '#0891b2', '#4f46e5', '#9333ea', '#475569'];
|
||||
|
||||
function userColor(value) {
|
||||
const raw = String(value || '').trim();
|
||||
return /^#[0-9a-fA-F]{6}$/.test(raw) ? raw.toLowerCase() : USER_COLORS[0];
|
||||
}
|
||||
|
||||
function randomUserColor() {
|
||||
return USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)];
|
||||
}
|
||||
|
||||
async function loadUsers() {
|
||||
const [uRes, bRes] = await Promise.all([
|
||||
@@ -37,11 +50,13 @@ function renderUsers(users) {
|
||||
const roleCls = u.role === 'admin' ? 'badge-admin' : 'badge-tutor';
|
||||
const roleTxt = u.role === 'admin' ? 'Администратор' : 'Тутор';
|
||||
const status = u.is_active ? '<span class="status-ok">Активен</span>' : '<span class="status-off">Заблокирован</span>';
|
||||
const color = userColor(u.color);
|
||||
const delBtn = u.id === currentUser.id
|
||||
? ''
|
||||
: `<button class="row-btn del" data-del="${u.id}" title="Удалить">×</button>`;
|
||||
tr.innerHTML = `
|
||||
<td>${u.id}</td>
|
||||
<td><span class="user-color-swatch" style="background:${color}" title="${color}"></span></td>
|
||||
<td>${esc(u.username)}</td>
|
||||
<td>${esc(u.name || '')}</td>
|
||||
<td><span class="${roleCls}">${roleTxt}</span></td>
|
||||
@@ -66,6 +81,7 @@ function openModal(user) {
|
||||
document.getElementById('userModalTitle').textContent = user ? `Пользователь: ${user.username}` : 'Новый пользователь';
|
||||
document.getElementById('uUsername').value = user ? user.username : '';
|
||||
document.getElementById('uName').value = user ? (user.name || '') : '';
|
||||
document.getElementById('uColor').value = user ? userColor(user.color) : randomUserColor();
|
||||
document.getElementById('uRole').value = user ? user.role : 'tutor';
|
||||
document.getElementById('uPassword').value = '';
|
||||
document.getElementById('uActive').checked = user ? user.is_active : true;
|
||||
@@ -92,13 +108,14 @@ async function saveUser() {
|
||||
const username = document.getElementById('uUsername').value.trim();
|
||||
const name = document.getElementById('uName').value.trim() || null;
|
||||
const role = document.getElementById('uRole').value;
|
||||
const color = userColor(document.getElementById('uColor').value);
|
||||
const password = document.getElementById('uPassword').value;
|
||||
const is_active = document.getElementById('uActive').checked;
|
||||
const branchSel = document.getElementById('uBranches');
|
||||
const branch_ids = role === 'admin' ? [] : [...branchSel.selectedOptions].map(o => Number(o.value));
|
||||
if (!username) { showToast('Укажите логин'); return; }
|
||||
if (!editingId && password.length < 6) { showToast('Пароль должен быть не короче 6 символов'); return; }
|
||||
const payload = { username, name, role, is_active, branch_ids };
|
||||
const payload = { username, name, role, is_active, branch_ids, color };
|
||||
if (password) payload.password = password;
|
||||
const res = await fetch(editingId ? `${API}/api/users/${editingId}` : `${API}/api/users`, {
|
||||
method: editingId ? 'PUT' : 'POST',
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Цвет</th>
|
||||
<th>Логин</th>
|
||||
<th>Имя</th>
|
||||
<th>Роль</th>
|
||||
@@ -53,6 +54,13 @@
|
||||
<label>Имя</label>
|
||||
<input type="text" id="uName" class="settings-input" placeholder="Имя Фамилия">
|
||||
</div>
|
||||
<div class="settings-field">
|
||||
<label>Цвет</label>
|
||||
<div style="display:flex;align-items:center;gap:8px">
|
||||
<input type="color" id="uColor" class="settings-input user-color-input" value="#2563eb">
|
||||
<button type="button" class="btn-primary ghost" id="uColorRandom" style="padding:6px 10px">Случайный</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settings-field">
|
||||
<label>Роль</label>
|
||||
<select id="uRole" class="settings-input">
|
||||
@@ -96,6 +104,8 @@
|
||||
.row-btn:hover{background:var(--bg);color:var(--accent)}
|
||||
.row-btn.del:hover{color:#ef4444;background:rgba(239,68,68,.1)}
|
||||
.user-branches{max-width:220px;color:var(--muted);font-size:.78rem}
|
||||
.user-color-input{width:72px;height:34px;padding:2px;cursor:pointer}
|
||||
.user-color-swatch{display:inline-block;width:18px;height:18px;border-radius:50%;border:1px solid rgba(127,127,127,.4);vertical-align:middle}
|
||||
</style>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user