This repository was archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.ts
61 lines (55 loc) · 2.11 KB
/
menu.ts
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
import { clearStats, getStats, importStats } from './stats';
export function menu(
button: HTMLElement,
menuDialog: HTMLDialogElement,
statsDialog: HTMLDialogElement,
aboutDialog: HTMLDialogElement,
) {
menuDialog.querySelector('.stats-link')!.addEventListener('click', () => {
menuDialog.close();
statsDialog.showModal();
});
statsDialog.querySelector('.clear')!.addEventListener('click', () => {
if (confirm('are you sure?')) {
clearStats();
alert('stats cleared!');
// TODO: need to clear current game state
statsDialog.close();
}
});
const exportLink = menuDialog.querySelector('.export-link a')!;
const importFile: HTMLInputElement = menuDialog.querySelector('.import-link input')!;
importFile.addEventListener('click', (e) => e.stopPropagation());
importFile.addEventListener('change', () => {
if (!importFile.files) {
return;
}
const [file] = importFile.files;
if (file) {
const reader = new FileReader();
reader.addEventListener('load', () => {
const contents = (reader.result || '').toString();
try {
importStats(contents);
} catch (e) {
alert(`error importing stats: ${e}`);
return;
}
// TODO: need to clear the current game state
alert('statistics imported successfully!');
menuDialog.close();
});
reader.readAsText(file);
}
});
menuDialog.querySelector('.import-link')!.addEventListener('click', () => importFile.click());
menuDialog.querySelector('.about-link')!.addEventListener('click', () => {
menuDialog.close();
aboutDialog.showModal();
});
button.addEventListener('click', () => {
menuDialog.showModal()
const blob = new Blob([JSON.stringify(getStats())], {type: 'application/json'});
exportLink.setAttribute('href', URL.createObjectURL(blob));
});
}