-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectWorldPage.js
More file actions
63 lines (59 loc) · 2.49 KB
/
SelectWorldPage.js
File metadata and controls
63 lines (59 loc) · 2.49 KB
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
import { Page, pm } from "./Page.js";
class SelectWorldPage extends Page {
static get outdegree() { return ["welcome", "play", "create-new-world", ]; };
constructor() {
super();
this.worldList = this.shadowRoot.getElementById("world-list");
this.btnSelect = this.shadowRoot.getElementById("btn-select");
this.btnDel = this.shadowRoot.getElementById("btn-del");
this.selectedWordStorageId = null;
this.btnSelect.addEventListener("click", this.onBtnSelectClick);
this.btnDel.addEventListener("click", this.onBtnDelClick);
};
refreshList() {
this.selectedWordStorageId = null;
this.btnSelect.disabled = true;
this.btnDel.disabled = true;
const timestamp2str = ts => (new Date(ts)).toLocaleString();
this.worldList.innerHTML =
Object.entries(JSON.parse(localStorage.getItem("worlds") || "{}"))
.sort((a, b) => b[1].modifyAt - a[1].modifyAt)
.reduce((str, [storageId, world]) => str + `
<li>
<span class="world-name">${world.name}</span>
<span class="create-at">Created - ${timestamp2str(world.createAt)}</span>
<span class="modify-at">Modified - ${timestamp2str(world.modifyAt)}</span>
<span class="world-mode">${world.type}</span>
<span class="storageId">${storageId}</span>
</li>
`, "");
this.worldList.querySelectorAll("li").forEach(li => li.onclick = () => this.onLiClick(li));
};
onConnected() {
this.refreshList();
};
onLiClick(li) {
this.selectedWordStorageId = li.querySelector(".storageId").innerHTML;
this.worldList.querySelector("li.selected")?.classList.remove("selected");
li.classList.add("selected");
this.btnSelect.disabled = false;
this.btnDel.disabled = false;
};
onBtnSelectClick = () => {
pm.openPageByID("play", { storageId: this.selectedWordStorageId, });
};
onBtnDelClick = () => {
const worlds = JSON.parse(localStorage.getItem("worlds") || "{}");
delete worlds[this.selectedWordStorageId];
localStorage.setItem("worlds", JSON.stringify(worlds));
this.refreshList();
};
onHistoryBack() { this.close(); };
onTransitioned(from, to, eventName, fromPage, toPage, ...data) {
if (to == "play") this.close();
};
};
SelectWorldPage.asyncLoadAndDefine();
export {
SelectWorldPage,
};