-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
474 lines (446 loc) · 18.2 KB
/
main.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
const scriptURL = 'https://script.google.com/macros/s/AKfycbyr8woNmi1zLBQkzC52Wl92rTT9RO5lxsAIYQxcXJ30ITt6JRKr-tdKwZXwe-524v6i/exec';
/**
* Handling the onclick for adding to the game piece count
*/
function addGamePiece(e, index) {
let row = e.parentElement.parentElement;
let input = row.querySelectorAll('input')[index];
let v = input.value * 1; // times 1 so it copies the number, and not a reference
input.value = Math.min(input.max, v + 1);
}
/**
* Handling the onclick for subtracting to the game piece count
*/
function subtractGamePiece(e, index) {
let row = e.parentElement.parentElement;
let input = row.querySelectorAll('input')[index];
let v = input.value * 1; // times 1 so it copies the number, and not a reference
input.value = Math.max(input.min, v - 1);
}
/**
* Updates the charge station to display the correct checkboxes
*/
function updateChargeStationCheckboxes() {
//* Getting Data
let data = new FormData(document.forms['scouting-form']);
//* Auto
let autoChargeStationState = data.get('auto-charge');
let autoEngagedAttempt = document.querySelector('#engage-attempt-auto');
//reset values
if (autoChargeStationState !== 'docked') {
//only resets if docked is not the one clicked
let autoEngagedAttemptCheckbox = document.querySelector('#auto-engage-attempt');
autoEngagedAttemptCheckbox.checked = false;
autoEngagedAttemptCheckbox.parentElement.classList.remove('checked'); //makes not green
}
//hide/unhide
autoEngagedAttempt.style = autoChargeStationState == 'docked' ? '' : 'display: none;';
//* End
let endChargeStationState = data.get('end-charge');
let endEngagedAttempt = document.querySelector('#end-engage');
let endPark = document.querySelector('#end-park');
//reset values
if (endChargeStationState !== 'docked') {
//only resets if docked is not the one clicked
let endEngagedAttemptCheckbox = document.querySelector('#end-engage-attempt');
endEngagedAttemptCheckbox.checked = false;
endEngagedAttemptCheckbox.parentElement.classList.remove('checked'); //makes not green
}
if (endChargeStationState !== 'none') {
//only resets if none is not the one clicked
let parkedCheckbox = document.querySelector('#Parked');
parkedCheckbox.checked = false;
parkedCheckbox.parentElement.classList.remove('checked'); //makes not green
}
autoEngagedAttempt.style = autoChargeStationState == 'docked' ? '' : 'display: none;';
//hide/unhide
autoEngagedAttempt.style = autoChargeStationState == 'docked' ? '' : 'display: none;';
endEngagedAttempt.style = endChargeStationState == 'docked' ? '' : 'display: none;';
endPark.style = endChargeStationState == 'none' ? '' : 'display: none;';
}
//back to top
let backToTop = document.querySelector('#back-to-top');
backToTop.addEventListener('click', () => {
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
});
/**
* Handles the onclick for setting the hidden input for the qualitative value
*/
function setQualitative(e) {
let q_row = e.parentElement;
let input = q_row.querySelector('input');
let value = e.getAttribute('value');
input.value = value;
//styling
let before = true;
[...q_row.querySelectorAll('div')].forEach((input) => {
input.className = 'star';
if (e.value == -1) return;
if (before) {
input.className = 'star starred';
}
if (input == e) {
before = false;
}
});
}
//breakdown + defense box logic
let breakdownCheckbox = document.querySelector('input[name=Breakdown]');
breakdownCheckbox.addEventListener('change', (e) => {
let breakdownElab = document.querySelector('#breakdownCommentBox');
let breakdownBox = document.querySelector('textarea[name=BreakdownCom]');
if (breakdownCheckbox.checked == true) {
let newValue = localStorage.getItem('breakdown');
breakdownBox.value = newValue;
breakdownElab.style = '';
} else {
breakdownElab.style = 'display: none;';
localStorage.setItem('breakdown', breakdownBox.value);
breakdownBox.value = '';
}
});
let defenseCheckbox = document.querySelector('input[name=Defense]');
defenseCheckbox.addEventListener('change', (e) => {
let defenseElab = document.querySelector('#defenseCommentBox');
let defenseBox = document.querySelector('textarea[name=DefenseCom]');
if (defenseCheckbox.checked == true) {
let newValue = localStorage.getItem('defense');
defenseBox.value = newValue;
defenseElab.style = '';
} else {
defenseElab.style = 'display: none;';
localStorage.setItem('defense', defenseBox.value);
defenseBox.value = '';
}
});
/**
* Function to delete inputs upon submission and no show toggle
*/
function clearInputs() {
[...document.querySelectorAll('input')].forEach((input) => {
let name = input.name;
if (!['ScoutName', 'ScoutTeamNum', 'TeamNumScouted', 'MatchNum', 'NoShow'].includes(name)) {
if (input.type == 'text') {
input.value = '';
} else if (input.type == 'number') {
input.value = 0;
} else if (input.type == 'checkbox') {
input.checked = false;
} else if (input.type == 'hidden') {
input.value = -1;
}
}
});
//removing stars and radio buttons
[...document.querySelectorAll('.starred')].forEach((starredButton) => {
starredButton.classList.remove('starred');
});
let checkedRadioAuto = document.querySelector('#auto-charge-none');
let checkedRadioEndgame = document.querySelector('#end-charge-none');
checkedRadioAuto.checked = true;
checkedRadioEndgame.checked = true;
//removing text boxes
let breakdownElab = document.querySelector('[name=BreakdownCom]');
let generalComments = document.querySelector('[name=GeneralCom]');
let defenseCom = document.querySelector('[name=DefenseCom]');
breakdownElab.value = '';
generalComments.value = '';
defenseCom.value = '';
//hiding breakdown/defense box
let breakdownSection = document.querySelector('#breakdownCommentBox');
breakdownSection.style = 'display: none;';
let defenseSection = document.querySelector('#defenseCommentBox');
defenseSection.style = 'display: none;';
}
/**
* Used for the NoShow input
* Clears form elements if NoShow clicked
*/
function noShowToggleHandler(e) {
let input = document.querySelector('div.not-no-show');
let inputCheckbox = document.querySelector('input[name=NoShow]');
if (inputCheckbox.checked == true) {
input.style = 'display:none';
//saves the data needed
let info = {};
[...document.querySelectorAll('input')].forEach((input) => {
if (input.name != 'NoShow') {
if (input.type == 'checkbox') {
info[input.name] = input.checked;
} else if (input.type == 'radio') {
if (input.checked == true) {
info[input.name] = input.value;
}
} else {
info[input.name] = input.value;
}
}
});
[...document.querySelectorAll('textarea')].forEach((input) => {
info[input.name] = input.value;
console.count('textarea');
});
let finalizedInfo = JSON.stringify(info);
localStorage.setItem('inGameData', finalizedInfo);
clearInputs();
} else {
input.style = '';
//displays saved data
let dataInfo = localStorage.getItem('inGameData');
let finalizedDataInfo = JSON.parse(dataInfo);
for (let name in finalizedDataInfo) {
let queryString = 'input[name=' + name + '],textarea[name=' + name + ']';
let dataValue = document.querySelector(queryString);
if (dataValue.type == 'checkbox') {
dataValue.checked = finalizedDataInfo[name];
} else if (dataValue.type == 'radio') {
let checkedRadioButton = document.querySelector('[value=' + finalizedDataInfo[name] + '][name=' + name + ']');
checkedRadioButton.checked = true;
} else {
dataValue.value = finalizedDataInfo[name];
}
}
//adds classes to starred elements
[...document.querySelectorAll('.qual')].forEach((element) => {
let input = element.querySelector('input');
let checkedStar = element.querySelector('button[value="' + input.value + '"],div[value="' + input.value + '"]');
setQualitative(checkedStar);
});
//shows breakdown + defense boxes
let bdCheckBox = document.querySelector('input[name=Breakdown]');
let breakdownSection = document.querySelector('#breakdownCommentBox');
breakdownSection.style = bdCheckBox.checked ? '' : 'display: none;';
let dCheckBox = document.querySelector('input[name=Defense]');
let defenseSection = document.querySelector('#defenseCommentBox');
defenseSection.style = dCheckBox.checked ? '' : 'display: none;';
}
}
let noShow = document.querySelector('input[name=NoShow]');
noShow.addEventListener('change', noShowToggleHandler);
const form = document.forms['scouting-form'];
function submit(e) {
let submitButton = document.querySelector('#submit');
//undos the submit while leaving data unchanged.
function undoSubmit() {
submitButton.disabled = false;
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
}
e.preventDefault();
window.data = new FormData(form);
//checking data
let name = data.get('ScoutName');
let teamNumber = data.get('ScoutTeamNum');
let teamScouted = data.get('TeamNumScouted');
let matchNumber = data.get('MatchNum');
if (!name || !teamNumber || !teamScouted || !matchNumber) {
showAlert('Please make sure you have provided all information (top 4 fields)');
undoSubmit();
return;
}
showConfirm(
'Are you sure you want to submit?',
() => {
//Hit yes
submitButton.disabled = true;
[
{ time: 'auto', cap: 'Auto' },
{ time: 'end', cap: 'End' },
].forEach(({ time, cap }) => {
let chargeInfo = data.get(time + '-charge'); //has value: none, attempted, docked, engaged
let engagedAttempt = data.get(time + '-engage-attempt');
//calculating values
let engagedSuccess = chargeInfo == 'engaged';
engagedAttempt = engagedSuccess || engagedAttempt;
let dockSuccess = engagedAttempt || chargeInfo == 'docked';
let dockAttempt = dockSuccess || chargeInfo == 'attempted';
//setting new values
data.set(time + '-charge', null);
data.set(time + '-engage-attempt', null);
data.set(cap + 'DockedAttempt', dockAttempt ? 1 : 0);
data.set(cap + 'DockedSuccess', dockSuccess ? 1 : 0);
data.set(cap + 'EngagedAttempt', engagedAttempt ? 1 : 0);
data.set(cap + 'EngagedSuccess', engagedSuccess ? 1 : 0);
});
//adding fields that are empty by default
['NoShow', 'AutoEngagedAttempt', 'EndEngagedAttempt', 'PreLoaded', 'Mobility', 'Breakdown', 'Parked', 'Defense'].forEach((name) => {
console.log(!data.get(name));
if (!data.get(name)) {
data.set(name, '0');
}
});
console.log([...data.entries()]);
//sends data to form
fetch(scriptURL, {
method: 'POST',
body: data,
})
.then((response) => {
console.log(response);
if (response.status !== 200) {
showAlert('There was a problem submitting... please try again.');
undoSubmit();
return;
}
showAlert('Thank you!');
//resets the form
undoSubmit();
clearInputs();
//removes excess
let noShow = document.querySelector('input[name=NoShow]');
noShow.checked = false;
localStorage.removeItem('inGameData');
localStorage.removeItem('breakdown');
localStorage.removeItem('defense');
noShowToggleHandler();
let teamNumScouted = document.querySelector('input[name=TeamNumScouted]');
teamNumScouted.value = '';
let matchNum = document.querySelector('input[name=MatchNum]');
matchNum.value++;
setLocalStorage();
document.querySelectorAll('.check-super-box').forEach((csb) => {
csb.classList.remove('checked');
});
let checkedRadioAuto = document.querySelector('#auto-charge-none');
let checkedRadioEndgame = document.querySelector('#end-charge-none');
checkedRadioAuto.click();
checkedRadioEndgame.click();
//confetti
document.querySelector('.confetti').classList.add('go');
setTimeout(() => {
document.querySelector('.go').classList.remove('go');
}, 3000);
})
.catch((error) => {
console.log(error);
showAlert('There was a problem... please try again and notify the Team 2485 Analytics department if this happens again.');
undoSubmit();
});
},
() => {
/* Hit no */
}
);
//barrel roll
document.querySelector('#submit').classList.add('spin');
setTimeout(() => {
document.querySelector('#submit').classList.remove('spin');
}, 1000);
}
form.addEventListener('submit', submit);
function setLocalStorage(){
let formData = new FormData(form);
let name = formData.get("ScoutName");
let team = formData.get("ScoutTeamNum");
let matchNum = formData.get("MatchNum")
localStorage.setItem("name", name);
localStorage.setItem("team", team);
localStorage.setItem("match", matchNum)
}
function displaySavedData(){
let scoutName = document.querySelector("input[name=ScoutName]");
let teamNum = document.querySelector("input[name=ScoutTeamNum]");
let matchNum = document.querySelector("input[name=MatchNum");
scoutName.value = localStorage.getItem("name");
teamNum.value = localStorage.getItem("team");
matchNum.value = localStorage.getItem("match");
}
displaySavedData();
//check-super-box code
[...document.querySelectorAll('.check-super-box')].forEach((csb) =>
csb.addEventListener('click', (e) => {
let input = csb.querySelector('input');
// input.checked = !input.checked;
if (e.target.tagName == 'DIV') {
input.click();
}
csb.className = input.checked ? 'check-super-box checked' : 'check-super-box';
})
);
//radio-super-box
document.querySelectorAll('.radio-super-box').forEach((radioSuperBoxes) => {
let radioButtonBoxes = radioSuperBoxes.children;
[...radioButtonBoxes].forEach((button) =>
button.addEventListener('click', (e) => {
let input = button.querySelector('input');
input.checked = true;
[...radioButtonBoxes].forEach((b) => (b.className = ''));
input.parentElement.className = 'checked';
updateChargeStationCheckboxes();
})
);
});
//popups
/**
* The function to make a yes/no confirm popup
* @param {String} message The message to display
* @param {function} callbackOnConfirmed The function to call when 'yes' is clicked
* @param {function} callbackOnCancelled The function to call when 'no' is clicked
*/
function showConfirm(message, callbackOnConfirmed, callbackOnCancelled) {
console.log('running');
let okayButton = document.querySelector('#okay');
okayButton.style = 'display: none;';
let yesButton = document.querySelector('#yes');
let noButton = document.querySelector('#no');
yesButton.style = '';
noButton.style = '';
let confirmPopup = document.querySelector('.transparent');
let paragraph = document.querySelector('#popup-message');
paragraph.innerText = message;
function handleYes(e) {
closePopup();
callbackOnConfirmed();
}
function handleNo(e) {
closePopup();
callbackOnCancelled();
}
function closePopup() {
confirmPopup.style = 'display: none;';
try {
noButton.removeEventListener('click', handleNo);
yesButton.removeEventListener('click', handleYes);
} catch (e) {
console.error(e);
}
}
yesButton.addEventListener('click', handleYes);
noButton.addEventListener('click', handleNo);
confirmPopup.style = '';
console.log('ran');
}
/**
* The function to make a alert popup
* @param {String} message The message to display
*/
function showAlert(message) {
console.log('running');
let yesButton = document.querySelector('#yes');
let noButton = document.querySelector('#no');
yesButton.style = 'display: none;';
noButton.style = 'display: none;';
let okayButton = document.querySelector('#okay');
okayButton.style = '';
let alertPopup = document.querySelector('.transparent');
let paragraph = document.querySelector('#popup-message');
paragraph.innerText = message;
function handleOkay(e) {
alertPopup.style = 'display: none;';
okayButton.removeEventListener('click', handleOkay);
}
okayButton.addEventListener('click', handleOkay);
alertPopup.style = '';
console.log('ran');
}
//information buttons
[...document.querySelectorAll('#help,#help-media')].forEach(button => button.addEventListener('click', e => {
window.open('https://docs.google.com/presentation/d/1A_ah3rh98wCnLV53Md-9dFzIz7zg2KHp60l6SF2APA0/edit?usp=sharing');
}));