2 //= require cal-heatmap/dist/cal-heatmap
4 //= require cal-heatmap/dist/plugins/Tooltip
6 /* global CalHeatmap, Tooltip */
7 document.addEventListener("DOMContentLoaded", () => {
8 const heatmapElement = document.querySelector("#cal-heatmap");
10 if (!heatmapElement) {
14 const heatmapData = heatmapElement.dataset.heatmap ? JSON.parse(heatmapElement.dataset.heatmap) : [];
15 const displayName = heatmapElement.dataset.displayName;
16 const colorScheme = document.documentElement.getAttribute("data-bs-theme") ?? "auto";
17 const rangeColors = ["#14432a", "#166b34", "#37a446", "#4dd05a"];
18 const startDate = new Date(Date.now() - (365 * 24 * 60 * 60 * 1000));
19 const monthNames = I18n.t("date.abbr_month_names");
21 const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
23 let cal = new CalHeatmap();
24 let currentTheme = getTheme();
26 function renderHeatmap() {
28 cal = new CalHeatmap();
31 itemSelector: "#cal-heatmap",
37 text: (timestamp) => monthNames[new Date(timestamp).getMonth() + 1],
41 dynamicDimension: true
63 range: currentTheme === "dark" ? rangeColors : Array.from(rangeColors).reverse(),
64 domain: [10, 20, 30, 40]
69 text: (date, value) => getTooltipText(date, value)
73 cal.on("click", (_event, timestamp) => {
74 if (!displayName) return;
75 for (const { date, max_id } of heatmapData) {
76 if (!max_id) continue;
77 if (timestamp !== Date.parse(date)) continue;
78 const params = new URLSearchParams([["before", max_id + 1]]);
79 location = `/user/${encodeURIComponent(displayName)}/history?${params}`;
84 function getTooltipText(date, value) {
85 const localizedDate = I18n.l("date.formats.long", date);
88 return I18n.t("javascripts.heatmap.tooltip.contributions", { count: value, date: localizedDate });
91 return I18n.t("javascripts.heatmap.tooltip.no_contributions", { date: localizedDate });
95 if (colorScheme === "auto") {
96 return mediaQuery.matches ? "dark" : "light";
102 if (colorScheme === "auto") {
103 mediaQuery.addEventListener("change", (e) => {
104 currentTheme = e.matches ? "dark" : "light";