forked from simonw/datasette-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
261 lines (239 loc) · 9.16 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="/favicon.svg" />
<title>Datasette - Nasa wakeup calls: Music to wake up to</title>
<link rel="stylesheet" href="https://latest.datasette.io/-/static/app.css?cead5a">
<style>
#loading-indicator {
text-align: center;
}
#loading-indicator svg {
height: 20vh;
}
#loading-indicator textarea {
height: 40vh;
width: 90%;
padding: 1em;
background-color: #276890;
color: white;
border: 1px solid black;
overflow: auto;
outline: none;
resize: none;
opacity: 0.6;
font-size: min(3vw, 1.2em);
line-height: 1.8;
}
textarea#sql-editor {
padding: 0.5em;
font-size: 1em;
line-height: 1.4;
}
</style>
</head>
<body>
<div id="loading-indicator">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="200px" height="200px" viewBox="0 0 100 100">
<g transform="rotate(0 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(30 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(60 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(90 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(120 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(150 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(180 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(210 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(240 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(270 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(300 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(330 50 50)">
<rect x="47" y="24" rx="3" ry="6" width="6" height="12" fill="#292664">
<animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animate>
</rect>
</g>
</svg>
<textarea id="loading-logs">Loading...</textarea>
</div>
<script>
const datasetteWorker = new Worker("webworker.js");
const githubUrl = /^https:\/\/github.com\/(.*)\/(.*)\/blob\/(.*)(\?raw=true)?$/;
const gistUrl = /^https:\/\/gist.github.com\/(.*)\/(.*)$/;
function fixUrl(url) {
const githubMatch = githubUrl.exec(url);
if (githubMatch) {
return `https://raw.githubusercontent.com/${githubMatch[1]}/${githubMatch[2]}/${githubMatch[3]}`;
}
const gistMatch = gistUrl.exec(url);
if (gistMatch) {
return `https://gist.githubusercontent.com/${gistMatch[1]}/${gistMatch[2]}/raw`;
}
return url;
}
const urlParams = new URLSearchParams(location.search);
const initialUrl = fixUrl(urlParams.get('url'));
const csvUrls = urlParams.getAll('csv').map(fixUrl);
const sqlUrls = urlParams.getAll('sql').map(fixUrl);
const jsonUrls = urlParams.getAll('json').map(fixUrl);
const installUrls = urlParams.getAll('install');
datasetteWorker.postMessage({type: 'startup', initialUrl, csvUrls, sqlUrls, jsonUrls, installUrls});
let loadingLogs = ["Loading..."];
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
datasetteWorker.onmessage = (event) => {
var ta = document.getElementById('loading-logs');
if (event.data.type == 'log') {
loadingLogs.push(event.data.line);
ta.value = loadingLogs.join("\n");
ta.scrollTop = ta.scrollHeight;
return;
}
let html = '';
if (event.data.error) {
html = `<div style="padding: 0.5em"><h3>Error</h3><pre>${escapeHtml(event.data.error)}</pre></div>`;
} else if (/^text\/html/.exec(event.data.contentType)) {
html = event.data.text;
} else if (/^application\/json/.exec(event.data.contentType)) {
html = `<pre style="padding: 0.5em">${escapeHtml(JSON.stringify(JSON.parse(event.data.text), null, 4))}</pre>`;
} else {
html = `<pre style="padding: 0.5em">${escapeHtml(event.data.text)}</pre>`;
}
document.getElementById("output").innerHTML = html;
let title = document.getElementById("output").querySelector("title");
if (title) {
document.title = title.innerText;
}
window.scrollTo({top: 0, left: 0});
document.getElementById('loading-indicator').style.display = 'none';
};
</script>
<div id="output"></div>
<p style="padding: 1em">
<a href="/datasette?install=datasette-query-links&install=datasette-copyable&install=datasette-yaml&install=datasette-dateutil" ><button type="button" class="csv-url">Reset Nasa Wakeup Calls</button></a>
</p>
<p style="margin: 2em;">
<h4 style="margin: 2em">Links</h4>
<ul style="margin: 2em; padding-bottom:1em">
<li>Nasa Wake Up Calls: <a href="https://nasawakeupcalls.github.io/" target="_blank">website</a></li>
<li>More on SQL syntax at <a target="_blank" href="https://www.w3schools.com/sql/">w3schools</a></li>
<li>Datasette: <a style="padding-left: 0.3em; color: #067EFF" href="https://github.com/simonw/datasette-lite">documentation</a></li>
</ul>
</p>
<script>
window.onpopstate = function(event) {
console.log(event);
datasetteWorker.postMessage({path: event.target.location.hash.split("#")[1] || ""});
}
// Start with path from location.hash, if available
if (location.hash) {
datasetteWorker.postMessage({path: location.hash.replace(/^\#/, '')});
} else {
datasetteWorker.postMessage({path: "/"});
}
function isExternal(url) {
// Cast regex match to boolean true or false.
let startsWithProtocol = !!/^http:\/\/|https:\/\//.exec(url);
if (!startsWithProtocol) {
// If we don't start with the protoocl, return false.
//
// NB. Not sure where we would not reach this.
return false;
}
// Return false also if localhost without the protocol.
//
// Otherwise true for external.
//
return new URL(url).host != 'localhost';
}
function isFragmentLink(url) {
// Is this a #fragment on the current page?
let u = new URL(url);
return (
location.pathname == u.pathname &&
location.hostname == u.hostname &&
u.hash &&
u.hash != location.hash
);
}
function loadPath(path) {
path = path.split("#")[0].replace("http://localhost", "");
console.log({path});
history.pushState({path: path}, path, "#" + path);
datasetteWorker.postMessage({path});
}
let output = document.getElementById('output');
output.addEventListener('click', (ev => {
var link = ev.srcElement.closest("a");
if (link && link.href) {
ev.stopPropagation();
ev.preventDefault();
if (isFragmentLink(link.href)) {
// Jump them to that element, but don't update the URL bar
// since we use # in the URL to mean something else
let fragment = new URL(link.href).hash.replace("#", "");
if (fragment) {
let el = document.getElementById(fragment);
el.scrollIntoView();
}
return;
}
let href = link.getAttribute("href");
if (isExternal(href)) {
window.open(href);
return;
}
loadPath(href);
}
}), true);
output.addEventListener('submit', (ev => {
console.log(ev);
ev.stopPropagation();
ev.preventDefault();
if (ev.target && ev.target.nodeName == "FORM" && ev.target.method.toLowerCase() == "get") {
let qs = new URLSearchParams(new FormData(ev.target)).toString();
let action = ev.target.getAttribute("action");
loadPath(`${action}?${qs}`);
}
}), true);
</script>
</body>
</html>