Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -56,7 +56,7 @@
|
||||
borderRadius: 4,
|
||||
},
|
||||
{
|
||||
label: "Иное",
|
||||
label: "Не по расписанию",
|
||||
data: other,
|
||||
backgroundColor: "#f59e0b",
|
||||
stack: "pairs",
|
||||
@@ -74,6 +74,7 @@
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
animation: false,
|
||||
interaction: {
|
||||
mode: "index",
|
||||
intersect: false,
|
||||
@@ -156,8 +157,84 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Print support: convert canvases to static images ----
|
||||
function prepareForPrint() {
|
||||
var report = window.__CLASS_REPORT__ || {};
|
||||
var sections = Array.isArray(report.sections) ? report.sections : [];
|
||||
var allEntries = sections.flatMap(function (s) {
|
||||
return s.entries || [];
|
||||
});
|
||||
|
||||
// Activate each class one-by-one, build its chart, capture the image,
|
||||
// then move on. This ensures Chart.js renders each canvas properly.
|
||||
var idx = 0;
|
||||
var storedImages = {};
|
||||
|
||||
function processNext() {
|
||||
if (idx >= allEntries.length) {
|
||||
// All done — replace canvases with stored images
|
||||
Object.keys(storedImages).forEach(function (classId) {
|
||||
var canvas = qs('.week-chart[data-chart-for="' + classId + '"]');
|
||||
if (!canvas) return;
|
||||
var img = document.createElement("img");
|
||||
img.src = storedImages[classId];
|
||||
img.alt = "Chart";
|
||||
img.style.width = "100%";
|
||||
img.style.height = "320px";
|
||||
img.style.objectFit = "contain";
|
||||
canvas.parentNode.replaceChild(img, canvas);
|
||||
});
|
||||
// Restore original class
|
||||
if (originalClassId) {
|
||||
setActiveClass(originalClassId, false, report);
|
||||
}
|
||||
// Short delay then open print dialog
|
||||
setTimeout(function () {
|
||||
window.print();
|
||||
}, 50);
|
||||
return;
|
||||
}
|
||||
|
||||
var entry = allEntries[idx];
|
||||
var cid = entry.id;
|
||||
if (!cid) {
|
||||
idx++;
|
||||
processNext();
|
||||
return;
|
||||
}
|
||||
|
||||
// Switch to this class to make its canvas visible
|
||||
setActiveClass(cid, false, report);
|
||||
|
||||
// Wait for layout + Chart.js to render
|
||||
setTimeout(function () {
|
||||
// Build chart if not already built
|
||||
buildChartForClass(cid, report);
|
||||
// Wait again for chart animation to finish
|
||||
setTimeout(function () {
|
||||
var chart = chartStore.get(cid);
|
||||
if (chart) {
|
||||
storedImages[cid] = chart.toBase64Image();
|
||||
}
|
||||
idx++;
|
||||
processNext();
|
||||
}, 100);
|
||||
}, 50);
|
||||
}
|
||||
|
||||
// Remember which class was active
|
||||
var originalClassId = null;
|
||||
var activeBlock = qs(".class-block.active");
|
||||
if (activeBlock) originalClassId = activeBlock.dataset.classId;
|
||||
|
||||
// Start sequential processing from first entry
|
||||
idx = 0;
|
||||
processNext();
|
||||
}
|
||||
|
||||
window.printReport = function printReport() {
|
||||
window.print();
|
||||
prepareForPrint();
|
||||
// print dialog opens inside prepareForPrint after all charts are processed
|
||||
};
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
|
||||
Reference in New Issue
Block a user