-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
149 lines (127 loc) · 4.26 KB
/
index.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
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
'use strict';
const load_demo = require('./lib/loader.js');
const read_icon = require('./lib/icon_reader.js');
const DemoPlayer = require('./lib/player.js');
document.addEventListener("DOMContentLoaded", async function () {
let running = false;
let main = document.createElement("div");
main.className = "main"
let fileSelect = document.createElement("input");
fileSelect.className = "dropzone__input"
let button_open_demo = document.createElement("div");
let dropzone = document.createElement("div");
dropzone.className = "dropzone";
dropzone.innerHTML = "<div class='dropzone__thumb'><div>Drop here</div><div>demo file</div></div>"
fileSelect.addEventListener("input", () => {
button_open_demo.classList.add("open_demo__open");
if (fileSelect.files.length) {
updateFileName(dropzone, fileSelect.files[0]);
}
});
let error = document.createElement("div");
error.className = "error";
error.textContent = "ERROR";
fileSelect.type = "file";
button_open_demo.className = "open_demo";
button_open_demo.textContent = "Open demo from file";
button_open_demo.addEventListener("click", () => {
if (!fileSelect.files[0]) {
error_open(error, "No file selected");
return;
}
if (running) {
return;
}
let status_holder = document.createElement("h1");
status_holder.className = "text-center";
status_holder.textContent = "Loading...";
document.body.appendChild(status_holder);
let reader = new FileReader();
reader.onload = () => {
if (running) {
return;
}
let buf = reader.result;
running = true;
status_holder = document.createElement("h1");
status_holder.className = "text-center";
document.body.innerHTML = "";
document.body.appendChild(status_holder);
run_demo(buf, status_holder);
};
reader.readAsArrayBuffer(fileSelect.files[0]);
});
document.body.appendChild(main);
dropzone.appendChild(fileSelect);
main.appendChild(dropzone);
main.appendChild(button_open_demo);
document.body.appendChild(error)
document.querySelectorAll(".dropzone__input").forEach((inputElement) => {
const dropZoneElement = inputElement.closest(".dropzone");
dropZoneElement.addEventListener("click", (e) => {
inputElement.click();
});
inputElement.addEventListener("change", (e) => {
if (inputElement.files.length) {
updateFileName(dropZoneElement, inputElement.files[0]);
}
});
dropZoneElement.addEventListener("dragover", (e) => {
e.preventDefault();
dropZoneElement.classList.add("dropzone__over");
});
["dragleave", "dragend"].forEach((type) => {
dropZoneElement.addEventListener(type, (e) => {
dropZoneElement.classList.remove("dropzone__over");
});
});
dropZoneElement.addEventListener("drop", (e) => {
e.preventDefault();
button_open_demo.classList.add("open_demo__open");
if (e.dataTransfer.files.length) {
inputElement.files = e.dataTransfer.files;
updateFileName(dropZoneElement, e.dataTransfer.files[0]);
}
dropZoneElement.classList.remove("dropzone__over");
});
});
});
function error_open(e, text = "ERROR") {
e.classList.add("error__open");
e.textContent = text
setTimeout(function () { e.classList.remove("error__open"); }, 3800)
}
function updateFileName(dropZoneElement, file) {
let thumbnailElement = dropZoneElement.querySelector(".dropzone__thumb");
// First time - there is no thumbnail element, so lets create it
if (thumbnailElement) {
thumbnailElement.classList.add("dropzone__thumblabel")
thumbnailElement.dataset.label = file.name;
}
}
async function run_demo(buf, status_holder) {
status_holder.textContent = "Parsing demo file...";
let demo = await load_demo(buf);
console.log(demo);
status_holder.textContent = "Downloading icons...";
let icons = new Map();
let icon_promises = [];
let completed = 0;
for (let icon of demo.icons_used) {
icon_promises.push((async () => {
let url = "https://cdn.jsdelivr.net/gh/" + window.repository + "@" + demo.commit + "/" + icon;
console.log(url);
try {
let icon_obj = await read_icon(url);
icons.set(icon, icon_obj);
} catch (e) {
console.error(e);
} finally {
completed++;
status_holder.textContent = "Downloading icons..." + (completed * 100 / demo.icons_used.length).toFixed(1) + "%";
}
})());
}
console.log(icons);
window.demo_player = new DemoPlayer(demo, icons);
}