History in widget

This commit is contained in:
Adam Štrauch 2025-10-06 14:39:18 +02:00
parent 3a2b78fcce
commit 84fe2d5946
Signed by: cx
GPG key ID: 7262DAFE292BCE20

View file

@ -47,6 +47,13 @@
return `${year}-${month}-${day}`;
}
function getHistoryDays() {
const urlParams = new URLSearchParams(window.location.search);
const history = parseInt(urlParams.get('history')) || 14;
// Limit history to maximum 365 days
return Math.min(Math.max(1, history), 365);
}
function getHourFromTimeString(timeStr) {
return parseInt(timeStr.split(':')[0]);
}
@ -74,6 +81,7 @@
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
const tomorrowString = formatDate(tomorrow);
const historyDays = getHistoryDays();
// Check if tomorrow's data is available
let tomorrowAvailable = false;
@ -85,18 +93,36 @@
}
} catch (e) {}
let rowCount = tomorrowAvailable ? 15 : 14;
// Calculate total rows: history days + today + tomorrow (if available)
let rowCount = historyDays + 1; // history days + today
if (tomorrowAvailable) {
rowCount += 1; // + tomorrow
}
for (let offset = 0; offset < rowCount; offset++) {
let date;
let label;
if (tomorrowAvailable && offset === 0) {
// Tomorrow is first row if available
date = tomorrow;
label = `Zítra (${tomorrowString})`;
} else if (tomorrowAvailable && offset === 1) {
// Today is second row if tomorrow is available
date = today;
const todayString = formatDate(today);
label = `Dnes (${todayString})`;
} else if (!tomorrowAvailable && offset === 0) {
// Today is first row if tomorrow is not available
date = today;
const todayString = formatDate(today);
label = `Dnes (${todayString})`;
} else {
// Historical days
date = new Date(today);
date.setDate(today.getDate() - (tomorrowAvailable ? offset - 1 : offset));
const daysBack = tomorrowAvailable ? offset - 1 : offset;
date.setDate(today.getDate() - daysBack);
const dateString = formatDate(date);
label = offset === (tomorrowAvailable ? 1 : 0) ? `Dnes (${dateString})` : dateString;
label = dateString;
}
const dateString = formatDate(date);
@ -287,18 +313,42 @@
}
} catch (e) {}
let rowCount = tomorrowAvailable ? 15 : 14;
const historyDays = getHistoryDays();
// Calculate total rows: history days + today + tomorrow (if available)
let rowCount = historyDays + 1; // history days + today
if (tomorrowAvailable) {
rowCount += 1; // + tomorrow
}
for (let offset = 0; offset < rowCount; offset++) {
let date;
let isToday = false;
if (tomorrowAvailable && offset === 0) {
// Tomorrow is first row if available
date = tomorrow;
} else if (tomorrowAvailable && offset === 1) {
// Today is second row if tomorrow is available
date = today;
isToday = true;
} else if (!tomorrowAvailable && offset === 0) {
// Today is first row if tomorrow is not available
date = today;
isToday = true;
} else {
// Historical days
date = new Date(today);
date.setDate(today.getDate() - (tomorrowAvailable ? offset - 1 : offset));
const daysBack = tomorrowAvailable ? offset - 1 : offset;
date.setDate(today.getDate() - daysBack);
}
const dateString = formatDate(date);
loadData(dateString, (tomorrowAvailable ? offset === 1 : offset === 0));
loadData(dateString, isToday);
}
// Update the chart after loading table data
await updateChart();
}
window.onload = function() {