-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
95 lines (79 loc) · 2.95 KB
/
script.js
File metadata and controls
95 lines (79 loc) · 2.95 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
// Fetch the manifest and render the site
async function loadNotes() {
const res = await fetch('notes.json');
const data = await res.json();
const subjectFilter = document.getElementById('subjectFilter');
const subjectsContainer = document.getElementById('subjects');
const searchInput = document.getElementById('search');
// populate filter
data.subjects.forEach(s => {
const opt = document.createElement('option');
opt.value = s.id;
opt.textContent = s.title;
subjectFilter.appendChild(opt);
});
function render(filter = 'all', query = '') {
subjectsContainer.innerHTML = '';
const q = query.trim().toLowerCase();
const filtered = data.subjects
.filter(s => filter === 'all' ? true : s.id === filter)
.map(s => {
// filter notes by search query if there is one
const notes = s.notes.filter(n => {
if (!q) return true;
return (n.name || '').toLowerCase().includes(q) || (s.title || '').toLowerCase().includes(q);
});
return {...s, notes};
})
.filter(s => s.notes.length > 0);
if (filtered.length === 0) {
const el = document.createElement('div');
el.className = 'card empty';
el.innerHTML = `<div class="empty">No notes found.</div>`;
subjectsContainer.appendChild(el);
return;
}
filtered.forEach(s => {
const card = document.createElement('div');
card.className = 'card';
const h = document.createElement('h2');
h.textContent = s.title;
card.appendChild(h);
const list = document.createElement('div');
list.className = 'note-list';
s.notes.forEach(n => {
const note = document.createElement('div');
note.className = 'note';
const meta = document.createElement('div');
meta.className = 'meta';
const name = document.createElement('div');
name.textContent = n.name || n.file.split('/').pop();
const ext = (n.file.split('.').pop() || '').toUpperCase();
const badge = document.createElement('span');
badge.className = 'badge';
badge.textContent = ext;
meta.appendChild(name);
meta.appendChild(badge);
const actions = document.createElement('div');
// direct download link
const a = document.createElement('a');
a.className = 'download';
a.href = n.file;
a.setAttribute('download', '');
a.textContent = 'Download';
actions.appendChild(a);
note.appendChild(meta);
note.appendChild(actions);
list.appendChild(note);
});
card.appendChild(list);
subjectsContainer.appendChild(card);
});
}
// initial render
render();
// attach events
subjectFilter.addEventListener('change', () => render(subjectFilter.value, searchInput.value));
searchInput.addEventListener('input', () => render(subjectFilter.value, searchInput.value));
}
document.addEventListener('DOMContentLoaded', loadNotes);