-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-data.js
More file actions
244 lines (205 loc) · 5.54 KB
/
load-data.js
File metadata and controls
244 lines (205 loc) · 5.54 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
import * as d3 from "d3";
let style;
export function showLoadingIcon(element) {
if (element.offsetHeight < 30) {
d3.select(element).transition().duration(100).style("min-height", "30px");
}
const css = (key) => getComputedStyle(element).getPropertyValue(key);
const spinnerBgColor = css("--togostanza-loading_spinner-bg_color");
const spinnerColor = css("--togostanza-loading_spinner-color");
style = document.createElement("style");
style.setAttribute("id", "spinner-css");
style.innerHTML = getSpinnerCss(
spinnerBgColor || "rgba(0,0,0,0.2)",
spinnerColor || "#fff"
);
element.getRootNode().appendChild(style);
const container = d3
.select(element)
.append("div")
.classed("metastanza-loading-icon-div", true)
.attr("id", "metastanza-loading-icon-div");
const wrap = container.append("div").classed("spinner-wrap", true);
const circle = wrap.append("div").classed("circle", true);
const spinner = circle
.append("div")
.classed("spinner", true)
.attr("style", "--count: 12");
for (let i = 0; i < 12; i++) {
spinner.append("span").attr("style", `--index: ${i}`);
}
}
export function hideLoadingIcon(element) {
style?.remove();
d3.select(element).select("#metastanza-loading-icon-div").remove();
}
function displayApiError(element, error) {
d3.select(element).select(".metastanza-error-message-div").remove();
const p = d3
.select(element)
.append("div")
.attr("class", "metastanza-error-message-div")
.append("p")
.attr("class", "metastanza-error-message");
p.append("span").text("MetaStanza API error");
p.append("br");
p.append("span").text(error);
}
function withAcceptHeader(fetcher, accept) {
return (url, requestInit) => {
const requestInitWithHeader = {
headers: {
Accept: accept,
},
...requestInit,
};
return fetcher(url, requestInitWithHeader);
};
}
function loadJSON(url, requestInit) {
return fetch(url, requestInit).then((res) => res.json());
}
function sparql2table(json) {
const head = json.head.vars;
const data = json.results.bindings;
return data.map((item) => {
const row = {};
head.forEach((key) => {
row[key] = item[key] ? item[key].value : "";
});
return row;
});
}
async function loadSPARQL(url, requestInit) {
const json = await fetch(url, requestInit).then((res) => res.json());
return sparql2table(json);
}
async function loadElasticsearch(url, requestInit) {
const json = await fetch(url, requestInit).then((res) => res.json());
return json.hits.hits.map((hit) => hit._source);
}
function getLoader(type) {
switch (type) {
case "text":
return withAcceptHeader(d3.text, "text/plain");
case "tsv":
return withAcceptHeader(d3.tsv, "text/tab-separated-values");
case "csv":
return withAcceptHeader(d3.csv, "text/csv");
case "sparql-results-json":
return withAcceptHeader(loadSPARQL, "application/sparql-results+json");
case "elasticsearch":
return withAcceptHeader(loadElasticsearch, "application/json");
case "json":
default:
return withAcceptHeader(loadJSON, "application/json");
}
}
let cache = null;
let cacheKey = null;
export default async function loadData(
url,
type = "json",
mainElement = null,
timeout = 10 * 60 * 1000,
limit = null,
offset = null
) {
const _cacheKey = JSON.stringify({ url, type, limit, offset });
if (cacheKey === _cacheKey) {
return cache;
}
const u = new URL(url);
if (limit) {
u.searchParams.set(type === "elasticsearch" ? "size" : "limit", limit);
}
if (offset) {
u.searchParams.set(type === "elasticsearch" ? "from" : "offset", offset);
}
const loader = getLoader(type);
let data = null;
const controller = new AbortController();
const requestInit = {
signal: controller.signal,
};
const timer = setTimeout(() => {
controller.abort();
}, timeout);
try {
if (mainElement) {
showLoadingIcon(mainElement);
}
data = await loader(u, requestInit);
addTogostanzaId(data);
cache = data;
cacheKey = _cacheKey;
} catch (error) {
if (mainElement) {
const detail =
error.name === "AbortError"
? "Error: Request timed out."
: error.toString();
displayApiError(mainElement, detail);
}
throw error;
} finally {
if (mainElement) {
hideLoadingIcon(mainElement);
}
clearTimeout(timer);
}
return data;
}
function addTogostanzaId(data) {
if (Array.isArray(data)) {
data.forEach((d, i) => {
d.__togostanza_id__ = i;
});
}
}
function getSpinnerCss(bgColor, spinnerColor) {
return `
:host {
--loading_spinner_bg_color: ${bgColor};
--loading_spinner_color: ${spinnerColor};
}
.metastanza-loading-icon-div {
display: flex;
align-items: center;
height: 150px;
}
.spinner-wrap {
position: absolute;
left: 50%;
}
.circle {
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: var(--loading_spinner_bg_color);
transform: translate(-50%, -50%);
}
.spinner {
position: absolute;
inset: 50%;
animation: spin 2s steps(var(--count), end) infinite;
}
.spinner span {
position: absolute;
height: 2px;
width: 4px;
top: -1px;
left: -2px;
background-color: var(--loading_spinner_color);
border-radius: 1.5px;
transform: rotate(calc(var(--index) * 30deg)) translateX(6px) scaleY(0.5);
opacity: calc(var(--index) / var(--count));
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
`;
}