-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
236 lines (199 loc) · 5.84 KB
/
app.js
File metadata and controls
236 lines (199 loc) · 5.84 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const selectedTags = new Set();
let searchQuery = "";
function init() {
renderTotalCount();
renderTags();
renderCards();
}
function renderTotalCount() {
document.getElementById("total-count").textContent = linksData.length;
}
function getTagsWithCount() {
const tagCount = {};
for (const link of linksData) {
for (const tag of link.tags) {
tagCount[tag] = (tagCount[tag] || 0) + 1;
}
}
return Object.entries(tagCount).sort((a, b) => a[0].localeCompare(b[0]));
}
function renderTags() {
const container = document.getElementById("tags-filter");
container.innerHTML = "";
const tags = getTagsWithCount();
for (const [tag, count] of tags) {
const btn = document.createElement("button");
btn.className = "tag-btn";
btn.dataset.tag = tag;
btn.appendChild(document.createTextNode(tag + " "));
const countSpan = document.createElement("span");
countSpan.className = "tag-count";
countSpan.textContent = count;
btn.appendChild(countSpan);
btn.addEventListener("click", () => toggleTag(tag));
container.appendChild(btn);
}
}
function toggleTag(tag) {
if (selectedTags.has(tag)) {
selectedTags.delete(tag);
} else {
selectedTags.add(tag);
}
updateTagButtons();
renderCards();
}
function updateTagButtons() {
for (const btn of document.querySelectorAll(".tag-btn")) {
btn.classList.toggle("active", selectedTags.has(btn.dataset.tag));
}
}
function filterLinks() {
return linksData.filter((link) => {
// Tag filter (AND logic: must have all selected tags)
if (selectedTags.size > 0) {
const hasAllTags = [...selectedTags].every((tag) =>
link.tags.includes(tag),
);
if (!hasAllTags) return false;
}
// Search filter
if (searchQuery) {
const query = searchQuery.toLowerCase();
const searchable = [
link.title,
link.description,
link.note,
...link.tags,
...link.highlights,
]
.join(" ")
.toLowerCase();
if (!searchable.includes(query)) return false;
}
return true;
});
}
function createCardElement(link) {
const article = document.createElement("article");
article.className = "card";
// Card header
const header = document.createElement("div");
header.className = "card-header";
// Cover image or placeholder
if (link.cover) {
const img = document.createElement("img");
img.src = link.cover;
img.alt = link.title;
img.className = "card-cover";
img.loading = "lazy";
img.onerror = function () {
const placeholder = document.createElement("div");
placeholder.className = "card-cover-placeholder";
placeholder.textContent = link.title.charAt(0).toUpperCase();
this.replaceWith(placeholder);
};
header.appendChild(img);
} else {
const placeholder = document.createElement("div");
placeholder.className = "card-cover-placeholder";
placeholder.textContent = link.title.charAt(0).toUpperCase();
header.appendChild(placeholder);
}
// Title section
const titleSection = document.createElement("div");
titleSection.className = "card-title-section";
const h2 = document.createElement("h2");
h2.className = "card-title";
const titleLink = document.createElement("a");
titleLink.href = link.link;
titleLink.target = "_blank";
titleLink.rel = "noopener noreferrer";
titleLink.textContent = link.title;
h2.appendChild(titleLink);
titleSection.appendChild(h2);
const domainLink = document.createElement("a");
domainLink.className = "card-domain";
domainLink.href = link.link;
domainLink.target = "_blank";
domainLink.rel = "noopener noreferrer";
domainLink.textContent = link.link;
titleSection.appendChild(domainLink);
if (link.description) {
const desc = document.createElement("p");
desc.className = "card-description";
desc.textContent = link.description;
titleSection.appendChild(desc);
}
header.appendChild(titleSection);
article.appendChild(header);
// Tags
const tagsDiv = document.createElement("div");
tagsDiv.className = "card-tags";
for (const tag of link.tags) {
const tagSpan = document.createElement("span");
tagSpan.className = "card-tag" + (selectedTags.has(tag) ? " active" : "");
tagSpan.textContent = tag;
tagSpan.addEventListener("click", () => toggleTag(tag));
tagsDiv.appendChild(tagSpan);
}
article.appendChild(tagsDiv);
// Note
if (link.note) {
const noteDiv = document.createElement("div");
noteDiv.className = "card-note";
noteDiv.textContent = link.note;
article.appendChild(noteDiv);
}
// Highlights
if (link.highlights.length > 0) {
const highlightsDiv = document.createElement("div");
highlightsDiv.className = "card-highlights";
const toggle = document.createElement("span");
toggle.className = "highlights-toggle";
toggle.textContent = `Highlights (${link.highlights.length})`;
const ul = document.createElement("ul");
ul.className = "highlights-list";
for (const h of link.highlights) {
const li = document.createElement("li");
li.textContent = h;
ul.appendChild(li);
}
toggle.addEventListener("click", () => {
ul.classList.toggle("show");
});
highlightsDiv.appendChild(toggle);
highlightsDiv.appendChild(ul);
article.appendChild(highlightsDiv);
}
return article;
}
function renderCards() {
const container = document.getElementById("cards-grid");
const emptyState = document.getElementById("empty-state");
const filtered = filterLinks();
container.innerHTML = "";
if (filtered.length === 0) {
emptyState.style.display = "block";
} else {
for (const link of filtered) {
container.appendChild(createCardElement(link));
}
emptyState.style.display = "none";
}
}
// biome-ignore lint/correctness/noUnusedVariables: called from HTML onclick
function resetFilters() {
selectedTags.clear();
searchQuery = "";
document.getElementById("search").value = "";
updateTagButtons();
renderCards();
}
// Search handler
document.getElementById("search").addEventListener("input", (e) => {
searchQuery = e.target.value.trim();
renderCards();
});
// Initialize
init();