-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDataAndLoadSelect2.js
72 lines (63 loc) · 2.24 KB
/
getDataAndLoadSelect2.js
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
// File: getDataAndLoadSelect2.js
import { queryBuilder } from './queryBuilder.js';
import { makeAjaxRequest } from './ajaxHelper.js';
import { formatData } from './dataFormatter.js';
import { initializeSelect2 } from './select2Helper.js';
import { createDevDropdown } from './createDevDropdown.js';
import { prepareAndStoreCorrespondenceData } from './sessionStorage.js';
import { endpointURL } from './globals.js';
createDevDropdown();
export function getDataAndLoadSelect2( callerId, family, correspondenceUri, version) {
if (callerId === "families") {
const query = queryBuilder(callerId, family);
getVersion(callerId, query, endpointURL);
} else if (callerId === "versions") {
const query = queryBuilder(callerId, family, correspondenceUri, version);
getConceptIDs(callerId, query, endpointURL);
}
}
async function getVersion(callerId, query, sparqlEndpoint) {
$("#spinner").show();
try {
const futureResponse = await new Promise((resolve, reject) => {
makeAjaxRequest(
`${sparqlEndpoint}?query=${encodeURIComponent(query)}`,
"GET",
{ Accept: "application/sparql-results+json" },
null,
(response) => resolve(response),
(error) => reject(error),
"getVersion function call from " + callerId
);
});
const formattedData = formatData(callerId, futureResponse);
prepareAndStoreCorrespondenceData(formattedData);
formattedData.unshift({ id: "", text: "" }); // Prepend an empty element to the array
initializeSelect2(callerId, formattedData); // Load formatted data into select2
} catch (error) {
console.error("Error executing query:", error);
} finally {
$("#spinner").hide(); // Ensure spinner is hidden at the end
}
}
export function getConceptIDs(callerId, query, endpointURL) {
$("#spinner").show();
makeAjaxRequest(
`${endpointURL}?query=${encodeURIComponent(query)}`,
'GET',
{
'Accept': 'application/sparql-results+json',
},
null,
function (data) {
const formattedData = formatData(callerId, data);
initializeSelect2(callerId, formattedData);
$('#spinner').hide();
},
function (jqXHR, textStatus, errorThrown) {
console.error('Error executing query:', errorThrown);
$('#spinner').hide();
},
"getConceptIDs function call from " + callerId
);
}