-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
46 lines (36 loc) · 1.2 KB
/
init.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
// -- Function definitions -- //
// Handle raw attachment data input
async function onFetch(atts) {
let _data = atts.filter(a => a.filename === "upload/_data.zip");
if (_data.length !== 1) {
throw new Error(`Need _data.length === 1, found ${_data.length}`);
};
let data = await fetch(_data[0].url).then(r => r.arrayBuffer());
let uzdt = await unzip(data);
return uzdt;
};
// Helper to unzip a zip file
async function unzip(data) {
let out = new Object();
let jsz = new JSZip();
let zip = await jsz.loadAsync(data);
let files = zip.files;
for (let k in files) {
let content = await files[k].async("string");
out[k.replace(/\..+$/, '')] = JSON.parse(content.trimEnd());
};
return out;
};
// Initialise the view (create all necessary data)
function init(data) {
for (let k in data) API.createData(k, data[k]);
API.createData("pingSimulation", true); // Generate the chart on load
_fb.clearMessage();
return;
};
// -- Operations -- //
_fb.message("Please wait, loading attachments....");
// Initialise CouchAttachments instance
let couch = new CouchAttachments();
// Fetch attachments list, operate on list, then initialise view
couch.fetchList().then(onFetch).then(init);