Files
datefinder/static/result.html.tmpl
T

196 lines
5.5 KiB
Cheetah

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Datefinder</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.calendar {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 10px;
margin: 20px auto;
width: 90%;
}
.day-column {
border: 1px solid #ccc;
background-color: #f9f9f9;
padding: 5px;
}
.day-header {
font-weight: bold;
margin-bottom: 5px;
}
.hour-block {
border: 1px solid #ddd;
padding: 10px;
margin: 2px 0;
background-color: #fff;
text-align: center;
position: relative;
cursor: pointer;
}
.hour-block:hover {
border-color: #87CEEB;
}
.hour-block.clicked {
background-color: #87CEEB;
}
.tooltip {
position: absolute;
padding: 5px;
background-color: black;
color: white;
border-radius: 4px;
font-size: 12px;
visibility: hidden;
white-space: nowrap;
z-index: 1000;
transform: translate(-50%, -150%);
}
.hour-block:hover .tooltip {
visibility: visible;
}
@media (max-width: 768px) {
.calendar {
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.hour-block {
font-size: 12px;
padding: 8px;
}
.tooltip {
font-size: 10px;
}
}
@media (max-width: 480px) {
.calendar {
grid-template-columns: repeat(2, 1fr);
gap: 5px;
}
.day-column {
padding: 5px;
}
.hour-block {
font-size: 10px;
padding: 5px;
}
.tooltip {
font-size: 9px;
}
}
</style>
</head>
<body>
<h1>Datefinder</h1>
<h2>{{ .PollName }}</h2>
<div class="calendar" id="calendar"></div>
<script>
function getColorBetween(upperColor, num, upperBound) {
const parseColor = (color) => {
const bigint = parseInt(color.slice(1), 16);
return {
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255,
};
};
const upperRGB = parseColor(upperColor);
const whiteRGB = { r: 255, g: 255, b: 255 };
const clampedNum = Math.max(0, Math.min(num, upperBound));
const weight = clampedNum / upperBound;
const interpolatedRGB = {
r: Math.round(whiteRGB.r + weight * (upperRGB.r - whiteRGB.r)),
g: Math.round(whiteRGB.g + weight * (upperRGB.g - whiteRGB.g)),
b: Math.round(whiteRGB.b + weight * (upperRGB.b - whiteRGB.b)),
};
const toHex = (val) => val.toString(16).padStart(2, "0");
return `#${toHex(interpolatedRGB.r)}${toHex(interpolatedRGB.g)}${toHex(interpolatedRGB.b)}`;
}
function createResultView(result) {
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
const calendar = document.getElementById("calendar");
const maxVotes = result.votes.flat().reduce((max, obj) => Math.max(max, obj.votes), 0);
for (let day = 0; day < 7; day++) {
const dayColumn = document.createElement("div");
dayColumn.classList.add("day-column");
const dayHeader = document.createElement("div");
dayHeader.classList.add("day-header");
dayHeader.textContent = daysOfWeek[day];
dayColumn.appendChild(dayHeader);
for (let hour = 0; hour < 24; hour++) {
const hourBlock = document.createElement("div");
hourBlock.classList.add("hour-block");
hourBlock.textContent = `${hour}:00`;
const tooltip = document.createElement("div");
tooltip.classList.add("tooltip");
tooltip.textContent = result.votes[day][hour].usernames.join(", ");
hourBlock.appendChild(tooltip);
hourBlock.style.backgroundColor = getColorBetween(
"#87CEEB",
result.votes[day][hour].votes,
maxVotes
);
dayColumn.appendChild(hourBlock);
}
calendar.appendChild(dayColumn);
}
}
const url = "/api/result/{{ .PollId }}";
fetch(url, { method: "GET" })
.then((response) => {
if (!response.ok) {
console.log(response);
}
return response.json();
})
.then((data) => {
createResultView(data);
})
.catch((error) => {
console.error("Fetch error:", error);
});
</script>
</body>
</html>