-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (104 loc) · 3.7 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
const header = document.getElementById("header");
let sticky = header.offsetTop;
let worksTimeout;
let typeTimeout;
function titleCase(s) {
return s.replace(/\w\S*/g, function (t) {
return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase();
});
}
function timeSince(date) {
const seconds = Math.floor((new Date() - date) / 1000);
const timeUnits = [
{ name: 'year', seconds: 31536000 },
{ name: 'month', seconds: 2592000 },
{ name: 'day', seconds: 86400 },
{ name: 'hour', seconds: 3600 },
{ name: 'minute', seconds: 60 },
{ name: 'second', seconds: 1 }
];
for (const unit of timeUnits) {
const interval = Math.floor(seconds / unit.seconds);
if (interval >= 1) {
return `${interval} ${unit.name}${interval > 1 ? 's' : ''}`;
}
}
}
scrollEvent = () => {
// Check if Lucas Oberwager should be stuck to top
if (window.pageYOffset >= sticky) {
header.classList.add("sticky");
header.children[0].classList.remove("type");
document.getElementsByClassName("works")[0].classList.remove("hideWorks");
clearTimeout(worksTimeout);
clearTimeout(typeTimeout);
} else {
header.classList.remove("sticky");
}
// Check if work link is in view
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const positionFromTop = elements[i].getBoundingClientRect().top;
if (positionFromTop - windowHeight <= 0) {
element.classList.add("worksInView");
element.classList.remove("hidden");
}
if (positionFromTop - windowHeight > windowHeight / 6) {
element.classList.remove("worksInView");
element.classList.add("hidden");
}
}
};
window.onload = function () {
// Animate works links in
elements = document.querySelectorAll(".hidden");
windowHeight = window.innerHeight;
scrollEvent();
window.addEventListener("scroll", scrollEvent);
window.addEventListener("resize", function () {
windowHeight = window.innerHeight;
const container = document.getElementById("header").parentElement;
sticky = container.offsetTop;
scrollEvent();
});
typeTimeout = setTimeout(function () {
document.getElementsByClassName("type")[0].classList.remove("type");
}, 9300);
// Update currently reading book
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://openlibrary.org/people/lucasobe/books/currently-reading.json", true);
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 400) {
const data = JSON.parse(xhr.responseText);
document.getElementById("curBook").innerHTML = data.reading_log_entries
.map(function (e) {
return `<a href="https://openlibrary.org${e.work.key}">${titleCase(
e.work.title,
)}</a> by ${titleCase(e.work.author_names[0])}`;
})
.join(" and ");
}
};
xhr.send();
// Update last updated repo
const xhr2 = new XMLHttpRequest();
xhr2.open("GET", "https://api.github.com/users/Watt3r/events?per_page=1", true);
xhr2.onload = function () {
if (xhr2.status >= 200 && xhr2.status < 400) {
const data = JSON.parse(xhr2.responseText);
document.getElementById("curRepo").innerHTML = `<a href="https://github.com/${
data[0].repo.name
}">${data[0].repo.name}</a>, ${timeSince(new Date(data[0].created_at))} ago`;
const xhr3 = new XMLHttpRequest();
xhr3.open("GET", `https://api.github.com/repos/${data[0].repo.name}`, true);
xhr3.onload = function () {
const data2 = JSON.parse(xhr3.responseText);
if (data2.homepage) {
document.getElementById("curRepo").firstChild.setAttribute("href", data2.homepage);
}
};
xhr3.send();
}
};
xhr2.send();
};