-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
193 lines (178 loc) · 6.06 KB
/
Copy pathindex.html
File metadata and controls
193 lines (178 loc) · 6.06 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>App Index</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<main class="container">
<header class="hero">
<div>
<p class="eyebrow">Home</p>
<h1>App Index</h1>
<p class="subtitle">
Browse the apps below. Data is loaded directly from the
<code>app_index.csv</code> file.
</p>
</div>
<div class="controls">
<label class="search">
<span>Search</span>
<div class="search-input">
<input
type="search"
id="search"
placeholder="Filter by name, tags, or description"
/>
<button type="button" class="clear" id="clear-search">Clear</button>
</div>
</label>
<label class="sort">
<span>Sort by</span>
<select id="sort">
<option value="index">Index</option>
<option value="name">Name</option>
</select>
</label>
</div>
</header>
<section>
<div class="status" id="status" role="status" aria-live="polite">Loading app index…</div>
<div class="grid" id="app-grid" hidden></div>
</section>
</main>
<template id="card-template">
<article class="card">
<div class="card-head">
<span class="index"></span>
<h2 class="name"></h2>
</div>
<p class="description"></p>
<div class="tags"></div>
<a class="link" target="_blank" rel="noreferrer noopener">Visit app</a>
</article>
</template>
<script>
const statusEl = document.getElementById('status');
const gridEl = document.getElementById('app-grid');
const searchEl = document.getElementById('search');
const clearEl = document.getElementById('clear-search');
const sortEl = document.getElementById('sort');
const template = document.getElementById('card-template');
const parseCsvLine = (line) => {
const values = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i += 1) {
const char = line[i];
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"';
i += 1;
} else {
inQuotes = !inQuotes;
}
continue;
}
if (char === ',' && !inQuotes) {
values.push(current);
current = '';
continue;
}
current += char;
}
values.push(current);
return values.map((value) => value.trim());
};
const parseCsv = (csvText) => {
const lines = csvText.trim().split(/\r?\n/);
if (lines.length === 0) return [];
const headers = parseCsvLine(lines[0]);
return lines.slice(1).map((line) => {
const values = parseCsvLine(line);
return headers.reduce((row, header, index) => {
row[header] = values[index] ?? '';
return row;
}, {});
});
};
const buildCard = (entry) => {
const clone = template.content.cloneNode(true);
clone.querySelector('.index').textContent = `#${entry.index}`;
clone.querySelector('.name').textContent = entry.name;
clone.querySelector('.description').textContent = entry.description;
const tagsEl = clone.querySelector('.tags');
entry.tags
.split('|')
.map((tag) => tag.trim())
.filter(Boolean)
.forEach((tag) => {
const span = document.createElement('span');
span.textContent = tag;
tagsEl.appendChild(span);
});
const linkEl = clone.querySelector('.link');
linkEl.href = entry.link;
linkEl.textContent = `Open ${entry.name}`;
return clone;
};
const render = (entries, total) => {
gridEl.innerHTML = '';
if (entries.length === 0) {
statusEl.textContent = total === 0 ? 'No apps are available.' : 'No apps match your search.';
gridEl.hidden = true;
return;
}
statusEl.textContent = `Showing ${entries.length} of ${total} app${
total === 1 ? '' : 's'
}.`;
gridEl.hidden = false;
entries.forEach((entry) => {
gridEl.appendChild(buildCard(entry));
});
};
const loadApps = async () => {
try {
const response = await fetch('app_index.csv');
if (!response.ok) {
throw new Error('Unable to load app_index.csv');
}
const csvText = await response.text();
const entries = parseCsv(csvText);
const normalized = entries.map((entry) => ({
...entry,
haystack: `${entry.name} ${entry.description} ${entry.tags}`.toLowerCase(),
}));
const update = () => {
const query = searchEl.value.trim().toLowerCase();
const filtered = query
? normalized.filter((entry) => entry.haystack.includes(query))
: normalized;
const sorted = [...filtered].sort((a, b) => {
if (sortEl.value === 'name') {
return a.name.localeCompare(b.name);
}
return Number(a.index) - Number(b.index);
});
clearEl.disabled = query.length === 0;
render(sorted, normalized.length);
};
searchEl.addEventListener('input', update);
sortEl.addEventListener('change', update);
clearEl.addEventListener('click', () => {
searchEl.value = '';
searchEl.focus();
update();
});
update();
} catch (error) {
statusEl.textContent = error.message;
gridEl.hidden = true;
}
};
loadApps();
</script>
</body>
</html>