feat(journal): превью последних вложений и фото с занятий в карточке отчёта
- эндпоинты /api/students/image-files-by-name и /api/students/works-by-name (requireAuth, branchScope) - галерея вложений карточки грузится асинхронно по имени ученика, кэш по имени - новый тумблер «Фото на занятиях» (localStorage reportCardStudentWorks) - выравнивание высоты галереи, обработка landscape-фото, CSS .sc-w__* - shorts: правка выравнивания автора в строке
This commit is contained in:
+162
-15
@@ -67,9 +67,14 @@ const REPORT_CARD_FIELDS_KEY = 'reportCardFields';
|
||||
const reportCardFields = loadReportCardFields();
|
||||
const REPORT_CARD_EMBED_KEY = 'reportCardEmbedImages';
|
||||
let reportCardEmbed = loadReportCardEmbed();
|
||||
const REPORT_CARD_STUDENT_WORKS_KEY = 'reportCardStudentWorks';
|
||||
let reportCardStudentWorks = loadReportCardStudentWorks();
|
||||
let reportCardEntryId = null;
|
||||
let galleryList = [];
|
||||
let galleryIndex = -1;
|
||||
const studentWorksCache = new Map();
|
||||
const studentImagesCache = new Map();
|
||||
let reportCardEmbedImages = [];
|
||||
|
||||
function loadReportCardEmbed() {
|
||||
try {
|
||||
@@ -87,13 +92,24 @@ function saveReportCardEmbed() {
|
||||
}
|
||||
}
|
||||
|
||||
function isEmbeddableImage(name) {
|
||||
return /\.(jpe?g|jfif|png|gif|webp|bmp|avif)$/i.test(name || '');
|
||||
function loadReportCardStudentWorks() {
|
||||
try {
|
||||
return localStorage.getItem(REPORT_CARD_STUDENT_WORKS_KEY) === '1';
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function reportCardImages(e) {
|
||||
if (!reportCardEmbed || !e.files) return [];
|
||||
return e.files.filter(f => isEmbeddableImage(f.name));
|
||||
function saveReportCardStudentWorks() {
|
||||
try {
|
||||
localStorage.setItem(REPORT_CARD_STUDENT_WORKS_KEY, reportCardStudentWorks ? '1' : '0');
|
||||
} catch (err) {
|
||||
showToast('Не удалось сохранить настройку «Фото на занятиях»', true);
|
||||
}
|
||||
}
|
||||
|
||||
function reportCardEmbedTokens() {
|
||||
return new Set(reportCardEmbedImages.map(f => f.token));
|
||||
}
|
||||
|
||||
function loadReportCardFields() {
|
||||
@@ -127,9 +143,10 @@ function splitStudentName(full) {
|
||||
}
|
||||
|
||||
function reportCardFilesHTML(e) {
|
||||
const files = (e.files || []).filter(f => !(reportCardEmbed && isEmbeddableImage(f.name)));
|
||||
const hidden = reportCardEmbed ? reportCardEmbedTokens() : null;
|
||||
const files = (e.files || []).filter(f => !(hidden && hidden.has(f.token)));
|
||||
if (!files.length) return '';
|
||||
return `<div class="student-card__meta-files">${files.map(f => {
|
||||
return `<div class="student-card__meta-files" id="reportCardFiles">${files.map(f => {
|
||||
const url = `${API}/api/files/${f.token}`;
|
||||
if (isImageFile(f.name)) return `<a href="#" data-img="${url}" title="Открыть">${esc(f.name)}</a>`;
|
||||
if (isPlayableVideoFile(f.name)) return `<a href="${url}?play=1" data-video="${url}?play=1" data-video-name="${esc(f.name)}" data-video-token="${f.token}" title="Смотреть">${esc(f.name)}</a>`;
|
||||
@@ -138,17 +155,52 @@ function reportCardFilesHTML(e) {
|
||||
}).join('')}</div>`;
|
||||
}
|
||||
|
||||
function reportCardGalleryHTML(e) {
|
||||
const imgs = reportCardImages(e);
|
||||
if (!imgs.length) return '';
|
||||
return `<div class="student-card__gallery-grid">${imgs.map((f, i) => {
|
||||
function reportCardGalleryHTML() {
|
||||
if (!reportCardEmbed) return '';
|
||||
return '<div class="student-card__gallery-grid is-loading" id="reportCardEmbedGallery"></div>';
|
||||
}
|
||||
|
||||
function paintReportCardEmbedGallery(box, images) {
|
||||
if (!box) return;
|
||||
box.classList.remove('is-loading');
|
||||
if (!images.length) { box.remove(); galleryList = []; return; }
|
||||
galleryList = images.map(f => `${API}/api/files/${f.token}`);
|
||||
box.innerHTML = images.map((f, i) => {
|
||||
const url = `${API}/api/files/${f.token}`;
|
||||
return `<button type="button" class="sc-gal__shot" data-img="${url}" data-gal-index="${i}" title="${esc(f.name)}" aria-label="${esc(f.name)}">
|
||||
<img class="sc-gal__img" src="${thumbSrc(url)}" alt="${esc(f.name)}" loading="lazy" decoding="async">
|
||||
<span class="sc-gal__spin" aria-hidden="true"></span>
|
||||
<span class="sc-gal__zoom" aria-hidden="true"><i data-lucide="maximize-2"></i></span>
|
||||
</button>`;
|
||||
}).join('')}</div>`;
|
||||
}).join('');
|
||||
settleReportGallery(box);
|
||||
equalizeReportGallery(box);
|
||||
}
|
||||
|
||||
function equalizeReportGallery(root) {
|
||||
if (!root) return;
|
||||
const imgs = Array.from(root.querySelectorAll('.sc-gal__img'));
|
||||
if (!imgs.length) return;
|
||||
let pending = imgs.length;
|
||||
const apply = () => {
|
||||
requestAnimationFrame(() => {
|
||||
let maxH = 0;
|
||||
imgs.forEach(img => {
|
||||
const h = img.getBoundingClientRect().height;
|
||||
if (h > maxH) maxH = h;
|
||||
});
|
||||
if (maxH > 0) {
|
||||
root.style.setProperty('--gal-h', `${Math.round(maxH)}px`);
|
||||
root.classList.add('is-equal');
|
||||
}
|
||||
});
|
||||
};
|
||||
const done = () => { pending -= 1; if (pending <= 0) apply(); };
|
||||
imgs.forEach(img => {
|
||||
if (img.complete && img.naturalWidth > 0) { done(); return; }
|
||||
img.addEventListener('load', done, { once: true });
|
||||
img.addEventListener('error', done, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function settleReportGallery(root) {
|
||||
@@ -157,6 +209,7 @@ function settleReportGallery(root) {
|
||||
const shot = img.closest('.sc-gal__shot');
|
||||
const ready = () => {
|
||||
img.classList.add('is-loaded');
|
||||
if (img.naturalWidth > 0 && img.naturalHeight > 0 && img.naturalWidth > img.naturalHeight) img.classList.add('is-landscape');
|
||||
if (shot) shot.classList.add('is-ready');
|
||||
};
|
||||
const failed = () => {
|
||||
@@ -172,13 +225,82 @@ function settleReportGallery(root) {
|
||||
});
|
||||
}
|
||||
|
||||
async function hydrateReportCardEmbedGallery(e) {
|
||||
const box = document.getElementById('reportCardEmbedGallery');
|
||||
if (!box) { reportCardEmbedImages = []; galleryList = []; return; }
|
||||
const name = String(e.student_name || '').trim();
|
||||
if (!name) { reportCardEmbedImages = []; box.remove(); galleryList = []; return; }
|
||||
let images = studentImagesCache.get(name);
|
||||
if (!images) {
|
||||
box.innerHTML = '<span class="sc-w__spin" aria-hidden="true"></span>';
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/image-files-by-name?name=${encodeURIComponent(name)}&limit=3`, { headers: hdr() });
|
||||
const data = await res.json();
|
||||
images = (data.files || []).filter(f => f && f.token);
|
||||
studentImagesCache.set(name, images);
|
||||
} catch (err) {
|
||||
if (reportCardEntryId === e.id) {
|
||||
const cur = document.getElementById('reportCardEmbedGallery');
|
||||
if (cur) cur.remove();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (reportCardEntryId !== e.id) return;
|
||||
reportCardEmbedImages = images;
|
||||
paintReportCardEmbedGallery(box, images);
|
||||
refreshReportCardFiles(e);
|
||||
renderIcons();
|
||||
}
|
||||
|
||||
function refreshReportCardFiles(e) {
|
||||
const el = document.getElementById('reportCardFiles');
|
||||
const html = reportCardFilesHTML(e);
|
||||
if (!html) { if (el) el.remove(); return; }
|
||||
if (el) el.outerHTML = html;
|
||||
}
|
||||
|
||||
function paintReportCardStudentWorks(box, photos, entryId) {
|
||||
if (!box || reportCardEntryId !== entryId) return;
|
||||
if (!photos.length) { box.remove(); return; }
|
||||
box.classList.remove('is-loading');
|
||||
box.innerHTML = photos.map(p => `<button type="button" class="sc-w__shot" data-img="${API}${p}" title="Открыть фотографию" aria-label="Открыть фотографию">
|
||||
<img class="sc-w__img" src="${thumbSrc(p)}" alt="" loading="lazy" decoding="async">
|
||||
</button>`).join('');
|
||||
}
|
||||
|
||||
async function hydrateReportCardStudentWorks(e) {
|
||||
const box = document.getElementById('reportCardStudentWorks');
|
||||
if (!box) return;
|
||||
const name = String(e.student_name || '').trim();
|
||||
if (!name) { box.remove(); return; }
|
||||
const cached = studentWorksCache.get(name);
|
||||
if (cached) { paintReportCardStudentWorks(box, cached, e.id); return; }
|
||||
box.classList.add('is-loading');
|
||||
box.innerHTML = '<span class="sc-w__spin" aria-hidden="true"></span>';
|
||||
try {
|
||||
const res = await fetch(`${API}/api/students/works-by-name?name=${encodeURIComponent(name)}&limit=3`, { headers: hdr() });
|
||||
const data = await res.json();
|
||||
const photos = (data.photos || []).filter(Boolean);
|
||||
studentWorksCache.set(name, photos);
|
||||
if (reportCardEntryId !== e.id) return;
|
||||
paintReportCardStudentWorks(document.getElementById('reportCardStudentWorks'), photos, e.id);
|
||||
} catch (err) {
|
||||
if (reportCardEntryId === e.id) {
|
||||
const cur = document.getElementById('reportCardStudentWorks');
|
||||
if (cur) cur.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function reportCardMetaHTML(e) {
|
||||
let html = '';
|
||||
if (reportCardFieldOn('date') && e.created_at) {
|
||||
html += `<div class="student-card__meta-val">${esc(fmtFull(e.created_at))}</div>`;
|
||||
}
|
||||
if (reportCardFieldOn('files')) html += reportCardFilesHTML(e);
|
||||
html += reportCardGalleryHTML(e);
|
||||
html += reportCardGalleryHTML();
|
||||
if (reportCardStudentWorks && e.student_name) html += '<div class="student-card__student-works" id="reportCardStudentWorks"></div>';
|
||||
return html;
|
||||
}
|
||||
|
||||
@@ -226,9 +348,12 @@ function renderReportCard(e) {
|
||||
document.getElementById('reportCardModuleIcon').textContent = moduleIconText(tool);
|
||||
toolBadge.hidden = !reportCardFieldOn('module') || !tool;
|
||||
const meta = document.getElementById('reportCardMeta');
|
||||
reportCardEmbedImages = [];
|
||||
meta.innerHTML = reportCardMetaHTML(e);
|
||||
galleryList = reportCardImages(e).map(f => `${API}/api/files/${f.token}`);
|
||||
galleryList = [];
|
||||
settleReportGallery(meta);
|
||||
hydrateReportCardEmbedGallery(e);
|
||||
hydrateReportCardStudentWorks(e);
|
||||
renderIcons();
|
||||
}
|
||||
|
||||
@@ -1964,7 +2089,7 @@ const reportFieldsBtn = document.getElementById('reportFieldsBtn');
|
||||
const reportFieldsPanel = document.getElementById('reportFieldsPanel');
|
||||
const reportEmbedToggle = document.getElementById('reportEmbedToggle');
|
||||
const reportEmbedHint = document.getElementById('reportEmbedHint');
|
||||
const REPORT_EMBED_HINT = 'Показывать превью картинок из вложений прямо в карточке';
|
||||
const REPORT_EMBED_HINT = 'Показывать превью последних картинок-вложений ученика за всё время';
|
||||
|
||||
function renderReportEmbedToggle() {
|
||||
if (!reportEmbedToggle) return;
|
||||
@@ -1983,6 +2108,27 @@ if (reportEmbedToggle) {
|
||||
reportEmbedToggle.addEventListener('change', e => setReportCardEmbed(e.target.checked));
|
||||
}
|
||||
|
||||
const reportStudentWorksToggle = document.getElementById('reportStudentWorksToggle');
|
||||
const reportStudentWorksHint = document.getElementById('reportStudentWorksHint');
|
||||
const REPORT_STUDENT_WORKS_HINT = 'Показывать последние фото с занятий ученика — до 3 в строку';
|
||||
|
||||
function renderReportStudentWorksToggle() {
|
||||
if (!reportStudentWorksToggle) return;
|
||||
reportStudentWorksToggle.checked = reportCardStudentWorks;
|
||||
if (reportStudentWorksHint) reportStudentWorksHint.textContent = REPORT_STUDENT_WORKS_HINT;
|
||||
}
|
||||
|
||||
function setReportCardStudentWorks(on) {
|
||||
reportCardStudentWorks = !!on;
|
||||
saveReportCardStudentWorks();
|
||||
renderReportStudentWorksToggle();
|
||||
refreshReportCard();
|
||||
}
|
||||
|
||||
if (reportStudentWorksToggle) {
|
||||
reportStudentWorksToggle.addEventListener('change', e => setReportCardStudentWorks(e.target.checked));
|
||||
}
|
||||
|
||||
function setReportFieldsOpen(open) {
|
||||
reportFieldsRoot.classList.toggle('open', open);
|
||||
reportFieldsBtn.setAttribute('aria-expanded', open ? 'true' : 'false');
|
||||
@@ -1990,6 +2136,7 @@ function setReportFieldsOpen(open) {
|
||||
|
||||
renderReportFieldsList();
|
||||
renderReportEmbedToggle();
|
||||
renderReportStudentWorksToggle();
|
||||
reportFieldsBtn.addEventListener('click', () => setReportFieldsOpen(!reportFieldsRoot.classList.contains('open')));
|
||||
reportFieldsPanel.addEventListener('change', e => {
|
||||
const cb = e.target.closest('[data-report-field]');
|
||||
|
||||
Reference in New Issue
Block a user