chessbaseLinux/website/static/js/site.js
vrubelroman 265d9d794b
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run
Website: one page in six languages, downloads via the server, download statistics
Rust server (axum) with server-side translations, OS-aware download buttons,
/download/<platform> redirecting to the latest GitHub release asset and logging
platform, version and geolocated region (no IP stored), password-protected /stats
and /stats.csv. Docker compose setup behind nginx. Ten screenshots taken with new
dev hooks (CP_FILTER, CP_PANE, CP_SORT, CP_BOARD).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 11:00:10 +03:00

315 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ChessPenguin site: the download button for the visitor's system, the panel after a
// download starts, download tabs, copy buttons, language choice and the screenshot
// lightbox. Nothing is sent anywhere: downloads are counted by the server.
(function () {
"use strict";
var CP = window.CP || { t: {}, assets: {}, shots: [] };
var t = function (k) { return (CP.t && CP.t[k]) || k; };
var $ = function (id) { return document.getElementById(id); };
document.documentElement.classList.add("js");
// --- The visitor's system --------------------------------------------------------
function detect() {
var ua = navigator.userAgent || "";
var platform = (navigator.userAgentData && navigator.userAgentData.platform) || navigator.platform || "";
var touchMac = /Mac/.test(platform) && navigator.maxTouchPoints > 1;
if (/iPhone|iPod|iPad|Android/i.test(ua) || touchMac) return { os: "mobile" };
if (/CrOS/.test(ua)) return { os: "linux", slug: "deb", chromeos: true };
if (/Win/.test(platform) || /Windows/.test(ua)) return { os: "windows", slug: "windows" };
if (/Mac/.test(platform) || /Macintosh/.test(ua)) return { os: "macos", slug: "macos" };
if (/Linux|X11/.test(platform) || /Linux/.test(ua)) {
if (/Ubuntu|Debian|Mint/i.test(ua)) return { os: "linux", slug: "deb" };
if (/Fedora|openSUSE|SUSE|Red Hat/i.test(ua)) return { os: "linux", slug: "rpm" };
return { os: "linux", slug: "appimage" };
}
return { os: "unknown" };
}
var sys = detect();
var FORMAT = { windows: ".zip", deb: ".deb", rpm: ".rpm", appimage: "AppImage" };
function intelMac() {
try {
var gl = document.createElement("canvas").getContext("webgl");
var ext = gl && gl.getExtension("WEBGL_debug_renderer_info");
var r = ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : "";
return /Intel/i.test(r) && !/Apple/i.test(r);
} catch (e) { return false; }
}
// --- Hero button -----------------------------------------------------------------
(function hero() {
var btn = $("hero-btn"), l1 = $("hero-l1"), l2 = $("hero-l2");
if (!btn) return;
if (sys.os === "mobile") {
btn.hidden = true;
$("hero-links").hidden = true;
$("hero-mobile").hidden = false;
var share = $("share");
if (!navigator.share) share.textContent = t("copy_link");
share.addEventListener("click", function () {
var url = location.origin + CP.home;
if (navigator.share) { navigator.share({ url: url, title: t("meta_title") }).catch(function () {}); return; }
if (navigator.clipboard) navigator.clipboard.writeText(url).then(function () {
share.textContent = t("link_copied");
setTimeout(function () { share.textContent = t("copy_link"); }, 2000);
});
});
return;
}
if (!sys.slug) return;
var asset = CP.assets[sys.slug];
btn.href = "/download/" + sys.slug;
btn.dataset.platform = sys.slug;
l1.textContent = t(sys.os === "windows" ? "btn_windows" : sys.os === "macos" ? "btn_macos" : "btn_linux");
var parts = [CP.version ? t("btn_version").replace("{version}", CP.version) : t("btn_latest")];
if (asset) parts.push(asset.size);
parts.push(sys.os === "macos" ? t("apple_silicon") : FORMAT[sys.slug]);
if (sys.chromeos) parts.push(t("chromeos"));
l2.textContent = parts.join(" · ");
if (sys.os === "macos") {
var note = $("hero-note");
note.hidden = false;
if (intelMac()) { note.textContent = t("mac_intel_warning"); note.classList.add("warn"); }
else note.textContent = t("mac_requires");
}
if (sys.os === "linux") {
$("hero-links").hidden = true;
var formats = $("hero-formats");
formats.hidden = false;
var cur = formats.querySelector('[data-platform="' + sys.slug + '"]');
if (cur) {
var prev = cur.previousSibling;
cur.remove();
if (prev && prev.nodeType === 3) prev.textContent = prev.textContent.replace(/·\s*$/, "");
}
}
})();
// --- After a download starts -----------------------------------------------------
function el(tag, attrs, text) {
var e = document.createElement(tag);
for (var k in attrs || {}) e.setAttribute(k, attrs[k]);
if (text != null) e.textContent = text;
return e;
}
function cmd(text) {
var box = el("div", { class: "cmd" });
box.appendChild(el("code", null, text));
var b = el("button", { type: "button", class: "copy", "aria-label": t("copy") }, t("copy"));
box.appendChild(b);
return box;
}
var COMMANDS = {
deb: function (f) { return ["sudo apt install ~/Downloads/" + f]; },
rpm: function (f) { return ["sudo dnf install ~/Downloads/" + f]; },
arch: function (f) { return ["sudo pacman -U ~/Downloads/" + f]; },
appimage: function (f) { return ["chmod +x ~/Downloads/" + f, "~/Downloads/" + f]; },
flatpak: function (f) { return ["flatpak install --user ~/Downloads/" + f, "flatpak run io.github.vrubelroman.chessPenguin"]; },
};
function started(platform, panelId) {
if (platform === "checksums") return;
["started", "started2"].forEach(function (id) { var p = $(id); if (p) { p.hidden = true; p.textContent = ""; } });
var panel = $(panelId);
if (!panel) return;
var asset = CP.assets[platform] || {};
panel.appendChild(el("div", { class: "ok" }, "✓ " + t("started")));
if (asset.file) panel.appendChild(el("div", { class: "file" }, asset.file + " · " + asset.size));
var again = el("p", { class: "file" });
var a1 = el("a", { href: "/download/" + platform }, t("try_again"));
var a2 = el("a", { href: "#download" }, t("choose_another"));
again.appendChild(a1); again.appendChild(document.createTextNode(" · ")); again.appendChild(a2);
panel.appendChild(again);
if (platform === "windows") panel.appendChild(el("p", null, t("next_windows")));
else if (platform === "macos") {
panel.appendChild(el("h4", null, t("next_macos_title")));
var ol = el("ol");
ol.appendChild(el("li", null, t("mac_step1")));
ol.appendChild(el("li", null, t("mac_step2")));
panel.appendChild(ol);
var d = el("details");
d.appendChild(el("summary", null, t("next_macos15")));
var ol2 = el("ol");
["mac15_step1", "mac15_step2", "mac15_step3"].forEach(function (k) { ol2.appendChild(el("li", null, t(k))); });
d.appendChild(ol2);
panel.appendChild(d);
} else if (COMMANDS[platform] && asset.file) {
panel.appendChild(el("p", { class: "file" }, t("next_linux")));
COMMANDS[platform](asset.file).forEach(function (c) { panel.appendChild(cmd(c)); });
}
var x = el("button", { type: "button", class: "x", "aria-label": t("close") }, "×");
x.addEventListener("click", function () { panel.hidden = true; });
panel.appendChild(x);
panel.hidden = false;
}
document.addEventListener("click", function (e) {
var a = e.target.closest && e.target.closest('a[href^="/download/"]');
if (!a) return;
var platform = a.getAttribute("href").split("/")[2];
// The link is followed as usual (the server redirects to the file); the panel only
// tells what happens next.
started(platform, a.closest("#download") ? "started2" : "started");
});
// --- Copy buttons ----------------------------------------------------------------
if (!navigator.clipboard) document.querySelectorAll(".copy").forEach(function (b) { b.hidden = true; });
document.addEventListener("click", function (e) {
var b = e.target.closest && e.target.closest(".copy");
if (!b || !navigator.clipboard) return;
var code = b.parentNode.querySelector("code");
navigator.clipboard.writeText(code.textContent).then(function () {
b.textContent = t("copied"); b.classList.add("done");
setTimeout(function () { b.textContent = t("copy"); b.classList.remove("done"); }, 2000);
});
});
// --- Download tabs ---------------------------------------------------------------
var tabs = Array.prototype.slice.call(document.querySelectorAll('.tabs [role="tab"]'));
function select(tab, focus) {
tabs.forEach(function (x) {
var on = x === tab;
x.setAttribute("aria-selected", on ? "true" : "false");
x.tabIndex = on ? 0 : -1;
$(x.getAttribute("aria-controls")).hidden = !on;
});
if (focus) tab.focus();
}
tabs.forEach(function (tab, i) {
tab.addEventListener("click", function () { select(tab); });
tab.addEventListener("keydown", function (e) {
var j = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key];
if (j == null) return;
e.preventDefault();
select(tabs[(j + tabs.length) % tabs.length], true);
});
});
var mine = { windows: "tab-windows", macos: "tab-macos", linux: "tab-linux" }[sys.os];
if (mine) {
$(mine).querySelector("small").hidden = false;
select($(mine));
} else if (tabs.length) select(tabs[0]);
// The visitor's Linux format first, with the accent button.
if (sys.os === "linux") {
var rows = document.querySelector(".linux-rows");
var row = rows && rows.querySelector('[data-os="' + sys.slug + '"]');
if (row) {
rows.insertBefore(row, rows.firstChild);
var b = row.querySelector(".btn");
b.classList.remove("btn-secondary"); b.classList.add("btn-accent");
}
}
function openHash() {
var id = location.hash.slice(1);
if (!id || id.indexOf("download-") !== 0) return;
var panelId = id.replace(/^(download-(windows|macos|linux)).*/, "$1");
var tab = tabs.filter(function (x) { return x.getAttribute("aria-controls") === panelId; })[0];
if (tab) select(tab);
var target = $(id);
if (target) target.scrollIntoView();
}
window.addEventListener("hashchange", openHash);
openHash();
// --- Header: border and the small download button --------------------------------
var top = document.querySelector(".top"), cta = $("cta");
function onScroll() {
top.classList.toggle("scrolled", window.scrollY > 4);
if (cta) top.classList.toggle("show-dl", cta.getBoundingClientRect().bottom < 56);
}
window.addEventListener("scroll", onScroll, { passive: true });
onScroll();
// --- Language --------------------------------------------------------------------
function remember(code) { try { localStorage.setItem("lang", code); } catch (e) {} }
var langSel = $("lang");
if (langSel) langSel.addEventListener("change", function () {
remember(langSel.value);
location.href = (langSel.value === "en" ? "/" : "/" + langSel.value + "/") + location.hash;
});
document.querySelectorAll("[data-lang]").forEach(function (a) {
a.addEventListener("click", function () { remember(a.dataset.lang); });
});
// --- Lightbox --------------------------------------------------------------------
var lb = $("lightbox"), img = $("lb-img"), cap = $("lb-cap"), count = $("lb-count"), full = $("lb-full");
var shots = CP.shots || [], cur = 0, opener = null, viaHistory = false;
var webp = document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") === 0;
function src(s) { return webp ? s.src : s.fallback; }
function preload(i) { var s = shots[(i + shots.length) % shots.length]; if (s) new Image().src = src(s); }
function show(i) {
cur = (i + shots.length) % shots.length;
var s = shots[cur];
img.src = src(s);
img.alt = s.alt;
cap.textContent = s.caption;
full.href = s.full;
count.textContent = t("lb_count").replace("{n}", cur + 1).replace("{total}", shots.length);
preload(cur + 1); preload(cur - 1);
if (viaHistory) history.replaceState({ lb: true }, "", "#screenshot-" + (cur + 1));
}
function open(i, from) {
if (!shots.length) return;
opener = from || null;
lb.hidden = false;
document.documentElement.style.overflow = "hidden";
history.pushState({ lb: true }, "", "#screenshot-" + (i + 1));
viaHistory = true;
show(i);
$("lb-close").focus();
}
function close(fromPop) {
if (lb.hidden) return;
lb.hidden = true;
document.documentElement.style.overflow = "";
if (viaHistory && !fromPop) { viaHistory = false; history.back(); }
viaHistory = false;
var thumb = document.querySelector('[data-shot="' + (cur + 1) + '"]') || opener;
if (thumb) { thumb.scrollIntoView({ block: "nearest" }); thumb.focus(); }
}
document.querySelectorAll("[data-shot]").forEach(function (a) {
a.addEventListener("click", function (e) {
e.preventDefault();
open(parseInt(a.dataset.shot, 10) - 1, a);
});
});
$("lb-close").addEventListener("click", function () { close(); });
$("lb-prev").addEventListener("click", function () { show(cur - 1); });
$("lb-next").addEventListener("click", function () { show(cur + 1); });
lb.addEventListener("click", function (e) { if (e.target === lb || e.target.classList.contains("lb-fig")) close(); });
document.addEventListener("keydown", function (e) {
if (lb.hidden) return;
if (e.key === "Escape") close();
else if (e.key === "ArrowRight") show(cur + 1);
else if (e.key === "ArrowLeft") show(cur - 1);
else if (e.key === "Home") show(0);
else if (e.key === "End") show(shots.length - 1);
else if (e.key === "Tab") {
// Keep the focus inside the lightbox.
var f = [$("lb-close"), $("lb-prev"), $("lb-next"), full];
var i = f.indexOf(document.activeElement);
e.preventDefault();
f[(i + (e.shiftKey ? -1 : 1) + f.length) % f.length].focus();
}
});
window.addEventListener("popstate", function () { if (!lb.hidden) close(true); });
// Swipe: left/right turns, down closes.
var sx = 0, sy = 0, dx = 0, dy = 0, touching = false;
img.addEventListener("touchstart", function (e) { touching = true; sx = e.touches[0].clientX; sy = e.touches[0].clientY; dx = dy = 0; }, { passive: true });
img.addEventListener("touchmove", function (e) {
if (!touching) return;
dx = e.touches[0].clientX - sx; dy = e.touches[0].clientY - sy;
if (Math.abs(dx) > Math.abs(dy)) img.style.transform = "translateX(" + dx + "px)";
else if (dy > 0) { img.style.transform = "translateY(" + dy + "px)"; img.style.opacity = String(Math.max(0.3, 1 - dy / 400)); }
}, { passive: true });
img.addEventListener("touchend", function () {
touching = false;
img.style.transform = ""; img.style.opacity = "";
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > window.innerWidth * 0.2) show(cur + (dx < 0 ? 1 : -1));
else if (dy > 80 && Math.abs(dy) > Math.abs(dx)) close();
});
// A link to #screenshot-N opens that screenshot.
var m = /^#screenshot-(\d+)$/.exec(location.hash);
if (m) {
history.replaceState(null, "", location.pathname);
open(Math.min(parseInt(m[1], 10), shots.length) - 1);
}
})();