-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.js
More file actions
211 lines (182 loc) · 5.27 KB
/
functions.js
File metadata and controls
211 lines (182 loc) · 5.27 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @global
* @var {Object} CircleSlices
*/
window.onerror = function (message, source, lineno, colno, error) {
alert(
`Error: ${message}\nSource: ${source}\nLine: ${lineno}\nColumn: ${colno}\nError object: ${JSON.stringify(error)}`
);
return false; // Returning false will suppress the default browser error message
};
var activePage = "home";
var dark = localStorage.getItem("dark-mode");
if (dark === "true") {
document.body.classList.add("dark-mode");
}
if (typeof URLSearchParams === "undefined") {
var script = document.createElement("script");
script.src = "https://cdn.jsdelivr.net/npm/url-search-params-polyfill@8.2.5/index.min.js";
script.async = false;
document.head.appendChild(script);
}
if (typeof fetch === "undefined") {
var script = document.createElement("script");
script.src = "https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.min.js";
script.async = false;
document.head.appendChild(script);
}
function $(selector, parent) {
return (parent || document).querySelector(selector);
}
function initContactForm() {
var autoSend = false;
const fields = ["Name", "Email", "Subject", "Message"];
const searchParams = new URLSearchParams(window.location.search);
fields.forEach(function (field) {
var value = searchParams.get(field);
$("#" + field).value = value;
if (value) {
autoSend = true;
}
});
if (autoSend) {
setTimeout(function () {
const form = $("#contact-form");
form.target = "";
form.submit();
}, 2000);
}
}
function hide(id) {
const el = document.getElementById(id);
if (el) {
el.style.display = "none";
} else {
console.error("element you are searching does not exist... check your selector");
}
}
function hidePreviousPage() {
hide(activePage);
var link = $(`a[data-page="${activePage}"]`);
link.classList.remove("active");
}
function showPage(pageId, rotate) {
hidePreviousPage();
document.getElementById(pageId).style.display = "block";
var link = $(`a[data-page="${pageId}"]`);
link.classList.add("active");
if (rotate !== false) {
setTimeout(() => {
rotateCircleMenu(pageId);
}, 10);
}
activePage = pageId;
}
function initLink(e) {
const link = e.target;
if (link.matches("a[data-page]")) {
const id = link.getAttribute("data-page");
showPage(id);
if (history.pushState) {
e.preventDefault();
history.pushState(null, null, "#" + id);
}
}
}
function initMenu() {
$("#top-menu-bar").addEventListener("click", initLink);
Array.from(document.querySelectorAll("a.page-actions[data-page]")).forEach(function (link) {
link.addEventListener("click", initLink);
});
$("#colorblind").addEventListener("click", function () {
document.body.classList.toggle("grayscale");
});
$("#toggle-dark-mode").addEventListener("click", function () {
document.body.classList.toggle("dark-mode");
localStorage.setItem("dark-mode", document.body.classList.contains("dark-mode"));
});
}
var circleMenu = {
home: { icon: "🏡", text: "Home" },
skills: { icon: "👌", text: "Skills" },
projects: { icon: "🔥", text: "Projects" },
languages: { icon: "🌎", text: "Languages" },
rubik: { icon: "💠", text: "Rubik" }
};
function initCircleMenu(pageId) {
const renderTo = "#circle-menu";
CircleSlices.render({
renderTo: renderTo,
groupSize: 200,
slicesSize: 120,
centerSize: 25,
default: circleMenu[pageId].icon,
text: Object.keys(circleMenu)
.map(function (hash) {
return `#${circleMenu[hash].text} \n ${circleMenu[hash].icon}`;
})
.join("\n"),
centerText: ``
});
document.querySelector(renderTo).addEventListener("rotate", event => {
const { index } = event.detail;
const id = Object.keys(circleMenu)[index];
showPage(id, false);
if (history.pushState) {
history.pushState(null, null, "#" + id);
}
});
}
function rotateCircleMenu(pageId) {
const i = Object.keys(circleMenu).findIndex(function (hash) {
return hash === pageId;
});
const slice = $(`.slice-text[data-index="${i + 1}"]`);
if (slice) {
const angle = parseFloat(slice.style.getPropertyValue("--angle").replace("deg", ""));
CircleSlices.rotate("#circle-menu", -angle);
}
}
function getHTMLSkills(skills) {
return skills
.map(function (skill) {
return `<li class="${skill.endorsements > 9 ? "favorite" : ""}">
${skill.name} <span>· ${skill.endorsements}</span>
</li>`;
})
.join("");
}
function showSkills(skills) {
const ul = $("#skills ul");
ul.innerHTML = getHTMLSkills(skills);
}
function loadSkills() {
fetch("/data/skills.json")
.then(function (r) {
return r.json();
})
.then(function (allSkills) {
allSkills.sort(function (s1, s2) {
return s2.endorsements - s1.endorsements;
});
showSkills(allSkills);
});
}
window.addEventListener("load", function () {
initContactForm();
var hash = window.location.hash.substring(1);
if (hash) {
var page = $("#" + hash);
if (page && page.classList.contains("page")) {
activePage = hash;
}
}
showPage(activePage);
initMenu();
initCircleMenu(activePage);
setTimeout(function () {
initRubik($("#rubikChallenge"));
initRubik($("#rubikChallengePage"));
loadSkills();
}, 10);
});