diff --git a/backup-restore.js b/backup-restore.js index 2d1ab7c..f2c9fec 100644 --- a/backup-restore.js +++ b/backup-restore.js @@ -704,7 +704,8 @@ function normalizeRestoreData(data) { })).filter(x => x.path); const sidebarItems = (data.sidebar_items || []).map(x => ({ id: reqInt(x.id), - page: optText(x.page, 64) || '', + kind: ['item','divider','group'].includes(x.kind) ? x.kind : 'item', + page: optText(x.page, 64) || null, label: optText(x.label, 64) || '', icon: optText(x.icon, 64) || 'circle', section: x.section === 'admin' ? 'admin' : 'base', @@ -713,7 +714,7 @@ function normalizeRestoreData(data) { enabled: x.enabled !== false, created_at: optTs(x.created_at), updated_at: optTs(x.updated_at), - })).filter(x => x.page && x.label); + })).filter(x => x.kind !== 'item' || (x.page && x.label)); const faceRefs = (data.student_face_refs || []).map(x => ({ id: reqInt(x.id), student_id: reqInt(x.student_id), diff --git a/db/init.sql b/db/init.sql index 54ae427..a5a81f1 100644 --- a/db/init.sql +++ b/db/init.sql @@ -733,14 +733,16 @@ CREATE INDEX IF NOT EXISTS idx_student_achievements_student ON student_achieveme CREATE TABLE IF NOT EXISTS sidebar_items ( id SERIAL PRIMARY KEY, - page TEXT NOT NULL UNIQUE, - label TEXT NOT NULL, + kind TEXT NOT NULL DEFAULT 'item', + page TEXT UNIQUE, + label TEXT NOT NULL DEFAULT '', icon TEXT NOT NULL DEFAULT 'circle', section TEXT NOT NULL DEFAULT 'base', sort_order INT NOT NULL DEFAULT 0, tutor_visible BOOLEAN NOT NULL DEFAULT true, enabled BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ DEFAULT now(), - updated_at TIMESTAMPTZ DEFAULT now() + updated_at TIMESTAMPTZ DEFAULT now(), + CONSTRAINT sidebar_items_kind_check CHECK (kind IN ('item','divider','group')) ); CREATE INDEX IF NOT EXISTS idx_sidebar_items_section_order ON sidebar_items(section, sort_order, id); diff --git a/db/migration.sql b/db/migration.sql index b0d8dc8..e03767c 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -991,14 +991,21 @@ INSERT INTO settings (key, value) VALUES ('feedback_imgbb_key','') ON CONFLICT ( CREATE TABLE IF NOT EXISTS sidebar_items ( id SERIAL PRIMARY KEY, - page TEXT NOT NULL UNIQUE, - label TEXT NOT NULL, + kind TEXT NOT NULL DEFAULT 'item', + page TEXT UNIQUE, + label TEXT NOT NULL DEFAULT '', icon TEXT NOT NULL DEFAULT 'circle', section TEXT NOT NULL DEFAULT 'base', sort_order INT NOT NULL DEFAULT 0, tutor_visible BOOLEAN NOT NULL DEFAULT true, enabled BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ DEFAULT now(), - updated_at TIMESTAMPTZ DEFAULT now() + updated_at TIMESTAMPTZ DEFAULT now(), + CONSTRAINT sidebar_items_kind_check CHECK (kind IN ('item','divider','group')) ); +ALTER TABLE sidebar_items ADD COLUMN IF NOT EXISTS kind TEXT NOT NULL DEFAULT 'item'; +ALTER TABLE sidebar_items ALTER COLUMN page DROP NOT NULL; +ALTER TABLE sidebar_items ALTER COLUMN label SET DEFAULT ''; +ALTER TABLE sidebar_items DROP CONSTRAINT IF EXISTS sidebar_items_kind_check; +ALTER TABLE sidebar_items ADD CONSTRAINT sidebar_items_kind_check CHECK (kind IN ('item','divider','group')); CREATE INDEX IF NOT EXISTS idx_sidebar_items_section_order ON sidebar_items(section, sort_order, id); diff --git a/lib/sidebar.js b/lib/sidebar.js index 7c3f82f..85484ca 100644 --- a/lib/sidebar.js +++ b/lib/sidebar.js @@ -32,21 +32,28 @@ const DEFAULT_ITEMS = [ async function ensureSidebarItemsTable(pool) { await pool.query(`CREATE TABLE IF NOT EXISTS sidebar_items ( id SERIAL PRIMARY KEY, - page TEXT NOT NULL UNIQUE, - label TEXT NOT NULL, + kind TEXT NOT NULL DEFAULT 'item', + page TEXT UNIQUE, + label TEXT NOT NULL DEFAULT '', icon TEXT NOT NULL DEFAULT 'circle', section TEXT NOT NULL DEFAULT 'base', sort_order INT NOT NULL DEFAULT 0, tutor_visible BOOLEAN NOT NULL DEFAULT true, enabled BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ DEFAULT now(), - updated_at TIMESTAMPTZ DEFAULT now() + updated_at TIMESTAMPTZ DEFAULT now(), + CONSTRAINT sidebar_items_kind_check CHECK (kind IN ('item','divider','group')) )`); + await pool.query(`ALTER TABLE sidebar_items ADD COLUMN IF NOT EXISTS kind TEXT NOT NULL DEFAULT 'item'`); + await pool.query(`ALTER TABLE sidebar_items ALTER COLUMN page DROP NOT NULL`); + await pool.query(`ALTER TABLE sidebar_items ALTER COLUMN label SET DEFAULT ''`); + await pool.query(`ALTER TABLE sidebar_items DROP CONSTRAINT IF EXISTS sidebar_items_kind_check`); + await pool.query(`ALTER TABLE sidebar_items ADD CONSTRAINT sidebar_items_kind_check CHECK (kind IN ('item','divider','group'))`); await pool.query(`CREATE INDEX IF NOT EXISTS idx_sidebar_items_section_order ON sidebar_items(section, sort_order, id)`); for (const it of DEFAULT_ITEMS) { await pool.query( - `INSERT INTO sidebar_items (page, label, icon, section, sort_order, tutor_visible, enabled) - VALUES ($1,$2,$3,$4,$5,$6,$7) ON CONFLICT (page) DO NOTHING`, + `INSERT INTO sidebar_items (kind, page, label, icon, section, sort_order, tutor_visible, enabled) + VALUES ('item',$1,$2,$3,$4,$5,$6,$7) ON CONFLICT (page) DO NOTHING`, [it.page, it.label, it.icon, it.section, it.sort_order, it.tutor_visible, it.enabled] ); } @@ -54,7 +61,7 @@ async function ensureSidebarItemsTable(pool) { async function listSidebarItems(pool) { const { rows } = await pool.query( - `SELECT id, page, label, icon, section, sort_order, tutor_visible, enabled, updated_at + `SELECT id, kind, page, label, icon, section, sort_order, tutor_visible, enabled, updated_at FROM sidebar_items ORDER BY section ASC, sort_order ASC, id ASC` ); return rows; @@ -63,22 +70,34 @@ async function listSidebarItems(pool) { function normalizeSidebarPayload(input) { if (!Array.isArray(input)) throw new Error('items must be array'); const out = []; - const seen = new Set(); + const seenPages = new Set(); for (const raw of input) { if (!raw || typeof raw !== 'object') throw new Error('bad item'); - const page = String(raw.page || '').trim(); - if (!page || page.length > 64) throw new Error('bad page'); - if (seen.has(page)) throw new Error('duplicate page'); - seen.add(page); - const label = String(raw.label || '').trim(); - if (!label || label.length > 64) throw new Error('bad label'); - const icon = String(raw.icon || '').trim(); - if (!icon || icon.length > 64) throw new Error('bad icon'); + const kind = ['item','divider','group'].includes(raw.kind) ? raw.kind : 'item'; + const id = Number.isFinite(Number(raw.id)) ? Math.trunc(Number(raw.id)) : null; const section = raw.section === 'admin' ? 'admin' : 'base'; const sort_order = Number.isFinite(Number(raw.sort_order)) ? Math.trunc(Number(raw.sort_order)) : 0; const tutor_visible = raw.tutor_visible === true || raw.tutor_visible === 'true'; const enabled = raw.enabled === undefined ? true : (raw.enabled === true || raw.enabled === 'true'); - out.push({ page, label, icon, section, sort_order, tutor_visible, enabled }); + + if (kind === 'item') { + const page = String(raw.page || '').trim(); + if (!page || page.length > 64) throw new Error('bad page'); + if (seenPages.has(page)) throw new Error('duplicate page'); + seenPages.add(page); + const label = String(raw.label || '').trim(); + if (!label || label.length > 64) throw new Error('bad label'); + const icon = String(raw.icon || '').trim(); + if (!icon || icon.length > 64) throw new Error('bad icon'); + out.push({ id, kind, page, label, icon, section, sort_order, tutor_visible, enabled }); + } else { + const label = String(raw.label || '').trim(); + if (kind === 'group' && (!label || label.length > 64)) throw new Error('bad label'); + if (label.length > 64) throw new Error('bad label'); + const icon = String(raw.icon || 'circle').trim() || 'circle'; + if (icon.length > 64) throw new Error('bad icon'); + out.push({ id, kind, page: null, label, icon, section, sort_order, tutor_visible, enabled }); + } } return out; } diff --git a/public/admin.css b/public/admin.css index 36ca6d4..023136c 100644 --- a/public/admin.css +++ b/public/admin.css @@ -27,6 +27,7 @@ body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;b .sidebar nav{display:flex;flex-direction:column;gap:10px;padding:0 8px} .nav-group{display:flex;flex-direction:column;gap:2px} .nav-group-title{font-size:.66rem;font-weight:700;text-transform:uppercase;letter-spacing:.06em;color:var(--muted);padding:6px 10px 4px;user-select:none;opacity:.8} +.nav-divider{height:1px;background:var(--border);margin:6px 10px;opacity:.7} .nav-items{display:flex;flex-direction:column;gap:1px} .sidebar nav a{display:flex;align-items:center;gap:10px;padding:7px 10px;border-radius:8px;color:var(--muted);text-decoration:none;font-size:.84rem;font-weight:500;line-height:1.3;transition:all .14s ease;position:relative} diff --git a/public/admin.js b/public/admin.js index 4b6274c..d2ff55f 100644 --- a/public/admin.js +++ b/public/admin.js @@ -117,13 +117,15 @@ function buildSidebar(active) { 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' }); + const toNav = (i) => ({ page: i.page, label: i.label, icon: i.icon, kind: i.kind || 'item', 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 => { + if (n.kind === 'divider') return ``; + if (n.kind === 'group') return ``; const isAct = n.page === active; const adminOnly = n.section === 'admin' || n.tutorVisible === false; const badgeHtml = n.badge ? ` 0 ? '' : 'hidden'}>${notifyUnread > 99 ? '99+' : notifyUnread}` : ''; diff --git a/public/js/sidebar-settings.js b/public/js/sidebar-settings.js index 4d61aac..cd95060 100644 --- a/public/js/sidebar-settings.js +++ b/public/js/sidebar-settings.js @@ -1,49 +1,65 @@ let sidebarItems = []; +let cidCounter = 0; +function newCid() { return 'c' + (++cidCounter); } async function loadSidebarSettings() { const res = await fetch(`${API}/api/sidebar`, { headers: hdr() }); if (!res.ok) throw new Error('load failed'); const data = await res.json(); - sidebarItems = (data.items || []).map(x => ({ ...x })); + sidebarItems = (data.items || []).map(x => ({ ...x, _cid: newCid() })); renderSidebarSettings(); } function renderSidebarSettings() { - const base = sidebarItems.filter(x => x.section === 'base').sort((a, b) => a.sort_order - b.sort_order); - const admin = sidebarItems.filter(x => x.section === 'admin').sort((a, b) => a.sort_order - b.sort_order); + const base = sidebarItems.filter(x => x.section === 'base').sort((a, b) => (a.sort_order || 0) - (b.sort_order || 0) || String(a._cid).localeCompare(b._cid)); + const admin = sidebarItems.filter(x => x.section === 'admin').sort((a, b) => (a.sort_order || 0) - (b.sort_order || 0) || String(a._cid).localeCompare(b._cid)); document.getElementById('sidebarBaseList').innerHTML = base.map(renderSidebarItem).join(''); document.getElementById('sidebarAdminList').innerHTML = admin.map(renderSidebarItem).join(''); renderIcons(); bindSidebarDrag(); + bindSidebarLabels(); + bindSidebarDeletes(); + ensureInsertButtons(); } function renderSidebarItem(it) { const disabled = it.enabled === false; const isTutor = it.section === 'base' && it.tutor_visible === true && it.enabled !== false; - const isAdminOnly = it.section === 'admin' || it.tutor_visible === false; let statusCls = 'status-admin'; let statusText = 'Только админ'; if (disabled) { statusCls = 'status-off'; statusText = 'Отключён'; } else if (isTutor) { statusCls = 'status-tutor'; statusText = 'Доступно тьюторам'; } - const handle = ``; - const icon = ``; - const tutorToggle = it.section === 'base' - ? `` + const handle = ``; + let iconHtml = ''; + let labelHtml = ''; + if (it.kind === 'divider') { + iconHtml = ``; + labelHtml = ``; + } else if (it.kind === 'group') { + iconHtml = ``; + labelHtml = ``; + } else { + iconHtml = ``; + labelHtml = ``; + } + const tutorToggle = (it.section === 'base' && it.kind === 'item') + ? `` : ''; - const enabledToggle = it.page === 'sidebar' - ? `` - : ``; - return `