-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
124 lines (105 loc) · 3.06 KB
/
index.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
import sections from "./sections/sections.js";
import Mandelbrot from "./mandelbrot.js";
import xd from "./engine_xd.js";
import rgb from "./rgb.js";
import pfp from "./pfp.js";
const _ = undefined;
const rbg_button = document.querySelector("#rgb");
let start;
const def = {
type: "div",
attributes: [],
properties: [],
children: [],
};
const generateElement = json => {
json = { ...def, ...json };
let element = document.createElement(json.type);
Object.entries(json.attributes).forEach(([ key, value ]) => element.setAttribute(key, value));
Object.entries(json.properties).forEach(([ key, value]) => element[key] = value);
json.children.forEach(child => element.appendChild(generateElement(child)));
return element;
};
const p = (_class, innerHTML) => {
return {
type: "p",
attributes: { class: _class },
properties: { innerHTML },
};
};
const div = (_class, id, children = []) => {
return {
type: "div",
attributes: { class: _class, id },
children,
}
}
const createCard = (id, {title = "", text = "", image, url}) => {
let img_child;
if (image) {
img_child = {
type: "img",
attributes: {
class: "card-image",
src: image,
}
};
}
let text_div = div("text", _, [
p("card-title", title),
p("card-text", text),
]);
let a = {
type: "a",
attributes: url ? { href: url } : {},
children: [ text_div ],
};
let card = generateElement(
div(`card ${id}-card`, _, [
img_child,
url ? a : text_div,
]),
);
return card;
};
const createSection = ({ name, id }) => {
let target = document.getElementById("sections");
let section = generateElement({
type: "section",
attributes: { class: "section" },
children: [
p("section-title", name),
div("section-cards", `${id}-cards`),
],
});
target.appendChild(section);
};
const loadSection = async section => {
let json;
try {
json = await (await import(`./sections/${section.id}.js`)).default;
}
catch(error) {
console.warn(`Failed to generate section ${section.id}:\n${error}`);
return;
}
let target = document.getElementById(`${section.id}-cards`)
if (section.urls === "relative") {
json.filter(element => element.image).forEach(element => element.image = `./sections/${section.id}/${element.image}`);
}
json.forEach(element => target.appendChild(createCard(section.id, element)));
};
window.onload = async () => {
const time = "Sections loaded in";
console.time(time);
sections.forEach(createSection);
await Promise.all(sections.map(loadSection));
console.timeEnd(time);
let width = document.getElementById("sections").offsetWidth;
let resolution = [ width, Math.ceil(width / 2) ];
start = Date.now();
pfp("#2196f3");
Mandelbrot(resolution);
xd(resolution);
rbg_button.onclick = rgb;
};