-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (61 loc) · 1.9 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
const poemContainer = document.getElementById("poems");
const sidebar = document.getElementById("poem-list");
if (!location.hash) {
location.hash = 0;
}
poems.forEach((poem, i) => {
const poemDiv = document.createElement("div");
poemDiv.classList.add(`${i}`, "selectable", "poem")
// create header
const header = document.createElement("h2");
header.innerText = poem.title;
poemDiv.appendChild(header);
// create content
const poemContent = createTelescopicTextFromBulletedList(poem.content, {
htmlContainerTag: "div",
specialCharacters: {
"^> (.*)$": (lineText) => {
const el = document.createElement("blockquote");
const span = document.createElement("span");
span.appendChild(document.createTextNode(lineText));
el.appendChild(span);
return el;
},
"---": () => {
const el = document.createElement("hr");
el.classList.add("spacer");
return el;
}
}
});
poemDiv.appendChild(poemContent);
// create signoff
const signoff = document.createElement("p");
signoff.innerHTML = `— ${poem.author}`;
poemDiv.appendChild(signoff);
// make list entry
const entry = document.createElement("li");
entry.classList.add(`${i}`, "selectable", "poem-entry")
const title = document.createElement("h3");
title.innerText = poem.title;
entry.appendChild(title);
sidebar.appendChild(entry);
if (i === parseInt(location.hash.substring(1))) {
poemDiv.classList.add("active");
entry.classList.add("active");
}
entry.addEventListener("click", () => {
// clear old
const old = document.getElementsByClassName("selectable");
for (const o of old) {
o.classList.remove("active");
}
// set current
poemDiv.classList.add("active");
entry.classList.add("active");
location.hash = i;
// scroll to top
window.scrollTo(0, 0);
});
poemContainer.appendChild(poemDiv);
});