-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
107 lines (89 loc) · 3.88 KB
/
setup.js
File metadata and controls
107 lines (89 loc) · 3.88 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
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
import { PreviewBall, PreviewStar } from "./js/previewPlayer.js";
import { create_div } from "./js/utils.js";
export class Setup {
static saveSetup(props, once=false) {
let setupCount = 0;
while(localStorage.getItem(`Setup${setupCount}`)) setupCount++;
if(once) {
localStorage.setItem(`Setup${Number.MAX_SAFE_INTEGER}`, JSON.stringify(props));
return Number.MAX_SAFE_INTEGER
}
else if(setupCount < maxSetups) {
localStorage.setItem(`Setup${setupCount}`, JSON.stringify(props));
Setup.showSetups();
return setupCount
} else {
alert('All slots are occupied. Clear some slots before saving.')
}
}
static deleteSetup(setupNumber) {
localStorage.removeItem(`Setup${setupNumber}`);
}
static retrieveSetup(setupIndex) {
const props = localStorage.getItem(`Setup${setupIndex}`);
if(!props) {
window.alert('Setup not found!');
return
}
return JSON.parse(props)
}
static showSetups() {
const setupNameNodes = document.querySelectorAll('.setupName');
const playerNodes = document.querySelectorAll('.playerSetup');
const trailNodes = document.querySelectorAll('.trailSetup');
let setupCount = 0;
while(setupCount < maxSetups) {
if(localStorage.getItem(`Setup${setupCount}`)) {
const { setupName, playerProps, effectProps } = JSON.parse(localStorage.getItem(`Setup${setupCount}`));
setupNameNodes[setupCount].textContent = setupName;
setupNameNodes[setupCount].setAttribute('setupNumber', setupCount);
playerNodes[setupCount].textContent = JSON.stringify(playerProps, null, 1);
trailNodes[setupCount].textContent = JSON.stringify(effectProps, null, 1);
}
setupCount++;
}
}
static createSetupListComponent(setupNumber) {
const container = create_div('setupListItem');
const setupLabel = create_div('setupLabel');
setupLabel.textContent = `Setup ${setupNumber}`;
const setupInfo = create_div('setupInfo');
const setupName = create_div('setupName');
setupName.textContent = `Setup Name`;
const setupControls = create_div('setupControls');
const deleteBtn = document.createElement('button');
deleteBtn.classList.add('removeSetupBtn');
deleteBtn.textContent = '❌';
const setup = create_div('setup');
const playerSetup = create_div('playerSetup');
playerSetup.textContent = 'Empty Slot';
const trailSetup = create_div('trailSetup');
deleteBtn.addEventListener('click', e => {
const setupNumber = setupName.getAttribute('setupNumber');
if(setupNumber) {
Setup.deleteSetup(setupNumber);
setupName.removeAttribute('setupNumber');
setupName.textContent = `Setup Name`;
playerSetup.textContent = 'Empty Slot';
trailSetup.textContent = '';
}
});
setupControls.append(deleteBtn);
setupInfo.append(setupName, setupControls);
setup.append(playerSetup, trailSetup);
container.append(setupLabel, setupInfo, setup);
return { container }
}
static createSetupComponents(count) {
const setups = document.getElementById('savedSetups');
const slots = [];
for(let i = 1; i <= count; i++) {
const {container} = Setup.createSetupListComponent(i);
setups.appendChild(container);
slots.push(container);
}
}
}
const maxSetups = 10;
Setup.createSetupComponents(maxSetups);
Setup.showSetups();