-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (90 loc) · 2.76 KB
/
script.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
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
const BASE_URL = "https://raw.githubusercontent.com/JulianEgbert/bachelor-thesis-kepler.gl";
var config = "";
var loadedFilename = "";
var loadingHTML = "<h1> Loading </h1>";
const errorHTML = `<div class="error"> <a>❌</a><p> An error has occured! <p> <p>Try again or load another file.</p></div><style>
.error {
font-size: 120px;
width: 100%;
height: 200px;
text-align: center;
position: absolute;
top: 50%;
left: 0;
margin-top: -100px;
user-select: none;
}.error a {
text-shadow: 0 0 3px rgb(41, 50, 60);
}.error p {
color: white;
font-size: 3rem;
margin: 5px;
}</style>`;
function loadConfig() {
fetch(`${BASE_URL}/main/config.json`).then(function (response) {
return response.json();
}).then(configLoaded);
fetch(`${BASE_URL}/main/loading.html`).then(function (response) {
return response.text();
}).then(function (html) {
loadingHTML = html;
});
}
function configLoaded(newConfig) {
config = newConfig;
const selectEl = document.getElementById("fileSelect");
config.files.forEach((file) => {
const option = document.createElement("option");
option.value = file.path;
option.innerHTML = file.name;
selectEl.appendChild(option);
});
}
loadConfig();
function loadFromInput() {
const inputField = document.getElementById("fileSelect");
if (inputField.disabled || inputField.value == "") {
return;
}
const filename = inputField.value;
if (filename == loadedFilename)
return;
loadKeplerFromFilepath(filename);
loadedFilename = filename;
}
function loadKeplerFromFilepath(filepath) {
const url = `${config.baseUrl}${config.branch}/${filepath}`;
loadKeplerFromUrl(url);
}
function displayLoading() {
document.getElementById("kepler.gl-content").srcdoc = loadingHTML;
}
function displayLoadingError() {
const iframe = document.getElementById("kepler.gl-content");
iframe.srcdoc = errorHTML;
}
function handleFetchErrors(response) {
if (!response.ok) {
loadedFilename = "";
displayLoadingError();
throw Error(response);
}
return response;
}
function loadKeplerFromUrl(url) {
displayLoading();
fetch(url).then(handleFetchErrors).then(response => response.text()).then(function (html) {
document.getElementById("kepler.gl-content").srcdoc = parseHTML(html);
}).catch(function (e) {
console.error("An error occured while fetching the requested file.");
});
}
function parseHTML(html) {
// Inject a valid MAPBOX_TOKEN here.
if (!config || !config.mapboxToken) {
window.alert("No mapbox token provided in config file.");
return html;
}
html = html.replace("'PROVIDE_MAPBOX_TOKEN'", `'${config.mapboxToken}'`);
return html;
}