-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (148 loc) · 4.62 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import DOM from "/src/DomBuilder.js";
import LocalStorage from "./src/LocalStorage.js";
import { useState } from "./src/Utils.js";
import getSvg from "./src/Svgs.js";
import posts from "./src/pages/posts.js";
import post from "./src/pages/post.js";
import tags from "./src/pages/tags.js";
import about from "./src/pages/about/about.js";
import search from "./src/pages/search.js";
import main from "./src/pages/main.js";
function title() {
return DOM.of("a")
.inner("Pedroth")
.attr("href", "/")
.attr("class", "navLogo");
}
function nav(actualPage) {
return DOM.of("nav")
.append(
...[
{ id: "posts", text: "Posts", url: "?p=posts" },
{ id: "tags", text: "Tags", url: "?p=tags" },
{ id: "about", text: "About", url: "?p=about" },
]
.map(({ id, text, url }) =>
DOM
.of("a")
.addClass("specialLink")
.addClass(id === actualPage ? "active" : "")
.attr("href", url)
.inner(text)
)
)
}
function searchButton(isActive) {
return DOM.of("button")
.addClass("specialIcon")
.addClass(isActive ? "active" : "")
.inner(getSvg("/assets/search.svg"))
.event("click", () => {
window.location.href = "?p=search";
})
}
function themeButton() {
const [getTheme, setTheme, onThemeChange] = useState(LocalStorage.getItem("theme") || "dark");
const button = DOM
.of("button")
.addClass("specialIcon")
.event("click", () => {
setTheme(theme => {
const nextTheme = theme === "dark" ? "light" : "dark";
LocalStorage.setItem("theme", nextTheme);
return nextTheme;
});
})
const updateTheme = (aTheme) => {
const isDark = aTheme === "dark";
button.inner(isDark ? getSvg("/assets/light_mode.svg") : getSvg("/assets/dark_mode.svg"))
if (isDark) {
document.documentElement.style.setProperty("--background-color", "rgb(24,24,24)");
document.documentElement.style.setProperty("--text-color", "rgba(255,255,255,0.9)");
} else {
document.documentElement.style.setProperty("--background-color", "rgb(254 253 245)");
document.documentElement.style.setProperty("--text-color", "rgba(24,24,24,0.9)");
}
}
onThemeChange(newTheme => {
updateTheme(newTheme);
})
// first render
updateTheme(getTheme());
return button;
}
function tools(actualPage) {
return DOM.of("div")
.addClass("tools")
.append(
searchButton(actualPage === "search"),
themeButton()
)
}
function header(page) {
return DOM.of("header")
.append(
DOM.of("div")
.addClass("navDiv")
.append(title())
.append(nav(page))
.append(tools(page))
)
.append(DOM.of("hr"))
}
function footer() {
return DOM.of("footer")
.append(DOM.of("hr"))
.append(
DOM.of("div")
.append(
DOM.of("p")
.append(
DOM.of("a")
.addClass("specialIcon")
.attr("href", "https://github.com/pedroth")
.inner(getSvg("/assets/github.svg")),
DOM.of("a")
.addClass("specialIcon")
.attr("href", "https://www.youtube.com/@pedroth3")
.inner(getSvg("/assets/youtube.svg")),
DOM.of("a")
.addClass("specialIcon")
.attr("href", "https://twitter.com/pedroth9")
.inner(getSvg("/assets/x-twitter.svg")),
DOM.of("a")
.addClass("specialIcon")
.attr("href", "/feed/rss.xml")
.inner(getSvg("/assets/rss.svg"))
),
DOM.of("p")
.inner(`© ${new Date().getFullYear()} Pedroth`)
)
)
}
//========================================================================================
/* *
* MAIN *
* */
//========================================================================================
(async () => {
const urlParams = new URLSearchParams(window.location.search);
const pageParam = urlParams.get("p") || "";
const split = pageParam.split("/");
const page = split.length > 0 ? split[0] : pageParam;
const navMap = {
post,
tags,
about,
posts,
search,
}
const content = page in navMap ?
await navMap[page](pageParam) :
await main();
DOM.ofId("root")
.append(header(page))
.append(content)
.append(footer())
.addClass("loaded")
})();