forked from srophe/srophe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.js
More file actions
71 lines (60 loc) · 2.07 KB
/
list.js
File metadata and controls
71 lines (60 loc) · 2.07 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
export async function renderKeywordPrettyList(query, listId, containerId, labelField = "label", valueField = "value", onSelect) {
const listEl = document.getElementById(listId);
const container = document.getElementById(containerId); // Make scroll listener flexible
const pageSize = 50;
let offset = 0;
let loading = false;
let endReached = false;
const SPARQL_ENDPOINT = "https://sparql.vanderbilt.edu/sparql";
function prettify(uri) {
const raw = uri.split('/').pop();
return raw
.replace(/[-_]/g, ' ')
.replace(/\b\w/g, c => c.toUpperCase());
}
async function loadNextPage() {
if (loading || endReached) return;
loading = true;
const pagedQuery = `${query}\nOFFSET ${offset}\nLIMIT ${pageSize}`;
try {
const res = await fetch(`${SPARQL_ENDPOINT}?query=${encodeURIComponent(pagedQuery)}`, {
headers: { Accept: 'application/sparql-results+json' }
});
const data = await res.json();
console.log("Keyword list data:", data);
const results = data.results.bindings;
if (results.length === 0) {
endReached = true;
return;
}
results.forEach(binding => {
const uri = binding[valueField]?.value || "";
const prettyLabel = prettify(uri);
const li = document.createElement("li");
li.style.marginBottom = "0.75rem";
li.style.fontFamily = "Georgia, serif";
li.style.fontSize = ".78rem";
li.style.cursor = "pointer";
li.style.padding = "0.5rem";
li.style.borderBottom = "1px solid #ddd";
li.textContent = prettyLabel;
li.addEventListener("click", () => {
onSelect(uri);
});
listEl.appendChild(li);
});
offset += pageSize;
} catch (err) {
console.error("Failed to load keywords:", err);
} finally {
loading = false;
}
}
loadNextPage();
container.addEventListener("scroll", () => {
const threshold = container.scrollHeight - container.clientHeight - 50;
if (container.scrollTop >= threshold) {
loadNextPage();
}
});
}