From 0757ac1639ab10847e8aad44497064eb28e779c8 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 5 Nov 2026 13:38:20 +0300 Subject: [PATCH] Fix digest page and logic --- public/digest.html | 66 ++++++++++++++++++++++++++------------- public/js/digest.js | 76 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 23 deletions(-) diff --git a/public/digest.html b/public/digest.html index 8e56489..865c8a9 100644 --- a/public/digest.html +++ b/public/digest.html @@ -6,16 +6,30 @@ Дайджест — Админ-панель @@ -116,19 +132,25 @@
-
-
-

Дайджест

-

Важные сообщения для команды

- -
-
-
diff --git a/public/js/digest.js b/public/js/digest.js index 2f68919..2fd218f 100644 --- a/public/js/digest.js +++ b/public/js/digest.js @@ -9,6 +9,8 @@ let previewRemoved = false; let previewFile = null; let digestImages = []; + let activeDigestId = null; + let digestNavObserver = null; const DRAFT_KEY = 'whatido:digest:draft'; function draftPayload() { @@ -206,6 +208,58 @@ : (digests.length ? 'Сообщений: ' + digests.length : ''); } + function digestNavItemHtml(d) { + const pinned = !!d.is_pinned; + return ''; + } + + function setActiveDigestNav(id, scrollIntoView) { + activeDigestId = id; + const items = document.querySelectorAll('#digestNav [data-scroll]'); + items.forEach(function(el) { + const on = Number(el.dataset.scroll) === id; + el.classList.toggle('active', on); + if (on && scrollIntoView) { + const box = el.parentElement; + if (box) { + const top = el.offsetTop - box.clientHeight / 2 + el.offsetHeight / 2; + box.scrollTo({ top: Math.max(0, top), behavior: 'smooth' }); + } + } + }); + } + + function observeDigestCards() { + if (digestNavObserver) { digestNavObserver.disconnect(); digestNavObserver = null; } + const cards = document.querySelectorAll('#digestList .digest-card'); + if (!cards.length || typeof IntersectionObserver === 'undefined') return; + digestNavObserver = new IntersectionObserver(function(entries) { + const visible = entries.filter(function(e) { return e.isIntersecting; }); + if (!visible.length) return; + visible.sort(function(a, b) { return a.boundingClientRect.top - b.boundingClientRect.top; }); + setActiveDigestNav(Number(visible[0].target.dataset.id), false); + }, { rootMargin: '-15% 0px -65% 0px', threshold: 0 }); + cards.forEach(function(c) { digestNavObserver.observe(c); }); + } + + function renderDigestNav(list) { + const el = document.getElementById('digestNav'); + if (!el) return; + if (!list.length) { + if (digestNavObserver) { digestNavObserver.disconnect(); digestNavObserver = null; } + el.innerHTML = '
' + (digests.length ? 'Ничего не найдено' : 'Сообщений пока нет') + '
'; + activeDigestId = null; + return; + } + el.innerHTML = list.map(digestNavItemHtml).join(''); + const ids = list.map(function(d) { return d.id; }); + setActiveDigestNav(ids.indexOf(activeDigestId) === -1 ? list[0].id : activeDigestId, false); + observeDigestCards(); + } + function renderDigests() { const el = document.getElementById('digestList'); if (!el) return; @@ -218,6 +272,7 @@ '

Опубликуйте важное объявление — его увидят все тьюторы и админы.

' + (isAdmin() ? '' : '') + '
'; + renderDigestNav([]); renderIcons(); return; } @@ -225,10 +280,12 @@ el.innerHTML = '
' + '

Ничего не найдено

' + '

По запросу «' + esc(query.trim()) + '» сообщений нет

'; + renderDigestNav([]); renderIcons(); return; } el.innerHTML = list.map(digestCardHtml).join(''); + renderDigestNav(list); renderIcons(); } @@ -238,6 +295,11 @@ el.innerHTML = '
' + '

Не удалось загрузить сообщения

' + '
'; + const nav = document.getElementById('digestNav'); + if (nav) { + if (digestNavObserver) { digestNavObserver.disconnect(); digestNavObserver = null; } + nav.innerHTML = '
Не удалось загрузить
'; + } renderIcons(); } @@ -678,6 +740,18 @@ document.getElementById('digestNewBtn').addEventListener('click', function() { openDigestModal(null); }); const search = document.getElementById('digestSearch'); if (search) search.addEventListener('input', function() { query = search.value; renderDigests(); }); + document.addEventListener('click', function(e) { + const nav = e.target.closest('#digestNav [data-scroll]'); + if (!nav) return; + const id = parseInt(nav.dataset.scroll, 10); + const card = document.querySelector('#digestList .digest-card[data-id="' + id + '"]'); + if (!card) return; + setActiveDigestNav(id, true); + card.scrollIntoView({ behavior: 'smooth', block: 'start' }); + card.classList.add('digest-highlight'); + clearTimeout(card._hlTimer); + card._hlTimer = setTimeout(function() { card.classList.remove('digest-highlight'); }, 1600); + }); document.addEventListener('click', async function(e) { const actEl = e.target.closest('[data-action]'); if (actEl) { @@ -731,7 +805,7 @@ if (admin || mine) openDigestModal(d); else { const card = document.querySelector('[data-page="digest"] .digest-card[data-id="' + id + '"]'); - if (card) { card.scrollIntoView({ behavior: 'smooth', block: 'center' }); card.classList.add('digest-highlight'); setTimeout(function() { card.classList.remove('digest-highlight'); }, 1500); } + if (card) { setActiveDigestNav(id, true); card.scrollIntoView({ behavior: 'smooth', block: 'center' }); card.classList.add('digest-highlight'); setTimeout(function() { card.classList.remove('digest-highlight'); }, 1500); } } } }