-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressLine.js
More file actions
148 lines (117 loc) · 4.57 KB
/
progressLine.js
File metadata and controls
148 lines (117 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { stages } from "./constants";
import confetti from "canvas-confetti";
const FINISH = 17;
export function getRunningTimes() {
const times = {};
const rows = document.querySelectorAll("table.table-condensed tbody tr");
rows.forEach((row) => {
const desc = row.querySelector("th.desc")?.textContent?.trim();
const time = row.querySelector("td")?.textContent?.trim();
if (desc?.startsWith("Running") && time && time !== "–") {
times[desc] = time;
}
});
return times;
}
export function createProgressLine(currentStageIndex, runningTimes = {}) {
const container = document.createElement("div");
container.className = "hyrox-timeline-container";
const timeline = document.createElement("ul");
timeline.className = "hyrox-timeline";
stages.forEach((stage, index) => {
const item = document.createElement("li");
// Circle element
const circle = document.createElement("div");
circle.className = "circle";
const startOrEnd = index === 0 || index === FINISH;
circle.textContent = startOrEnd ? "" : index;
// Label element
const label = document.createElement("span");
label.className = "label";
label.textContent = stage;
const hasFinished = index === FINISH && currentStageIndex === FINISH;
if (index < currentStageIndex || hasFinished) {
item.classList.add("active-tl");
circle.style.color = "white";
} else {
circle.style.color = "black";
}
if (stage.toLowerCase().includes("running")) {
const time = runningTimes[stage];
if (time) {
const tooltip = document.createElement("div");
tooltip.className = "station-tooltip";
tooltip.textContent = `${time}`;
item.appendChild(tooltip);
circle.addEventListener("mouseenter", () => {
tooltip.style.display = "block";
});
circle.addEventListener("mousemove", (e) => {
const rect = circle.getBoundingClientRect();
tooltip.style.top = `${e.clientY - rect.top + 20}px`;
tooltip.style.left = `${e.clientX - rect.left + 20}px`;
});
circle.addEventListener("mouseleave", () => {
tooltip.style.display = "none";
});
}
}
if (hasFinished) {
const confettiBtn = document.createElement("span");
confettiBtn.textContent = "🎉";
confettiBtn.style.cursor = "pointer";
confettiBtn.title = "Celebrate!";
Object.assign(confettiBtn.style, {
fontSize: "1.9rem",
display: "inline-block",
});
confettiBtn.onclick = () => launchConfetti();
circle.appendChild(confettiBtn);
// Wiggle animation
setInterval(() => {
confettiBtn.classList.add("wiggle");
setTimeout(() => confettiBtn.classList.remove("wiggle"), 2000);
}, 4000);
}
item.appendChild(circle);
item.appendChild(label);
timeline.appendChild(item);
});
container.appendChild(timeline);
return container;
}
function launchConfetti() {
if (typeof confetti !== "function") return;
confetti({
particleCount: 150,
spread: 70,
colors: ["#FFFF00", "#000000", "#FFFFFF"],
origin: { y: 0.7 },
});
}
export function animateWave(currentIndex) {
const timelineItems = document.querySelectorAll(".hyrox-timeline li");
function runWave() {
timelineItems.forEach((item, index) => {
item.classList.remove("wave-animated", "fading-out");
void item.offsetWidth;
if (
index < currentIndex ||
(index === FINISH && currentIndex === FINISH)
) {
setTimeout(() => {
item.classList.add("wave-animated");
setTimeout(() => {
item.classList.remove("wave-animated");
item.classList.add("fading-out");
setTimeout(() => {
item.classList.remove("fading-out");
}, 1200); // match fade out duration
}, 1000); // match active duration
}, index * 600);
}
});
}
runWave();
setInterval(runWave, (currentIndex + 1) * 600 + 3000);
}