Debian Codegraph @

pkg/playground/preloaded.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3
4 // This is the basic structure of a preloaded page. It has a two
5 // phase initialization: phase 1 while it is still invisible, and
6 // phase 2 when it becomes visible.
7 //
8 // Elements on the page (including the body) are made visible only
9 // once the page itself is visible. Otherwise layout might go wrong
10 // and not recover automatically.
11
12 function init_1() {
13 return (cockpit.spawn(["hostname"])
14 .then(data => {
15 document.getElementById("host").innerText = data.trim();
16 }));
17 }
18
19 function init_2() {
20 return (cockpit.file("/etc/os-release").read()
21 .then(data => {
22 document.getElementById("release").innerText = data;
23 }));
24 }
25
26 function navigate() {
27 document.getElementById("path").innerText = cockpit.location.path.join("/");
28 document.body.removeAttribute("hidden");
29 }
30
31 function maybe_phase_2() {
32 cockpit.removeEventListener("visibilitychange", maybe_phase_2);
33 if (cockpit.hidden) {
34 cockpit.addEventListener("visibilitychange", maybe_phase_2);
35 } else {
36 init_2().then(navigate);
37 }
38 }
39
40 function init() {
41 init_1().then(maybe_phase_2);
42 }
43
44 document.addEventListener("DOMContentLoaded", init);