chessbaseLinux/apps/web/page/page.js

212 lines
7.9 KiB
JavaScript
Raw Normal View History

Web version, stage 1: the interface as a library over a platform layer; apps/web apps/desktop becomes a library (chesspenguin_app: the interface) and a thin binary; apps/web starts the same interface in a page's canvas (eframe's WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser. The platform layer (apps/desktop/src/platform): the program's folders, files, background work, the files the user opens or saves, the clipboard and what the platform offers (CAPS). On a computer it is the system as before; in a browser the program's data lives in IndexedDB behind an in-memory file tree loaded before the start, the files the user opens are read into it for the session (only read), background jobs run on the page's thread after the frame, the clipboard is read through the browser's API, sounds play through audio elements, a game is saved as a download. What a browser does not have is not built for it: updates, the online games window and its probe, engine processes (analysis starts a Web Worker there; the review and preparation jobs, Rodent and the judge are native), blocking requests (engine, Maia and Rodent downloads report no download for the platform, translation reports that it makes no requests). The desktop program behaves as before; its build finds unused code (the web build compiles parts it never shows). apps/web: the entry point, the page /app/ (device check before anything large is loaded: phones and tablets get the page about the apps in the site's nine languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile web), a static server and headless Firefox checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
// The page /app/: a computer gets ChessPenguin (the WebAssembly program, loaded only
// then); a phone or a tablet gets the page about the apps (docs/design/web-version.md
// §3.6, decisions «Веб-версия»), with a quiet way to open the web version anyway.
//
// ?phone=1 the device page on any device (checks, screenshots of the site);
// ?phone=android, ?phone=ios: that variant of it
Web version, stage 1: the interface as a library over a platform layer; apps/web apps/desktop becomes a library (chesspenguin_app: the interface) and a thin binary; apps/web starts the same interface in a page's canvas (eframe's WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser. The platform layer (apps/desktop/src/platform): the program's folders, files, background work, the files the user opens or saves, the clipboard and what the platform offers (CAPS). On a computer it is the system as before; in a browser the program's data lives in IndexedDB behind an in-memory file tree loaded before the start, the files the user opens are read into it for the session (only read), background jobs run on the page's thread after the frame, the clipboard is read through the browser's API, sounds play through audio elements, a game is saved as a download. What a browser does not have is not built for it: updates, the online games window and its probe, engine processes (analysis starts a Web Worker there; the review and preparation jobs, Rodent and the judge are native), blocking requests (engine, Maia and Rodent downloads report no download for the platform, translation reports that it makes no requests). The desktop program behaves as before; its build finds unused code (the web build compiles parts it never shows). apps/web: the entry point, the page /app/ (device check before anything large is loaded: phones and tablets get the page about the apps in the site's nine languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile web), a static server and headless Firefox checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
// ?phone=0 the program on any device
// ?lang=xx the language (also of the program)
(function () {
"use strict";
var LANGS = [
["en", "English"], ["ru", "Русский"], ["fr", "Français"], ["de", "Deutsch"], ["es", "Español"],
["it", "Italiano"], ["pt", "Português"], ["uz", "Oʻzbekcha"], ["kk", "Қазақша"],
];
// The engine the web version downloads for analysis (Stockfish 19, brotli).
var ENGINE_MB = 78;
var ANYWAY_KEY = "cp.web.phoneAnyway";
var SITE = "https://chess.vrubel.xyz";
var params = new URLSearchParams(location.search);
var texts = window.CP_TEXTS || {};
function pickLang() {
var asked = (params.get("lang") || "").toLowerCase();
if (texts[asked]) return asked;
var prefs = navigator.languages || [navigator.language || ""];
for (var i = 0; i < prefs.length; i++) {
var code = String(prefs[i]).split(/[-_]/)[0].toLowerCase();
if (texts[code]) return code;
}
return "en";
}
var lang = pickLang();
function t(key, vars) {
var s = (texts[lang] && texts[lang][key]) || (texts.en && texts.en[key]) || key;
for (var k in vars || {}) s = s.split("{" + k + "}").join(vars[k]);
return s;
}
function el(tag, attrs, children) {
var e = document.createElement(tag);
for (var a in attrs || {}) {
if (a === "text") e.textContent = attrs[a];
else if (a === "onclick") e.addEventListener("click", attrs[a]);
else e.setAttribute(a, attrs[a]);
}
(children || []).forEach(function (c) { if (c) e.appendChild(c); });
return e;
}
function storage(get, value) {
try {
if (get) return localStorage.getItem(ANYWAY_KEY);
if (value === null) localStorage.removeItem(ANYWAY_KEY);
else localStorage.setItem(ANYWAY_KEY, value);
} catch (e) {}
return null;
}
// ----- the program --------------------------------------------------------------
function startApp() {
document.documentElement.lang = lang;
document.title = t("app_title");
document.getElementById("cp-device").hidden = true;
var status = document.getElementById("cp-status");
status.textContent = t("app_loading");
status.hidden = false;
var failed = function (e) {
console.error("ChessPenguin did not start:", e);
document.getElementById("cp-app").hidden = true;
status.textContent = t("app_failed");
status.className = "failed";
status.hidden = false;
};
import("./chesspenguin_web.js")
.then(function (m) { return m.default().then(function () { return m; }); })
.then(function (m) {
document.getElementById("cp-app").hidden = false;
return m.start("cp-canvas");
})
.then(function () {
status.hidden = true;
document.getElementById("cp-canvas").focus();
})
.catch(failed);
}
// ----- the page for phones and tablets ---------------------------------------------
function shots(names) {
return el("div", { class: "shots" }, names.map(function (n) {
return el("img", { src: "/screenshots/" + n, alt: "", loading: "lazy" });
Web version, stage 1: the interface as a library over a platform layer; apps/web apps/desktop becomes a library (chesspenguin_app: the interface) and a thin binary; apps/web starts the same interface in a page's canvas (eframe's WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser. The platform layer (apps/desktop/src/platform): the program's folders, files, background work, the files the user opens or saves, the clipboard and what the platform offers (CAPS). On a computer it is the system as before; in a browser the program's data lives in IndexedDB behind an in-memory file tree loaded before the start, the files the user opens are read into it for the session (only read), background jobs run on the page's thread after the frame, the clipboard is read through the browser's API, sounds play through audio elements, a game is saved as a download. What a browser does not have is not built for it: updates, the online games window and its probe, engine processes (analysis starts a Web Worker there; the review and preparation jobs, Rodent and the judge are native), blocking requests (engine, Maia and Rodent downloads report no download for the platform, translation reports that it makes no requests). The desktop program behaves as before; its build finds unused code (the web build compiles parts it never shows). apps/web: the entry point, the page /app/ (device check before anything large is loaded: phones and tablets get the page about the apps in the site's nine languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile web), a static server and headless Firefox checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
}));
}
function shareButton() {
var url = SITE + "/app/?lang=" + lang;
var label = navigator.share ? t("app_send_link") : t("copy_link");
var b = el("button", { class: "btn", type: "button", text: "⇪ " + label });
b.addEventListener("click", function () {
if (navigator.share) {
navigator.share({ title: "ChessPenguin", url: url }).catch(function () {});
} else if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(function () { b.textContent = t("link_copied"); });
}
});
return b;
}
function anyway() {
var size = ENGINE_MB + (lang === "ru" || lang === "kk" ? " МБ" : " MB");
return el("div", null, [
el("hr"),
el("button", {
class: "anyway", type: "button", text: t("app_open_anyway"),
onclick: function () { storage(false, "1"); startApp(); },
}),
el("p", { class: "weak", text: t("app_open_anyway_note", { size: size }) }),
]);
}
function androidPart() {
var v = CPDevice.androidVersion();
if (v && v < 12) {
return [el("p", { text: t("app_and_old", { version: String(v) }) }), shareButton()];
}
var hint = el("p", { class: "weak", text: t("next_android"), hidden: "" });
var show = function () { hint.hidden = false; };
return [
el("a", { class: "btn", href: "/download/android-noengine", rel: "nofollow", onclick: show }, [
document.createTextNode(t("btn_android")),
el("small", { text: t("and_light_title") }),
]),
el("p", null, [
el("a", { class: "link", href: "/download/android", rel: "nofollow", onclick: show, text: t("and_full_title") + " ›" }),
]),
el("p", { class: "weak", text: t("and_requires") }),
hint,
shots(["phone/01-home-360.webp", "phone/03-game-360.webp", "phone/04-engine-360.webp", "phone/08-review-360.webp"]),
el("p", null, [el("a", { class: "link", href: "/" + (lang === "en" ? "" : lang + "/") + "#download-android", text: t("app_and_more") + " ›" })]),
];
}
function iosPart() {
return [
el("p", { class: "lead", text: t("app_ios_lead") }),
el("p", { text: t("app_ios_now") }),
shareButton(),
el("p", { style: "margin-top:16px", text: t("app_ios_notify") }),
el("div", { class: "contacts" }, [
el("a", { class: "link", href: "mailto:vrubelroman@gmail.com?subject=ChessPenguin", text: "✉ " + t("email") }),
el("a", { class: "link", href: "https://t.me/vrubelr", rel: "noopener", text: "✈ Telegram" }),
]),
shots(["01-game-640.webp", "04-review-640.webp"]),
];
}
function showDevicePage(kind) {
document.documentElement.lang = lang;
document.title = t("app_phone_title");
var page = document.getElementById("cp-device");
page.textContent = "";
var select = el("select", { "aria-label": t("language") }, LANGS.map(function (l) {
var o = el("option", { value: l[0], text: l[1] });
if (l[0] === lang) o.selected = true;
return o;
}));
select.addEventListener("change", function () {
lang = select.value;
showDevicePage(kind);
});
page.appendChild(el("header", null, [
el("span", { class: "brand" }, [el("img", { src: "icon-64.png", alt: "" }), document.createTextNode("ChessPenguin")]),
select,
]));
page.appendChild(el("h1", { text: t("app_phone_title") }));
var body;
if (kind === "android-phone" || kind === "android-tablet") {
body = [el("p", { text: t("app_and_lead") })].concat(androidPart());
} else if (kind === "iphone" || kind === "ipad") {
body = iosPart();
} else {
body = [
el("p", { text: t("app_phone_generic") }),
el("a", { class: "btn", href: "/download/android-noengine", rel: "nofollow", text: t("btn_android") }),
el("p"),
shareButton(),
];
}
body.forEach(function (n) { page.appendChild(n); });
page.appendChild(anyway());
page.hidden = false;
}
// ----- which one --------------------------------------------------------------------
var asked = params.get("phone");
var kind = CPDevice.deviceClass();
if (asked === "1") {
showDevicePage(kind === "computer" ? "phone" : kind);
} else if (asked === "android" || asked === "ios") {
showDevicePage(asked === "android" ? "android-phone" : "iphone");
Web version, stage 1: the interface as a library over a platform layer; apps/web apps/desktop becomes a library (chesspenguin_app: the interface) and a thin binary; apps/web starts the same interface in a page's canvas (eframe's WebRunner). cfg(web) (build.rs) marks WebAssembly in a browser. The platform layer (apps/desktop/src/platform): the program's folders, files, background work, the files the user opens or saves, the clipboard and what the platform offers (CAPS). On a computer it is the system as before; in a browser the program's data lives in IndexedDB behind an in-memory file tree loaded before the start, the files the user opens are read into it for the session (only read), background jobs run on the page's thread after the frame, the clipboard is read through the browser's API, sounds play through audio elements, a game is saved as a download. What a browser does not have is not built for it: updates, the online games window and its probe, engine processes (analysis starts a Web Worker there; the review and preparation jobs, Rodent and the judge are native), blocking requests (engine, Maia and Rodent downloads report no download for the platform, translation reports that it makes no requests). The desktop program behaves as before; its build finds unused code (the web build compiles parts it never shows). apps/web: the entry point, the page /app/ (device check before anything large is loaded: phones and tablets get the page about the apps in the site's nine languages, keys app_* in website/site/i18n), build.sh (wasm-bindgen, profile web), a static server and headless Firefox checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:09:17 +03:00
} else if (asked === "0" || kind === "computer" || storage(true) === "1") {
startApp();
} else {
showDevicePage(kind);
}
})();