- Updated CSS styles in report.css for improved readability and consistency. - Removed deprecated report_class.html and report_group.html files. - Introduced new Jinja2 templates: report_class.jinja and report_group.jinja for better maintainability. - Enhanced JavaScript functionality in report_class.js to support dynamic chart rendering using Chart.js. - Updated report_group.js to streamline print functionality.
169 lines
4.4 KiB
JavaScript
169 lines
4.4 KiB
JavaScript
(function () {
|
|
const chartStore = new Map();
|
|
|
|
function qs(selector, root) {
|
|
return (root || document).querySelector(selector);
|
|
}
|
|
|
|
function qsa(selector, root) {
|
|
return Array.from((root || document).querySelectorAll(selector));
|
|
}
|
|
|
|
function buildChartForClass(classId, report) {
|
|
if (!classId || chartStore.has(classId) || typeof Chart === "undefined") {
|
|
return;
|
|
}
|
|
|
|
const sections = Array.isArray(report.sections) ? report.sections : [];
|
|
const entry = sections
|
|
.flatMap((section) => section.entries || [])
|
|
.find((item) => item.id === classId);
|
|
if (!entry) {
|
|
return;
|
|
}
|
|
|
|
const canvas = qs(`.week-chart[data-chart-for="${classId}"]`);
|
|
if (!canvas) {
|
|
return;
|
|
}
|
|
|
|
const chartData = entry.chart_data || {};
|
|
const labels = Array.isArray(chartData.labels) ? chartData.labels : [];
|
|
const missed = Array.isArray(chartData.missed) ? chartData.missed : [];
|
|
const other = Array.isArray(chartData.other) ? chartData.other : [];
|
|
const conducted = Array.isArray(chartData.conducted)
|
|
? chartData.conducted
|
|
: [];
|
|
|
|
if (!labels.length) {
|
|
const container = canvas.parentElement;
|
|
if (container) {
|
|
container.innerHTML = "<div class='muted'>Нет данных.</div>";
|
|
}
|
|
return;
|
|
}
|
|
|
|
const chart = new Chart(canvas, {
|
|
type: "bar",
|
|
data: {
|
|
labels,
|
|
datasets: [
|
|
{
|
|
label: "Пропущено",
|
|
data: missed,
|
|
backgroundColor: "#ef4444",
|
|
stack: "pairs",
|
|
borderRadius: 4,
|
|
},
|
|
{
|
|
label: "Иное",
|
|
data: other,
|
|
backgroundColor: "#f59e0b",
|
|
stack: "pairs",
|
|
borderRadius: 4,
|
|
},
|
|
{
|
|
label: "Проведено",
|
|
data: conducted,
|
|
backgroundColor: "#22c55e",
|
|
stack: "pairs",
|
|
borderRadius: 4,
|
|
},
|
|
],
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
interaction: {
|
|
mode: "index",
|
|
intersect: false,
|
|
},
|
|
scales: {
|
|
x: {
|
|
stacked: true,
|
|
grid: {
|
|
display: false,
|
|
},
|
|
},
|
|
y: {
|
|
stacked: true,
|
|
beginAtZero: true,
|
|
ticks: {
|
|
precision: 0,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
chartStore.set(classId, chart);
|
|
}
|
|
|
|
function setActiveSection(sectionId, report) {
|
|
qsa(".section-nav .nav-btn[data-section]").forEach((btn) => {
|
|
btn.classList.toggle("active", btn.dataset.section === sectionId);
|
|
});
|
|
|
|
qsa(".class-nav-group").forEach((group) => {
|
|
group.classList.toggle("active", group.dataset.section === sectionId);
|
|
});
|
|
|
|
const firstButton = qs(`.class-nav-group.active .class-btn[data-class-id]`);
|
|
if (firstButton) {
|
|
setActiveClass(firstButton.dataset.classId, false, report);
|
|
}
|
|
}
|
|
|
|
function setActiveClass(classId, scrollIntoView, report) {
|
|
qsa(".class-btn[data-class-id]").forEach((btn) => {
|
|
btn.classList.toggle("active", btn.dataset.classId === classId);
|
|
});
|
|
|
|
qsa(".class-block[data-class-id]").forEach((block) => {
|
|
const active = block.dataset.classId === classId;
|
|
block.classList.toggle("active", active);
|
|
if (active && scrollIntoView) {
|
|
block.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
}
|
|
});
|
|
|
|
buildChartForClass(classId, report);
|
|
}
|
|
|
|
function init() {
|
|
const report = window.__CLASS_REPORT__ || {};
|
|
const activeSection = report.active_section || "";
|
|
const activeClass = report.active_class || "";
|
|
|
|
qsa(".section-nav .nav-btn[data-section]").forEach((btn) => {
|
|
btn.addEventListener("click", () =>
|
|
setActiveSection(btn.dataset.section, report),
|
|
);
|
|
});
|
|
|
|
qsa(".class-btn[data-class-id]").forEach((btn) => {
|
|
btn.addEventListener("click", () =>
|
|
setActiveClass(btn.dataset.classId, true, report),
|
|
);
|
|
});
|
|
|
|
if (activeSection) {
|
|
setActiveSection(activeSection, report);
|
|
}
|
|
|
|
if (activeClass) {
|
|
setActiveClass(activeClass, false, report);
|
|
}
|
|
}
|
|
|
|
window.printReport = function printReport() {
|
|
window.print();
|
|
};
|
|
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|