-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (112 loc) · 2.63 KB
/
app.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { $, $$, LoadFile, ResolveSceneByName, ResolveThemeByName, NullConditional, CreateElementByDescriptor, NullishCoalescingOp, Log } from "./modules/utils.js";
import { data } from "./modules/globals.js";
/**
* Where the frame is drawn.
* Contained within the index.
*/
let target;
/**
* Where the scene is drawn.
* Contained within the frame.
*/
let content;
/**
* Sets up and loads the first scene.
*/
function Initialize() {
target = $("#target");
LoadFile(ResolveThemeByName(conf.theme), (e) => {
target.innerHTML = e.responseText;
content = $("#content");
CreateElementByDescriptor({
parent: target,
name: "script",
attributes: [
{src: ResolveThemeByName(conf.theme, "js")},
{type: "module"},
],
});
CreateElementByDescriptor({
parent: target,
name: "link",
attributes: [
{rel: "stylesheet"},
{href: ResolveThemeByName(conf.theme, "css")}
],
});
if (data.currentScene === null) {
data.currentScene = conf.entryPoint;
}
// Load the first scene.
Scene(data.currentScene);
});
document.addEventListener("click", (e) => {
let sender = e.target;
// If it's not an anchor tag...
if (sender.tagName !== "A") {
return;
}
// And it's not a navigation link...
if (sender.getAttribute("data-link") !== null) {
return
}
let allowSwitch = true;
let required = $$(":required");
if (required.length > 0) {
required.forEach((e) => {
if (e.value === "") {
allowSwitch = false;
}
});
}
// Then navigate to that scene.
e.preventDefault();
if (allowSwitch) {
Scene(sender.getAttribute("href"));
}
});
document.addEventListener("file-loaded", (e) => {
if (e.detail === 200) {
Scene(data.currentScene);
}
});
}
/**
* Loads a scene by its name.
* @param {string} name Name of the scene to load.
* @param {Function} cb Callback when the scene loaded.
*/
function Scene(name, cb = null) {
LoadFile(ResolveSceneByName(name), (e) => {
Log(null, true);
content.innerHTML = e.responseText;
CreateElementByDescriptor({
parent: content,
name: "script",
attributes: [
{src: ResolveSceneByName(name, "js")},
{type: "module"}
]
});
CreateElementByDescriptor({
parent: content,
name: "link",
attributes: [
{rel: "stylesheet"},
{href: ResolveSceneByName(name, "css")}
]
});
if ($("#sceneData") !== null) {
document.title = `${$("#sceneData").getAttribute("data-title")} | ${conf.title}`;
} else {
document.title = conf.title;
}
// Set the scene as the current scene.
data.currentScene = name;
data.subscribers = {};
if (cb !== null) {
cb();
}
});
}
Initialize();