mirror-cockpit-project-cockpit/pkg/playground/preloaded.js
Martin Pitt 07cf7634ea pkg: Add missing license declarations
Update static code test accordingly.
2026-02-04 10:24:11 +01:00

44 lines
1.3 KiB
JavaScript

// SPDX-License-Identifier: LGPL-2.1-or-later
import cockpit from "cockpit";
// This is the basic structure of a preloaded page. It has a two
// phase initialization: phase 1 while it is still invisible, and
// phase 2 when it becomes visible.
//
// Elements on the page (including the body) are made visible only
// once the page itself is visible. Otherwise layout might go wrong
// and not recover automatically.
function init_1() {
return (cockpit.spawn(["hostname"])
.then(data => {
document.getElementById("host").innerText = data.trim();
}));
}
function init_2() {
return (cockpit.file("/etc/os-release").read()
.then(data => {
document.getElementById("release").innerText = data;
}));
}
function navigate() {
document.getElementById("path").innerText = cockpit.location.path.join("/");
document.body.removeAttribute("hidden");
}
function maybe_phase_2() {
cockpit.removeEventListener("visibilitychange", maybe_phase_2);
if (cockpit.hidden) {
cockpit.addEventListener("visibilitychange", maybe_phase_2);
} else {
init_2().then(navigate);
}
}
function init() {
init_1().then(maybe_phase_2);
}
document.addEventListener("DOMContentLoaded", init);