-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathwith-ui.html
259 lines (203 loc) · 9.25 KB
/
with-ui.html
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../dist/brackets-viewer.min.css" />
<script type="text/javascript" src="../dist/brackets-viewer.min.js"></script>
<title>Demo with UI</title>
<!-- You can choose a default theme or make you own. -->
<link rel="stylesheet" href="themes/light.css" />
<style>
#input-mask {
position: absolute;
left: 50%;
margin-left: -150px;
height: 190px;
top: 50%;
margin-top: -95px;
background: #9e9e9e;
box-shadow: #14191f;
border-radius: 5px;
z-index: 1;
display: none;
justify-content: center;
align-items: center;
}
#input-mask div {
text-align: center;
background: lightgrey;
padding: 10px 20px;
}
#createNewBracket>div {
display: grid;
grid-template-columns: 1fr 2fr;
margin: 5px;
align-items: center;
justify-content: center;
}
#createNewBracket>div:last-child {
grid-template-columns: 1fr;
}
</style>
</head>
<!-- TODO: rename "mask" to form -->
<!-- TODO: Autofill the form with scores when opening it -->
<!-- TODO: Use new FormData(document.querySelector('form')); -->
<body style="background: gray;">
<div id="updateCurrentBracket"></div>
<!-- This div will be used as the root for the library. It must be **perfectly** empty to prevent a FOUC. -->
<div id="bracketsViewerExample" class="brackets-viewer"></div>
<div id="createNewBracket"></div>
<script type="text/javascript" src="../dist/stage-form-creator.min.js"></script>
<div id="input-mask">
<div>
<h3></h3>
<label id="opponent1-label" for="opponent1">Opponent 1: </label><input type="number" id="opponent1"><br>
<label id="opponent2-label" for="opponent2">Opponent 2: </label><input type="number" id="opponent2"><br>
<button id="input-submit">Submit</button>
</div>
</div>
<script>
const BRACKETS = 'brackets';
const INPUT_MASK = 'input-mask';
const INPUT_SUBMIT = 'input-submit';
const OPPONENT1 = 'opponent1';
const OPPONENT2 = 'opponent2';
const ELEMENT_ID = 'bracketsViewerExample';
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
const config = {
parent_id: 'createNewBracket',
html_name_id: 'name',
html_stage_type_selector_id: 'selector',
html_team_names_input_id: 'teams',
html_team_count_input_id: 'team-count',
html_group_id: 'groups',
html_seed_order_id: 'seeds',
html_round_robin_mode_id: 'round-robin-mode',
html_consolation_final_checkbox_id: 'consolation_final',
html_skip_first_round_checkbox_id: 'skip_first',
html_grand_final_type_id: 'grand_final',
html_double_elimination_seed_textarea_id: 'double_elimination_seeds',
group_default_size: 1
}
function listBrackets() {
const storageData = JSON.parse(localStorage.getItem(BRACKETS)) || {};
const bracketsList = document.createElement('ul');
for (const [key, value] of Object.entries(storageData)) {
const bracketItem = document.createElement('li');
const bracketLink = document.createElement('a');
bracketLink.href = '?id=' + key;
bracketLink.innerText = 'Go to ID: ' + key;
bracketItem.appendChild(bracketLink);
bracketsList.appendChild(bracketItem);
}
document.body.insertBefore(bracketsList, document.getElementById(ELEMENT_ID));
}
function loadData(id) {
const bracketsStore = JSON.parse(localStorage.getItem(BRACKETS));
if (null === bracketsStore || !(id in bracketsStore)) {
alert('Key is not found in data!');
window.location.search = '?id'
return;
}
return bracketsStore[id];
}
function renderBracket(data) {
document.getElementById(ELEMENT_ID).innerHTML = '';
window.bracketsViewer.render({
stages: data.stage,
matches: data.match,
matchGames: data.match_game,
participants: data.participant,
}, {
selector: '#' + ELEMENT_ID,
participantOriginPlacement: 'before',
separatedChildCountLabel: true,
showSlotsOrigin: true,
showLowerBracketSlotsOrigin: true,
highlightParticipantOnHover: true,
});
}
if (id === null || id === '') {
listBrackets();
window.stageFormCreator(config, async (config) => {
await window.bracketsManager.create.stage(config)
const rawStoredBrackets = localStorage.getItem(BRACKETS);
if (null === rawStoredBrackets || '' === rawStoredBrackets) {
localStorage.setItem(BRACKETS, JSON.stringify({
0: window.inMemoryDatabase.data,
}));
window.location.href = '?id=0';
}
let storedBrackets = JSON.parse(rawStoredBrackets);
console.log(storedBrackets);
let index = Object.keys(storedBrackets).length;
storedBrackets[index] = window.inMemoryDatabase.data;
localStorage.setItem(BRACKETS, JSON.stringify(storedBrackets));
window.location.href = '?id=' + index;
});
} else {
const backLink = document.createElement('a');
backLink.href = '?id';
backLink.innerText = 'Go back';
document.body.insertBefore(backLink, document.getElementById('updateCurrentBracket'));
const deleteButton = document.createElement('a');
deleteButton.href = '?id';
deleteButton.innerText = 'Delete';
document.body.insertBefore(deleteButton, document.getElementById('updateCurrentBracket'));
deleteButton.addEventListener('click', async (e) => {
const bracketsStore = JSON.parse(localStorage.getItem(BRACKETS));
delete bracketsStore[id];
localStorage.setItem(BRACKETS, JSON.stringify(bracketsStore));
});
window.updateFormCreator({ ...config, parent_id: 'updateCurrentBracket' }, async (config) => {
await window.bracketsManager.create.stage(config)
const rawStoredBrackets = localStorage.getItem(BRACKETS);
let storedBrackets = JSON.parse(rawStoredBrackets);
console.log(storedBrackets);
// Overwrite the bracket
let index = Object.keys(storedBrackets).length - 1;
storedBrackets[index] = window.inMemoryDatabase.data;
localStorage.setItem(BRACKETS, JSON.stringify(storedBrackets));
// Reload the page
window.location.href = '?id=' + index;
});
window.bracketsViewer.onMatchClicked = async (match) => {
const inputMask = document.getElementById(INPUT_MASK);
inputMask.style.display = 'flex';
const matchTitle = document.querySelector(`[data-match-id="${match.id}"] .opponents > span`).textContent;
inputMask.querySelector('h3').innerText = matchTitle;
const enterKeyListener = (event) => {
if (event.key === 'Enter') {
updateMatch()
}
}
document.addEventListener('keypress', enterKeyListener)
const updateMatch = async () => {
const opponent1 = parseInt(document.getElementById(OPPONENT1).value);
const opponent2 = parseInt(document.getElementById(OPPONENT2).value);
const data = loadData(id);
await window.bracketsManager.import(data);
await window.bracketsManager.update.match({
id: match.id,
status: 4,
opponent1: { score: opponent1 },
opponent2: { score: opponent2 },
});
const newData = await window.bracketsManager.export();
renderBracket(newData);
const bracketsStore = JSON.parse(localStorage.getItem(BRACKETS));
bracketsStore[id] = newData;
localStorage.setItem(BRACKETS, JSON.stringify(bracketsStore));
inputMask.style.display = 'none';
document.removeEventListener('keypress', enterKeyListener)
}
const inputButton = document.getElementById(INPUT_SUBMIT);
inputButton.onclick = updateMatch
}
const data = loadData(id);
renderBracket(data);
}
</script>
</body>
</html>