feat(sidebar): настройка пунктов меню, порядок и доступность для тьюторов; подсветка admin-only
This commit is contained in:
+30
-4
@@ -1,6 +1,7 @@
|
||||
const API = location.origin;
|
||||
let token = sessionStorage.getItem('authToken');
|
||||
let currentUser = null;
|
||||
let sidebarConfig = null;
|
||||
function hdr() { return { 'X-Auth-Token': token }; }
|
||||
function hdrJson() { return { 'Content-Type': 'application/json', 'X-Auth-Token': token }; }
|
||||
function esc(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }
|
||||
@@ -37,6 +38,7 @@ async function checkAuth() {
|
||||
if (!res.ok) { cleanupSession(); return false; }
|
||||
currentUser = await res.json();
|
||||
await initDateTime();
|
||||
await loadSidebarConfig();
|
||||
return true;
|
||||
} catch { cleanupSession(); return false; }
|
||||
}
|
||||
@@ -50,6 +52,15 @@ function cleanupSession() {
|
||||
|
||||
function isAdmin() { return currentUser && currentUser.role === 'admin'; }
|
||||
|
||||
async function loadSidebarConfig() {
|
||||
try {
|
||||
const res = await fetch(`${API}/api/sidebar`, { headers: hdr() });
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
sidebarConfig = Array.isArray(data.items) ? data.items : null;
|
||||
} catch { sidebarConfig = null; }
|
||||
}
|
||||
|
||||
async function requireAdminPage() {
|
||||
if (!(await checkAuth())) return false;
|
||||
if (!isAdmin()) { location.href = 'journal.html'; return false; }
|
||||
@@ -65,8 +76,8 @@ async function doLogout() {
|
||||
function buildSidebar(active) {
|
||||
const el = document.getElementById('sidebar');
|
||||
if (!el) return;
|
||||
const base = [
|
||||
{ page: 'dashboard', label: 'Дашборд', icon: 'layout-dashboard' },
|
||||
const fallbackBase = [
|
||||
{ page: 'dashboard', label: 'Дашборд', icon: 'layout-dashboard', section: 'base', tutorVisible: true },
|
||||
{ page: 'profile', label: 'Профиль', icon: 'user' },
|
||||
{ page: 'journal', label: 'Журнал', icon: 'book-open' },
|
||||
{ page: 'lessons', label: 'Отчёты о занятиях', icon: 'notebook-pen' },
|
||||
@@ -85,7 +96,7 @@ function buildSidebar(active) {
|
||||
{ page: 'trash', label: 'Корзина', icon: 'trash-2' },
|
||||
{ page: 'notes', label: 'Заметки', icon: 'sticky-note' },
|
||||
];
|
||||
const adminNav = [
|
||||
const fallbackAdmin = [
|
||||
{ page: 'settings', label: 'Настройки', icon: 'settings' },
|
||||
{ page: 'modules', label: 'Темы модулей', icon: 'layers' },
|
||||
{ page: 'users', label: 'Пользователи', icon: 'user-cog' },
|
||||
@@ -95,14 +106,29 @@ function buildSidebar(active) {
|
||||
{ page: 'faces', label: 'Сканер лиц', icon: 'scan-face' },
|
||||
{ page: 'apikeys', label: 'API-ключи', icon: 'key-round' },
|
||||
{ page: 'bans', label: 'Блокировки', icon: 'shield-off' },
|
||||
{ page: 'sidebar', label: 'Меню', icon: 'panel-left' },
|
||||
];
|
||||
fallbackBase.forEach(i => { i.section = 'base'; i.tutorVisible = true; });
|
||||
fallbackAdmin.forEach(i => { i.section = 'admin'; i.tutorVisible = false; });
|
||||
let base = fallbackBase;
|
||||
let adminNav = fallbackAdmin;
|
||||
if (sidebarConfig) {
|
||||
const sorted = [...sidebarConfig].sort((a, b) => {
|
||||
if (a.section !== b.section) return a.section === 'base' ? -1 : 1;
|
||||
return (a.sort_order || 0) - (b.sort_order || 0);
|
||||
});
|
||||
const toNav = (i) => ({ page: i.page, label: i.label, icon: i.icon, badge: i.page === 'notifications', chatBadge: i.page === 'chat' });
|
||||
base = sorted.filter(i => i.section === 'base' && i.enabled !== false).map(toNav);
|
||||
adminNav = sorted.filter(i => i.section === 'admin' && i.enabled !== false).map(toNav);
|
||||
}
|
||||
const userName = currentUser?.name || currentUser?.username || '';
|
||||
const roleLabel = isAdmin() ? 'Администратор' : 'Тутор';
|
||||
const renderItem = n => {
|
||||
const isAct = n.page === active;
|
||||
const adminOnly = n.section === 'admin' || n.tutorVisible === false;
|
||||
const badgeHtml = n.badge ? `<span class="sidebar-badge" id="sidebarNotifyBadge" ${notifyUnread > 0 ? '' : 'hidden'}>${notifyUnread > 99 ? '99+' : notifyUnread}</span>` : '';
|
||||
const chatBadgeHtml = n.chatBadge ? `<span class="sidebar-badge" id="sidebarChatBadge" ${chatUnread > 0 ? '' : 'hidden'}>${chatUnread > 99 ? '99+' : chatUnread}</span>` : '';
|
||||
return `<a href="${n.page}.html" data-nav="${n.page}" class="${isAct ? 'active' : ''}" title="${n.label}">
|
||||
return `<a href="${n.page}.html" data-nav="${n.page}" class="${isAct ? 'active' : ''} ${isAdmin() && adminOnly ? 'nav-admin-only' : ''}" title="${n.label}${isAdmin() && adminOnly ? ' (только администратор)' : ''}">
|
||||
<i data-lucide="${n.icon}"></i>
|
||||
<span class="nav-text">${n.label}</span>
|
||||
${badgeHtml}
|
||||
|
||||
Reference in New Issue
Block a user