-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_manager.js
147 lines (129 loc) · 4.65 KB
/
script_manager.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
137
138
139
140
141
142
143
144
145
146
147
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("next");
let addScript = (info) => {
return new Promise(function(resolve, reject) {
let gfgData = document.createElement('script');
gfgData.src = info;
gfgData.async = false;
gfgData.onload = () => {
resolve(info);
};
gfgData.onerror = () => {
reject(info);
};
document.body.appendChild(gfgData);
});
};
let common = ["theme.js", "priority_queue.js"]
let sketches = {
"map": {
scripts: ["map/map.js", "map/bfs_map.js", "map/astar_map.js"],
buttons: ".buttons-map",
displayname: "Map",
source: "https://github.com/benman604/benman604.github.io/tree/v2/map"
},
"maze": {
scripts: ["maze/cell.js", "maze/maze.js", "maze/astar_maze.js", "maze/bfs_maze.js", "maze/dfs_maze.js"],
buttons: ".buttons-maze",
displayname: "Maze",
source: "https://github.com/benman604/benman604.github.io/tree/v2/maze"
},
"balls": {
scripts: ["polar/polar.js"],
buttons: ".buttons-polar",
displayname: "Bouncing Balls",
source: "https://github.com/benman604/benman604.github.io/tree/v2/polar"
}
}
let scripts = [];
const params = new URLSearchParams(window.location.search);
let currSketch = params.get("sketch");
if(params.has("sketch") && sketches[currSketch] !== undefined) {
scripts = sketches[currSketch].scripts
} else {
currSketch = "map"
scripts = sketches[currSketch].scripts
}
document.querySelectorAll(sketches[currSketch].buttons).forEach(element => {
element.style.display = 'block';
});
const sketchesList = document.getElementById("sketchesList");
for (const [name, sketch] of Object.entries(sketches)) {
if (name !== currSketch) {
document.querySelectorAll(sketch.buttons).forEach(element => {
element.style.display = 'none';
});
let a = document.createElement("a")
a.innerText = sketch.displayname
a.href = `?sketch=${name}`;
sketchesList.appendChild(a);
sketchesList.appendChild(document.createElement("br"));
}
}
document.getElementById('sketchName').innerText = sketches[currSketch].displayname;
document.getElementById('sourceLink').href = sketches[currSketch].source;
let promiseData = [];
[...common, ...scripts].forEach(function(info) {
promiseData.push(addScript(info));
});
console.log(promiseData)
Promise.all(promiseData).then(function() {
console.log('required scripts loaded successfully');
}).catch(function(gfgData) {
console.log(gfgData + ' failed to load');
});
let skeys = Object.keys(sketches);
let curri = skeys.indexOf(currSketch);
prevBtn.onclick = () => {
curri = (curri - 1 + skeys.length) % skeys.length;
window.location.href = `?sketch=${skeys[curri]}`;
}
nextBtn.onclick = () => {
curri = (curri + 1) % skeys.length;
window.location.href = `?sketch=${skeys[curri]}`;
}
// Define all the modals and their associated buttons and close buttons
const allModals = [
{
buttonId: "whatsThis",
modalId: "whatsThisModal",
closeId: "whatsThisClose"
},
{
buttonId: "allSketches",
modalId: "allSketchesModal",
closeId: "allSketchesClose"
}
];
// Function to initialize modals
function initializeModals(modals) {
modals.forEach(({ buttonId, modalId, closeId }) => {
const button = document.getElementById(buttonId);
const modal = document.getElementById(modalId);
const close = document.getElementById(closeId);
// Toggle modal visibility when the button is clicked
button.onclick = () => {
// Open the clicked modal
modal.style.display = (modal.style.display === "none") ? "block" : "none";
button.classList.toggle("activemodal");
// Close all other modals
modals.forEach(({ modalId: otherModalId, buttonId: otherButtonId }) => {
if (otherModalId !== modalId) {
const otherModal = document.getElementById(otherModalId);
const otherButton = document.getElementById(otherButtonId);
if (otherModal) {
otherModal.style.display = "none"
otherButton.classList.remove("activemodal");
};
}
});
};
// Close modal when the close button is clicked
close.onclick = () => {
modal.style.display = "none";
button.classList.remove("activemodal");
};
});
}
// Initialize all modals
initializeModals(allModals);