-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintro-splash.js
More file actions
120 lines (103 loc) · 3.2 KB
/
Copy pathintro-splash.js
File metadata and controls
120 lines (103 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { initGLSLHills } from "./glsl-hills.js";
function isLoggedIn() {
return (
window.AuthManager &&
typeof window.AuthManager.isAuthenticated === "function" &&
window.AuthManager.isAuthenticated()
);
}
function waitForAuthReady() {
return new Promise((resolve) => {
if (window.AuthManager && typeof window.AuthManager.init === "function") {
window.AuthManager.init();
}
if (isLoggedIn()) {
resolve();
return;
}
const finish = () => resolve();
window.addEventListener("apush:session-restored", finish, { once: true });
window.addEventListener("apush:login", finish, { once: true });
setTimeout(finish, 500);
});
}
function revealApp(splash, shell, cleanup) {
if (cleanup) cleanup();
splash.classList.remove("intro-splash--exit");
splash.hidden = true;
document.body.classList.remove("intro-active");
shell.classList.remove("app-shell--gated");
shell.removeAttribute("inert");
window.dispatchEvent(new CustomEvent("apush:app-shell-revealed"));
if (window.APUSHReveal && typeof window.APUSHReveal.refresh === "function") {
window.APUSHReveal.refresh();
}
}
function showLoginGate(splash, shell) {
document.body.classList.add("intro-active");
shell.classList.add("app-shell--gated");
shell.setAttribute("inert", "");
splash.hidden = false;
const hillsRoot = document.getElementById("glsl-hills-root");
if (hillsRoot) {
return initGLSLHills(hillsRoot, { speed: 0.5, cameraZ: 125, planeSize: 256 });
}
return null;
}
function hasStoredSession() {
try {
return !!localStorage.getItem("apush_session");
} catch {
return false;
}
}
async function initIntroSplash() {
const splash = document.getElementById("intro-splash");
const shell = document.getElementById("app-shell");
if (!splash || !shell) return;
if (hasStoredSession() || isLoggedIn()) {
if (window.AuthManager && typeof window.AuthManager.init === "function") {
window.AuthManager.init();
}
revealApp(splash, shell, null);
return;
}
await waitForAuthReady();
let hillsCleanup = null;
const onAuthenticated = () => {
if (!isLoggedIn()) return;
revealApp(splash, shell, hillsCleanup);
hillsCleanup = null;
};
if (isLoggedIn()) {
revealApp(splash, shell, null);
return;
}
hillsCleanup = showLoginGate(splash, shell);
window.addEventListener("apush:session-restored", onAuthenticated);
window.addEventListener("apush:login", onAuthenticated);
window.addEventListener("apush:logout", () => {
if (isLoggedIn()) return;
hillsCleanup = showLoginGate(splash, shell);
});
}
function appPath(filename) {
return new URL(filename, window.location.href).href;
}
function setupIntroAuthButtons() {
document.getElementById("intro-sign-in-btn")?.addEventListener("click", () => {
window.location.assign(appPath("login.html"));
});
document.getElementById("intro-create-account-btn")?.addEventListener("click", () => {
window.location.assign(appPath("login.html?signup=1"));
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => {
setupIntroAuthButtons();
initIntroSplash();
});
} else {
setupIntroAuthButtons();
initIntroSplash();
}