-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.js
More file actions
305 lines (236 loc) · 7.83 KB
/
peer.js
File metadata and controls
305 lines (236 loc) · 7.83 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
let publications = [];
let filtered = [];
const resultsList = document.getElementById("results-list");
const searchInput = document.getElementById("search-input");
const yearFilter = document.getElementById("year-filter");
const journalFilter = document.getElementById("journal-filter");
const keywordFilter = document.getElementById("keyword-filter");
const clearBtn = document.getElementById("clear-filters");
const resultsCount = document.getElementById("results-count");
const paginationContainer = document.getElementById("pagination");
const RESULTS_PER_PAGE = 12;
let currentPage = 1;
// --------------------------------------------------
// FETCH AND PARSE CSV
// --------------------------------------------------
function parseCSV(csvText) {
const lines = csvText.split('\n').filter(line => line.trim());
const headers = lines[0].split(',').map(h => h.replace(/"/g, '').trim());
const rows = lines.slice(1).map(line => {
const values = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
values.push(current.replace(/"/g, '').trim());
current = '';
} else {
current += char;
}
}
values.push(current.replace(/"/g, '').trim());
return values;
});
return rows.map(row => {
const obj = {};
headers.forEach((header, i) => {
obj[header.toLowerCase()] = row[i] || '';
});
return obj;
});
}
fetch("peer.csv")
.then(res => res.text())
.then(csvText => {
const data = parseCSV(csvText);
publications = data.map(p => ({
title: p.title,
authors: p.authors,
year: parseInt(p.year) || 0,
date: p.date,
link: p.link,
journal: p.journal,
doi: p.doi,
keywords: p.keywords ? p.keywords.split(';').map(k => k.trim()).filter(k => k) : []
}));
filtered = publications;
populateFilters();
applyFilters();
});
// --------------------------------------------------
// SEARCH INDEX
// --------------------------------------------------
function buildSearchIndex(pub) {
return [
pub.title,
pub.authors,
pub.journal,
...pub.keywords
]
.filter(Boolean)
.join(" ")
.toLowerCase();
}
// --------------------------------------------------
// FILTER POPULATION
// --------------------------------------------------
function populateFilters() {
const years = [...new Set(publications.map(p => p.year).filter(Boolean))]
.sort((a,b)=>b-a);
const journals = [...new Set(publications.map(p => p.journal).filter(Boolean))].sort();
years.forEach(year => {
yearFilter.appendChild(new Option(year, year));
});
journals.forEach(journal => {
journalFilter.appendChild(new Option(journal, journal));
});
}
// --------------------------------------------------
// FILTERING
// --------------------------------------------------
function applyFilters() {
const search = searchInput.value.toLowerCase().trim();
const year = yearFilter.value;
const journal = journalFilter.value;
const keywordQuery = keywordFilter.value.toLowerCase().trim();
filtered = publications.filter(pub => {
const index = buildSearchIndex(pub);
const matchesSearch =
!search ||
search.split(" ").every(token =>
index.includes(token)
);
const matchesYear =
!year || String(pub.year) === year;
const matchesJournal =
!journal || pub.journal === journal;
const matchesKeyword =
!keywordQuery ||
pub.keywords.some(k =>
k.toLowerCase().includes(keywordQuery)
);
return (
matchesSearch &&
matchesYear &&
matchesJournal &&
matchesKeyword
);
});
currentPage = 1;
render();
}
// --------------------------------------------------
// PAGINATION
// --------------------------------------------------
function renderPagination(totalPages) {
paginationContainer.innerHTML = "";
if (totalPages <= 1) return;
const MAX_VISIBLE = 10;
// Determine current page group (1–10, 11–20, etc.)
const groupStart = Math.floor((currentPage - 1) / MAX_VISIBLE) * MAX_VISIBLE + 1;
const groupEnd = Math.min(groupStart + MAX_VISIBLE - 1, totalPages);
// Helper to create button
function createButton(label, page, isActive = false) {
const btn = document.createElement("button");
btn.textContent = label;
btn.className = isActive ? "active-page" : "";
btn.setAttribute("aria-label", `Go to page ${page}`);
btn.addEventListener("click", () => {
currentPage = page;
render();
window.scrollTo({ top: 0, behavior: "smooth" });
});
return btn;
}
// Previous group button
if (groupStart > 1) {
const prevGroupBtn = createButton("« Prev 10", groupStart - 1);
paginationContainer.appendChild(prevGroupBtn);
}
// Page numbers within current group
for (let i = groupStart; i <= groupEnd; i++) {
paginationContainer.appendChild(
createButton(i, i, i === currentPage)
);
}
// Ellipsis + jump to last page
if (groupEnd < totalPages) {
const ellipsis = document.createElement("span");
ellipsis.textContent = "…";
ellipsis.style.padding = "0.6rem 0.75rem";
paginationContainer.appendChild(ellipsis);
const lastBtn = createButton("Last", totalPages);
paginationContainer.appendChild(lastBtn);
const nextGroupBtn = createButton("Next 10 »", groupEnd + 1);
paginationContainer.appendChild(nextGroupBtn);
}
}
// --------------------------------------------------
// RENDER
// --------------------------------------------------
function render() {
resultsList.innerHTML = "";
const total = filtered.length;
const totalPages = Math.ceil(total / RESULTS_PER_PAGE);
const start = (currentPage - 1) * RESULTS_PER_PAGE;
const end = start + RESULTS_PER_PAGE;
const pageResults = filtered.slice(start, end);
const startNum = total === 0 ? 0 : start + 1;
const endNum = Math.min(end, total);
resultsCount.textContent =
total === 0
? "No publications found"
: `Showing ${startNum}–${endNum} of ${total} article${total !== 1 ? "s" : ""} published in peer-reviewed journals since 2000.`;
pageResults.forEach(pub => {
const li = document.createElement("li");
li.className = "result-item";
let href = pub.link;
if (!href) {
const encodedTitle = encodeURIComponent(pub.title);
href = `https://scholar.google.com/scholar?hl=en&as_sdt=0%2C33&q=${encodedTitle}&btnG=`;
}
const titleHTML = href ? `<a href="${href}" target="_blank" rel="noopener">${pub.title}</a>` : pub.title;
li.innerHTML = `
<h3>${titleHTML}</h3>
<p><strong>${pub.journal ? pub.journal : ""}</strong></p>
<p>${pub.authors}
${pub.year ? pub.year : ""}
</p>
${pub.date ? `<p>Published: ${pub.date}</p>` : ""}
<div>
${pub.keywords.map(k =>
`<span class="badge">${k}</span>`
).join("")}
</div>
`;
resultsList.appendChild(li);
});
renderPagination(totalPages);
}
// --------------------------------------------------
// UTIL
// --------------------------------------------------
function debounce(fn, delay = 250) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
// --------------------------------------------------
// EVENT LISTENERS
// --------------------------------------------------
searchInput.addEventListener("input", debounce(applyFilters));
keywordFilter.addEventListener("input", debounce(applyFilters));
yearFilter.addEventListener("change", applyFilters);
journalFilter.addEventListener("change", applyFilters);
clearBtn.addEventListener("click", () => {
searchInput.value = "";
yearFilter.value = "";
journalFilter.value = "";
keywordFilter.value = "";
applyFilters();
});