Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (107 loc) · 3.35 KB
/
Copy pathapp.js
File metadata and controls
124 lines (107 loc) · 3.35 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
const grid = document.querySelector("#cards-grid");
const allowedThemes = [
"halloween",
"christmas",
"summer",
"y2k",
"birthday",
"spring",
"ocean",
"starlight"
];
function renderEmptyState(message) {
const emptyCard = document.createElement("article");
emptyCard.className = "card card--empty";
emptyCard.innerHTML = `
<p class="card-kicker">Hello Wall</p>
<h2>Start the wall</h2>
<p>${message}</p>
`;
grid.appendChild(emptyCard);
}
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function validateCard(card, fileName) {
if (!card || typeof card !== "object") {
throw new Error(`${fileName} is not a valid JSON object.`);
}
if (typeof card.title !== "string" || card.title.trim() === "") {
throw new Error(`${fileName} is missing a valid "title".`);
}
if (typeof card.text !== "string" || card.text.trim() === "") {
throw new Error(`${fileName} is missing a valid "text".`);
}
if (!allowedThemes.includes(card.theme)) {
throw new Error(
`${fileName} has an invalid theme. Use one of: ${allowedThemes.join(", ")}.`
);
}
}
function renderCards(cards) {
if (cards.length === 0) {
renderEmptyState(
'Add a JSON file inside <code>cards/</code>, list it in <code>cards/index.json</code>, and reload this page.'
);
return;
}
for (const card of cards) {
const article = document.createElement("article");
article.className = `card theme-${card.theme}`;
article.innerHTML = `
<div class="card-scene" aria-hidden="true">
<span class="corner-icon corner-icon--top"></span>
<span class="corner-icon corner-icon--bottom"></span>
</div>
<div class="card-body">
<p class="card-kicker">${escapeHtml(card.theme)}</p>
<h2>${escapeHtml(card.title)}</h2>
<p>${escapeHtml(card.text)}</p>
<footer>${escapeHtml(card.fileName)}</footer>
</div>
`;
grid.appendChild(article);
}
}
async function loadCards() {
const manifestResponse = await fetch("./cards/index.json", { cache: "no-store" });
if (!manifestResponse.ok) {
throw new Error("Could not load cards/index.json.");
}
const manifest = await manifestResponse.json();
if (!Array.isArray(manifest)) {
throw new Error("cards/index.json must be a JSON array of filenames.");
}
const cards = await Promise.all(
manifest.map(async (fileName) => {
if (typeof fileName !== "string" || !fileName.endsWith(".json")) {
throw new Error("cards/index.json can only contain .json filenames.");
}
const response = await fetch(`./cards/${encodeURIComponent(fileName)}`, {
cache: "no-store"
});
if (!response.ok) {
throw new Error(`Could not load card file: ${fileName}.`);
}
const card = await response.json();
validateCard(card, fileName);
return {
...card,
title: card.title.trim(),
text: card.text.trim(),
fileName
};
})
);
renderCards(cards);
}
loadCards().catch((error) => {
renderEmptyState(
`${escapeHtml(error.message)} If you opened this page directly from your filesystem, use a simple static server or GitHub Pages so <code>fetch()</code> can load the card files.`
);
});