Update digest page and logic

This commit is contained in:
dev
2026-11-05 13:34:05 +03:00
parent f07898721c
commit 67fcaf452a
2 changed files with 161 additions and 89 deletions
+53 -33
View File
@@ -110,6 +110,11 @@
} catch { return null; }
}
function digestInitial(name) {
const s = String(name || '').trim();
return s ? s.charAt(0).toUpperCase() : '?';
}
function digestCardHtml(d) {
const admin = isAdmin();
const mine = currentUser && d.author_id === currentUser.id;
@@ -119,57 +124,68 @@
const postFuture = d.post_at && new Date(d.post_at).getTime() > nowTs;
const hidden = d.hide_at && new Date(d.hide_at) <= nowTs;
const badge = [];
if (pinned) badge.push('<span class="digest-badge">Закреплено</span>');
if (postFuture) badge.push('<span class="digest-badge scheduled">Запланировано на ' + esc(fmtDateTimeY(d.post_at)) + '</span>');
if (hidden) badge.push('<span class="digest-badge dim">Скрыто</span>');
else if (d.hide_at) badge.push('<span class="digest-badge dim">Скроется ' + esc(fmtDateTimeY(d.hide_at)) + '</span>');
const badges = [];
if (pinned) badges.push('<span class="digest-badge"><i data-lucide="pin"></i>Закреплено</span>');
if (postFuture) badges.push('<span class="digest-badge scheduled"><i data-lucide="clock"></i>Запланировано на ' + esc(fmtDateTimeY(d.post_at)) + '</span>');
if (hidden) badges.push('<span class="digest-badge dim"><i data-lucide="eye-off"></i>Скрыто</span>');
else if (d.hide_at) badges.push('<span class="digest-badge dim"><i data-lucide="clock"></i>Скроется ' + esc(fmtDateTimeY(d.hide_at)) + '</span>');
const preview = d.preview_path
? '<div class="digest-head-img"><button type="button" class="digest-shot" data-img="' + esc(d.preview_path) + '" title="Открыть превью"><img class="digest-preview-img" src="' + esc(thumbSrc(d.preview_path)) + '" alt="превью"></button></div>'
const cover = d.preview_path
? '<div class="digest-cover"><button type="button" data-img="' + esc(d.preview_path) + '" title="Открыть обложку"><img src="' + esc(thumbSrc(d.preview_path)) + '" alt="' + esc(d.title) + '" loading="lazy" decoding="async"></button></div>'
: '';
const images = Array.isArray(d.images) ? d.images : [];
let imagesHtml = '';
let galleryHtml = '';
if (images.length) {
imagesHtml = '<div class="digest-images-row">' + images.slice(0, 4).map(function(img, i) {
return '<button type="button" class="digest-shot" data-img="' + esc(img.path) + '" data-gal-index="' + i + '" title="' + esc(img.name || '') + '" aria-label="' + esc(img.name || '') + '">' +
'<img class="digest-card-thumb" src="' + esc(thumbSrc(img.path)) + '" alt="' + esc(img.name || '') + '" loading="lazy" decoding="async">' +
'<span class="digest-shot-zoom" aria-hidden="true"><i data-lucide="maximize-2"></i></span>' +
galleryHtml = '<div class="digest-gallery">' + images.slice(0, 4).map(function(img, i) {
const more = (i === 3 && images.length > 4) ? '<span class="digest-more">+' + (images.length - 4) + '</span>' : '';
return '<button type="button" class="digest-shot" data-img="' + esc(img.path) + '" title="' + esc(img.name || '') + '" aria-label="' + esc(img.name || '') + '">' +
'<img src="' + esc(thumbSrc(img.path)) + '" alt="' + esc(img.name || '') + '" loading="lazy" decoding="async">' +
more +
'</button>';
}).join('') + (images.length > 4 ? '<span class="digest-none">+ ' + (images.length - 4) + '</span>' : '') + '</div>';
}).join('') + '</div>';
}
const actions = [];
if (canEdit) {
actions.push('<button type="button" class="btn-link" data-action="edit" data-id="' + d.id + '">Изменить</button>');
actions.push('<button type="button" class="btn-link danger" data-action="delete" data-id="' + d.id + '">Удалить</button>');
}
if (admin) {
actions.push('<button type="button" class="btn-link" data-action="pin" data-id="' + d.id + '" data-pin="' + (pinned ? '0' : '1') + '">' + (pinned ? 'Открепить' : 'Закрепить') + '</button>');
actions.push('<button type="button" class="digest-act' + (pinned ? ' is-pinned' : '') + '" data-action="pin" data-id="' + d.id + '" data-pin="' + (pinned ? '0' : '1') + '"><i data-lucide="pin"></i>' + (pinned ? 'Открепить' : 'Закрепить') + '</button>');
}
if (canEdit) {
actions.push('<button type="button" class="digest-act" data-action="edit" data-id="' + d.id + '"><i data-lucide="pencil"></i>Изменить</button>');
actions.push('<button type="button" class="digest-act danger" data-action="delete" data-id="' + d.id + '"><i data-lucide="trash-2"></i>Удалить</button>');
}
const bodyText = d.body || '';
const longBody = bodyText.length > 600;
const bodyHtml = '<div class="digest-body' + (longBody ? ' clamp' : '') + '">' + esc(bodyText) + '</div>' +
(longBody ? '<button type="button" class="digest-body-toggle" data-action="toggle-body" data-id="' + d.id + '">Показать полностью</button>' : '');
(longBody ? '<button type="button" class="digest-body-toggle" data-action="toggle-body" data-id="' + d.id + '"><span class="digest-toggle-label">Читать полностью</span><i data-lucide="chevron-down"></i></button>' : '');
return '<div class="digest-card' + (pinned ? ' pinned' : '') + (hidden ? ' digest-hidden' : '') + '" data-id="' + d.id + '">' +
'<div class="digest-head">' +
const createdTs = d.created_at ? new Date(d.created_at).getTime() : 0;
const updatedTs = d.updated_at ? new Date(d.updated_at).getTime() : 0;
const edited = createdTs && updatedTs && updatedTs - createdTs > 60000;
const note = edited ? '<span class="digest-foot-note"><i data-lucide="pencil"></i>Изменено ' + esc(fmtDateTimeY(d.updated_at)) + '</span>' : '';
const footHtml = (note || actions.length)
? '<footer class="digest-foot">' + note + '<div class="digest-actions">' + actions.join('') + '</div></footer>'
: '';
return '<article class="digest-card' + (pinned ? ' pinned' : '') + (hidden ? ' digest-hidden' : '') + '" data-id="' + d.id + '">' +
'<div class="digest-card-top">' +
(badges.length ? '<div class="digest-badges">' + badges.join('') + '</div>' : '') +
'<h3 class="digest-title">' + esc(d.title) + '</h3>' +
badge.join('') +
'<div class="digest-meta">' +
'<span class="digest-avatar" aria-hidden="true">' + esc(digestInitial(d.author_name)) + '</span>' +
'<div class="digest-meta-text">' +
'<span class="digest-author">' + esc(d.author_name || '—') + '</span>' +
'<span class="digest-date"><i data-lucide="calendar"></i>' + esc(fmtDateTimeY(d.published_at || d.created_at)) + '</span>' +
'</div>' +
'</div>' +
'</div>' +
preview +
'<div class="digest-meta">' +
'<span class="digest-meta-item"><i data-lucide="user"></i>' + esc(d.author_name || '—') + '</span>' +
'<span class="digest-meta-item"><i data-lucide="calendar"></i>' + esc(fmtDateTimeY(d.published_at || d.created_at)) + '</span>' +
'<div class="digest-main">' +
cover +
'<div class="digest-content">' + bodyHtml + galleryHtml + '</div>' +
'</div>' +
bodyHtml +
imagesHtml +
'<div class="digest-foot">' +
'<div class="digest-actions">' + actions.join('') + '</div>' +
'</div>' +
'</div>';
footHtml +
'</article>';
}
function filterDigests() {
@@ -200,6 +216,7 @@
el.innerHTML = '<div class="digest-empty"><i data-lucide="megaphone"></i>' +
'<p>Сообщений пока нет</p>' +
'<p class="sub">Опубликуйте важное объявление — его увидят все тьюторы и админы.</p>' +
(isAdmin() ? '<button type="button" class="btn-primary" data-action="new"><i data-lucide="plus"></i>Создать сообщение</button>' : '') +
'</div>';
renderIcons();
return;
@@ -679,11 +696,14 @@
const body = card.querySelector('.digest-body');
if (body) {
const clamped = body.classList.toggle('clamp');
actEl.textContent = clamped ? 'Показать полностью' : 'Свернуть';
actEl.classList.toggle('open', !clamped);
const label = actEl.querySelector('.digest-toggle-label');
if (label) label.textContent = clamped ? 'Читать полностью' : 'Свернуть';
}
}
return;
}
if (action === 'new') { openDigestModal(null); return; }
if (action === 'reload') { await loadDigests(); return; }
return;
}