-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1695 lines (1471 loc) · 52.6 KB
/
Copy pathscript.js
File metadata and controls
1695 lines (1471 loc) · 52.6 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// initialize some variables
var overlay;
var resultsPage;
var startText;
var gameList;
var listNum;
var allowColorChange = false;
var passCorrectList = [];
var cardList = [];
var currentPosition = "NEUTRAL";
var gamesDict;
//defaultTimer = 60;
function shuffleList(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
// Swap array[i] and array[j]
[array[i], array[j]] = [array[j], array[i]];
}
}
// Check if the user is on an iOS device
function isiOS() {
return /iPhone|iPad|iPod/i.test(navigator.userAgent);
}
// Detects if device is in standalone mode
function isRunningStandalone() {
return (window.matchMedia('(display-mode: standalone)').matches);
}
function removeDuplicates(inputList) {
return [...new Set(inputList)];
}
function createOverlay() {
// create the overlay
overlay = document.createElement('div');
overlay.id = "overlay";
overlay.classList = "light-or-dark";
overlay.style.height = "100%";
overlay.style.width = "100%";
overlay.style.overflow = "hidden";
overlay.style.top = "0";
overlay.style.position = "fixed";
resultsPage = document.createElement('div');
resultsPage.style.height = "85%";
resultsPage.style.width = "90%";
resultsPage.style.top = "15%";
resultsPage.style.position = "fixed";
resultsPage.style.alignItems = "flex-start";
overlay.appendChild(resultsPage);
var banner = document.createElement('div');
banner.style.backgroundColor = "#06C7C3";
banner.style.height = "15%";
banner.style.width = "100%";
banner.style.position = "absolute";
banner.style.top = "0";
overlay.appendChild(banner);
circle = document.createElement('div');
circle.classList = "circle light-or-dark";
circle.style.borderRadius = "50%";
circle.style.border = "1vw solid #06C7C3";
circle.style.height = "10vw";
circle.style.width = "10vw";
circle.style.top = "80%";
circle.style.left = "50%";
circle.style.position = "absolute";
circle.style.transform = "translate(-50%, -50%)";
circle.innerText = roundTimer;
circle.style.fontSize = "240%";
circle.style.fontWeight = "bold";
banner.appendChild(circle);
startText = document.createElement('div');
startText.innerText = "Get Ready\n5";
startText.style.position = "absolute";
startText.style.width = "85%";
startText.style.fontSize = "500%";
startText.style.backgroundColor = "rgba(0, 0, 0, 0)";
startText.style.top = "60%";
startText.style.left = "50%";
startText.style.transform = "translate(-50%, -50%)";
startText.style.textAlign = "center";
startText.style.display = "-webkit-box";
startText.style.webkitLineClamp = "3";
startText.style.webkitBoxOrient = "vertical";
startText.style.overflow = "hidden";
startText.style.textOverflow = "ellipsis";
overlay.appendChild(startText);
const exitButton = document.createElement('div');
exitButton.style.top = "30%";
exitButton.style.left = "4%";
exitButton.style.position = "absolute";
exitButton.style.fontSize = "150%";
exitButton.innerHTML = '<img src="icons/general/arrow-left.svg" style="display: inline; width: 1em;" alt="back" class="invertible-image">Back';
exitButton.addEventListener('click', function () {
// break the loop for displaying the results
breakLoop = true;
document.body.removeChild(overlay);
// set the gameCanceled variable to true to stop the countdown timers
gameCanceled = true;
allowColorChange = false;
});
banner.appendChild(exitButton);
document.body.appendChild(overlay);
gameCanceled = false;
getReady(6);
}
function getReady(countdown) {
if (gameCanceled) {
return;
}
// start counting down to get ready
if (countdown == 1) {
passCorrectList = [];
cardList = [];
startGame();
} else {
var newNum = countdown - 1;
startText.textContent = "Get Ready\n" + newNum;
// play the count sound
playCountSound();
setTimeout(function () {
getReady(newNum);
}, 1000);
}
}
function startTimer(countdown) {
if (gameCanceled) {
return;
}
// start the game timer
if (countdown == 1) {
circle.innerText = "0";
passCorrectList.push("PASS");
cardList.push(gameList[listNum]);
endGame();
} else {
var newNum = countdown - 1;
circle.innerText = newNum;
setTimeout(function () {
startTimer(newNum);
}, 1000);
}
}
function startGame() {
// start the game timer
startTimer(roundTimer); // how many seconds the game will last. Default is roundTimer
listNum = 0;
startText.innerText = gameList[listNum];
allowColorChange = true;
}
function getGameList(gameName) {
// set a global variable to know what the key for the game is
replay = function () {
getGameList(gameName);
}
// get the array from the dictionary
gameList = gamesDict[gameName];
shuffleList(gameList);
createOverlay();
}
function handleOrientation(event) {
if (!allowColorChange) {
return;
}
if (event.gamma > -50 && event.gamma < 0) {
if (lockAnswer) {
return;
}
if (currentPosition == "NEUTRAL") {
overlay.classList.add('redGradient');
startText.innerText = "PASS";
playPassSound();
// add current item to passed list
passCorrectList.push("PASS");
cardList.push(gameList[listNum]);
currentPosition = "PASS";
lockAnswer = true;
listNum += 1;
} else {
skippedNeutral();
}
} else if (event.gamma < 60 && event.gamma > 0) {
if (lockAnswer) {
return;
}
if (currentPosition == "NEUTRAL") {
overlay.classList.add('greenGradient');
startText.innerText = "CORRECT";
playCorrectSound();
// add current item to correct list
passCorrectList.push("CORRECT");
cardList.push(gameList[listNum]);
currentPosition = "CORRECT";
lockAnswer = true;
listNum += 1;
} else {
skippedNeutral();
}
} else {
if (lockAnswer) {
if (waitingForLockAnswer) {
return;
}
setTimeout(function () {
lockAnswer = false;
waitingForLockAnswer = false;
}, 500);
waitingForLockAnswer = true;
return;
}
overlay.classList.remove('greenGradient');
overlay.classList.remove('redGradient');
if (currentPosition != "NEUTRAL") {
if (listNum > gameList.length - 1) {
listNum = 0;
}
startText.innerText = gameList[listNum];
currentPosition = "NEUTRAL";
}
}
}
function skippedNeutral() {
lockAnswer = true;
waitingForLockAnswer = true;
overlay.classList.remove('greenGradient');
overlay.classList.remove('redGradient');
if (currentPosition != "NEUTRAL") {
if (listNum > gameList.length - 1) {
listNum = 0;
}
startText.innerText = gameList[listNum];
currentPosition = "NEUTRAL";
}
setTimeout(function () {
lockAnswer = false;
waitingForLockAnswer = false;
}, 100);
}
function requestPermission() {
if (window.DeviceOrientationEvent) {
if (!isiOS()) {
window.addEventListener('deviceorientation', handleOrientation);
return true;
}
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
window.addEventListener('deviceorientation', handleOrientation);
console.log("permission granted");
return true;
} else {
console.log("permission denied");
return false;
}
})
.catch(error => {
console.error('Error requesting device orientation permission:', error);
return false;
});
} else {
return false;
}
}
function endGame() {
// end the game and show the results
allowColorChange = false;
overlay.classList.remove('greenGradient');
overlay.classList.remove('redGradient');
startText.innerText = "TIME'S UP!";
// play the pass sound
playPassSound();
setTimeout(function () {
// display the results
startText.innerText = '';
var leftCol = document.createElement('div');
leftCol.style.width = "45%";
leftCol.style.height = "100%";
leftCol.style.justifyContent = "start";
leftCol.style.flexDirection = "column";
leftCol.style.display = "flex";
leftCol.style.fontSize = "150%";
leftCol.style.height = "auto";
resultsPage.appendChild(leftCol);
var middleCol = document.createElement('div');
middleCol.style.width = "10%";
resultsPage.appendChild(middleCol);
var rightCol = document.createElement('div');
rightCol.style.width = "45%";
rightCol.style.height = "100%";
rightCol.style.justifyContent = "start";
rightCol.style.flexDirection = "column";
rightCol.style.display = "flex";
rightCol.style.fontSize = "150%";
rightCol.style.height = "auto";
resultsPage.appendChild(rightCol);
resultsPage.style.overflowY = "auto";
// allow certain elements to scroll again if the user started creating a new set
allowScroll(resultsPage);
overlay.removeChild(startText);
// create the replay button
const replayButton = document.createElement('div');
replayButton.style.top = "30%";
replayButton.style.right = "4%";
replayButton.style.position = "absolute";
replayButton.style.fontSize = "150%";
replayButton.innerHTML = 'Replay<img src="icons/general/arrow-replay.svg" style="display: inline; width: 1em;" alt="replay" class="invertible-image">';
replayButton.addEventListener('click', function () {
// break the loop for displaying the results
breakLoop = true;
overlay.remove();
replay();
});
var banner = document.querySelector("#overlay > div:nth-child(2)");
banner.appendChild(replayButton);
var totalPoints = 0;
var length = passCorrectList.length;
breakLoop = false;
loopTime = 1000;
function loopResults(indexLength, i = 0) {
if (i == indexLength || breakLoop == true) {
return;
}
var card = cardList[i];
if (passCorrectList[i] == "CORRECT") {
var color = "green";
totalPoints++;
if (loopTime == 1000) {
// play the count sound
playCountSound();
}
} else {
var color = "red";
}
var text = document.createElement('span');
text.innerText = card;
text.style.color = color;
text.style.marginTop = "3%";
var evenOdd = i % 2;
if (evenOdd == 0) {
// it is even, add to left list
leftCol.appendChild(text);
} else {
// it is odd, add to right list
rightCol.appendChild(text);
}
circle.innerText = totalPoints;
setTimeout(function () {
loopResults(indexLength, i + 1);
}, loopTime);
}
// call the function to loop through the results
loopResults(length);
// add an event listener to change the loop time to 0 seconds if the screen is tapped
function changeLoopTime() {
loopTime = 0;
// Remove the event listener after it executes once
document.removeEventListener('click', changeLoopTime);
}
document.addEventListener('click', changeLoopTime);
}, 3000);
}
function getDictionary(path) {
//first, request permission to use the device's orientation
requestPermission();
// get the JSON file from the path
fetch("game-sets/" + path)
.then(response => response.json())
.then(collectionDict => {
var title = collectionDict["title"];
var description = collectionDict["description"];
gamesDict = collectionDict["games"];
buildGamePreview(title, description);
})
.catch(error => console.error('Error fetching or parsing JSON:', error));
}
function buildGamePreview(title, description) {
// build the screen to confirm to play the game
// first create a blank div tag to prevent background items from being clicked
preventInput = document.createElement('div');
preventInput.style.height = "100%";
preventInput.style.width = "100%";
preventInput.style.overflow = "hidden";
preventInput.style.top = "0";
preventInput.style.position = "fixed";
preventInput.style.backgroundColor = "rgba(0, 0, 0, 0.75)";
document.body.appendChild(preventInput);
// Create main container div
const mainDiv = document.createElement('div');
mainDiv.classList.add('mainGamePreview');
mainDiv.style.position = 'absolute';
mainDiv.style.top = '50%';
mainDiv.style.left = '50%';
mainDiv.style.transform = 'translate(-50%, -50%)';
mainDiv.style.width = '85%';
mainDiv.style.border = '1px solid #ccc';
mainDiv.style.padding = '10px';
mainDiv.style.backgroundColor = '#06C7C3';
mainDiv.style.textAlign = 'center';
mainDiv.style.overflowY = "hidden";
// Create close button in the top left
const closeButton = document.createElement('div');
closeButton.classList = "closeBtn redGradient";
closeButton.style.borderRadius = "999px";
closeButton.style.aspectRatio = "1/1";
closeButton.style.display = "flex";
closeButton.style.alignItems = "center";
closeButton.style.justifyContent = "center";
closeButton.textContent = 'x';
closeButton.style.position = 'absolute';
closeButton.style.cursor = 'pointer';
closeButton.addEventListener('click', () => {
mainDiv.remove();
preventInput.remove();
});
// Create title
const titleDiv = document.createElement('div');
titleDiv.textContent = title;
titleDiv.style.fontWeight = 'bold';
titleDiv.style.fontSize = "200%";
titleDiv.style.display = "-webkit-box";
titleDiv.style.webkitLineClamp = "2";
titleDiv.style.webkitBoxOrient = "vertical";
titleDiv.style.overflow = "hidden";
titleDiv.style.textOverflow = "ellipsis";
titleDiv.style.margin = "0% 5%";
// Create time options div
const timeOptionsDiv = document.createElement('div');
timeOptionsDiv.className = "orangeGradient";
timeOptionsDiv.classList.add('timeOptions');
timeOptionsDiv.style.margin = "auto";
timeOptionsDiv.style.display = 'flex';
timeOptionsDiv.style.flexDirection = 'column';
timeOptionsDiv.style.fontSize = "125%";
var chooseTimeText = document.createElement('div');
chooseTimeText.innerText = "Game Timer Options";
timeOptionsDiv.appendChild(chooseTimeText);
var theOptions = document.createElement('div');
theOptions.style.display = 'flex';
theOptions.style.flexDirection = 'row';
theOptions.style.justifyContent = 'center';
timeOptionsDiv.appendChild(theOptions);
// Create options (60s, 90s, 120s)
const timeOptions = [60, 90, 120];
timeOptions.forEach((option) => {
optionDiv = document.createElement('div');
optionDiv.classList.add('timerOption');
optionDiv.textContent = option + "s";
optionDiv.style.cursor = 'pointer';
optionDiv.style.paddingLeft = '8%'; // Add margin to separate options
optionDiv.style.paddingRight = '8%';
optionDiv.style.paddingTop = '2%';
optionDiv.style.paddingBottom = '2%';
optionDiv.style.borderRadius = "999px";
if (defaultTimer == option) {
optionDiv.classList.add('selectedTimer');
roundTimer = defaultTimer;
}
optionDiv.addEventListener('click', () => {
// Execute function based on the selected option
roundTimer = option;
// Find all elements with the class name 'selectedTimer'
const selectedTimers = document.getElementsByClassName('timerOption');
// Loop through the collection and remove the 'selectedTimer' class from each element
for (let i = 0; i < selectedTimers.length; i++) {
const element = selectedTimers[i];
if (element.innerText == option + 's') {
element.classList.add('selectedTimer');
} else {
element.classList.remove('selectedTimer');
}
}
});
theOptions.appendChild(optionDiv);
});
// Create description div
const descriptionDiv = document.createElement('div');
descriptionDiv.style.flexGrow = "1";
descriptionDiv.style.display = "flex";
descriptionDiv.style.flexDirection = "column";
descriptionDiv.style.justifyContent = "center";
descriptionDiv.style.alignItems = "center";
descriptionDiv.style.overflow = "hidden";
var descriptionText = document.createElement('p');
descriptionText.className = "description";
descriptionText.innerText = description;
descriptionText.style.margin = "2%";
descriptionDiv.appendChild(descriptionText);
const gridDiv = document.createElement('div');
gridDiv.style.height = "100%";
gridDiv.style.overflowY = "auto";
theKeys = Object.keys(gamesDict);
if (theKeys.length > 1) {
// add a "Play All" option
var playAll = addPlayAllOption();
gridDiv.appendChild(playAll);
var gamesGrid = document.createElement('div');
gamesGrid.classList.add('grid');
// Create grid of div tags with titles and functions
theKeys.forEach((key) => {
const gridItemDiv = document.createElement('div');
gridItemDiv.textContent = key;
gridItemDiv.style.border = '1px solid #ddd';
gridItemDiv.style.padding = '5px';
gridItemDiv.style.margin = '5px';
gridItemDiv.style.cursor = 'pointer';
gridItemDiv.style.display = 'flex';
gridItemDiv.style.justifyContent = 'center';
gridItemDiv.style.alignItems = 'center';
gridItemDiv.addEventListener('click', function () {
getGameList(key);
});
gridItemDiv.style.borderRadius = "3vmin";
gamesGrid.appendChild(gridItemDiv);
});
gridDiv.appendChild(gamesGrid);
var topHeight = "65%";
var bottomHeight = "35%";
} else {
// add only the single play option
var playBtn = addPlayButton();
gridDiv.appendChild(playBtn);
var topHeight = "80%";
var bottomHeight = "20%";
}
var top = document.createElement('div');
top.style.height = topHeight;
top.style.display = "flex";
top.style.flexFlow = "column";
var bottom = document.createElement('div');
bottom.style.height = bottomHeight;
bottom.style.marginTop = "2%";
mainDiv.style.borderRadius = "4vmin";
timeOptionsDiv.style.borderRadius = "999px";
// Append created elements to the main container
top.appendChild(closeButton);
top.appendChild(titleDiv);
top.appendChild(descriptionDiv);
top.appendChild(timeOptionsDiv);
bottom.appendChild(gridDiv);
mainDiv.appendChild(top);
mainDiv.appendChild(bottom);
// Append main container to the body
document.body.appendChild(mainDiv);
// allow certain elements to scroll again if the user started creating a new set
allowScroll(gridDiv);
}
function addPlayAllOption() {
const gridItemDiv = document.createElement('div');
gridItemDiv.textContent = "Play All Sets";
gridItemDiv.style.border = '1px solid #ddd';
gridItemDiv.style.padding = '5px';
gridItemDiv.style.margin = '5px';
gridItemDiv.style.cursor = 'pointer';
gridItemDiv.addEventListener('click', function () {
getAllGames();
replay = getAllGames;
});
gridItemDiv.style.borderRadius = "3vmin";
gridItemDiv.className = "pinkGradient";
gridItemDiv.style.color = "white";
gridItemDiv.style.fontSize = "150%";
return gridItemDiv;
}
function getAllGames() {
var theList = [];
theKeys.forEach((key) => {
var set = gamesDict[key];
theList = theList.concat(set);
});
removeDuplicates(theList);
shuffleList(theList);
gameList = theList;
createOverlay();
}
function addPlayButton() {
var gridItemDiv = document.createElement('div');
gridItemDiv.classList = "pinkGradient";
gridItemDiv.textContent = "Play";
gridItemDiv.style.border = '1px solid #ddd';
gridItemDiv.style.padding = '5px';
gridItemDiv.style.margin = '5px';
gridItemDiv.style.cursor = 'pointer';
gridItemDiv.addEventListener('click', function () {
getGameList("Play");
});
gridItemDiv.style.borderRadius = "3vmin";
gridItemDiv.style.color = "white";
gridItemDiv.style.fontSize = "150%";
return gridItemDiv;
}
function preventScroll() {
// prevent any element from scrolling after the user has clicked on an input
var preventDefault = function (e) {
e.preventDefault();
e.stopPropagation(); // Stop the event from propagating to parent or child elements
};
document.addEventListener('touchmove', preventDefault, {
passive: false
});
document.addEventListener('touchforcechange', preventDefault, {
passive: false
});
}
function allowScroll(element) {
var allowDefault = function (e) {
// do nothing
e.stopPropagation();
};
element.addEventListener('touchmove', allowDefault, {
passive: false
});
element.addEventListener('touchforcechange', allowDefault, {
passive: false
});
element.addEventListener('scroll', function () {
// Check if the scroll position is at the top
if (element.scrollTop === 0) {
// If at the top, prevent further scrolling up
element.scrollTop = 1; // Set it to 1 to prevent further scrolling
} else if (element.scrollHeight - element.scrollTop === element.clientHeight) {
// If at the bottom, prevent further scrolling down
element.scrollTop = element.scrollHeight - element.clientHeight - 1;
}
});
setTimeout(function () {
// set topscroll to 1 immediately so that scrolling up doesn't cause the whole screen to move
element.scrollTop = 1;
}, 1);
}
function controlStretch(element) {
// adjusts the height of the stretch element to make sure that the grid containers can always scroll
var stretch = element.querySelector('.stretch');
if (stretch == null) {
return;
}
// first set the height to zero in order to reset it
stretch.style.height = "0px";
if (element.clientHeight >= element.scrollHeight) {
var allCards = element.querySelectorAll('.gameCard:not(.stretch):not(.buffer)');
var num = allCards.length;
if (num == 0) {
stretch.style.height = element.clientHeight + 1 + 'px';
return;
}
var lastCard = allCards[num - 1];
var lastElementPosition = lastCard.offsetTop + lastCard.offsetHeight;
var newHeight = (element.clientHeight - lastElementPosition) + 1;
stretch.style.height = newHeight + 'px';
}
}
function addEmptyClickEvent() {
// this function is needed in order to prevent random scrolling when double tapping on elements after clicking on an input tag on iOS
// Get all elements on the page
var allElements = document.getElementsByTagName("*");
// Iterate through each element
for (var i = 0; i < allElements.length; i++) {
var currentElement = allElements[i];
// Check if the element does not already have an onclick attribute
if (!currentElement.hasAttribute("onclick")) {
currentElement.addEventListener("click", function () {
// simply return to prevent scrolling
return;
});
}
}
}
function displayPopup(message, closeText = null, continueText = null, continueFunction = null, ...continueArgs) {
// first remove the prevent input element if it already exists
var oldElem = document.getElementById('preventInput');
if (oldElem != null) {
oldElem.remove();
}
// create an element over the top of everything and display a message with options
// create a blank div tag to prevent background items from being clicked
var preventInput = document.createElement('div');
preventInput.id = "preventInput";
preventInput.style.height = "100%";
preventInput.style.width = "100%";
preventInput.style.overflow = "hidden";
preventInput.style.top = "0";
preventInput.style.position = "fixed";
preventInput.style.backgroundColor = "rgba(0, 0, 0, 0.75)";
document.body.appendChild(preventInput);
var mainDiv = document.createElement('div');
mainDiv.classList = "light-or-dark";
mainDiv.style.width = "70%";
mainDiv.style.position = "fixed";
mainDiv.style.top = "50%";
mainDiv.style.left = "50%";
mainDiv.style.transform = 'translate(-50%, -50%)';
mainDiv.style.margin = "auto";
mainDiv.style.overflow = "hidden";
mainDiv.style.borderRadius = "20px";
mainDiv.style.border = "solid";
mainDiv.style.display = "flex";
mainDiv.style.flexDirection = "column";
mainDiv.style.textAlign = "center";
preventInput.appendChild(mainDiv);
var messageDiv = document.createElement('div');
messageDiv.style.margin = "5%";
messageDiv.innerHTML = message;
mainDiv.appendChild(messageDiv);
var buttonContainer = document.createElement('div');
buttonContainer.style.display = "flex";
buttonContainer.style.justifyContent = "space-evenly";
buttonContainer.style.marginBottom = "5%";
mainDiv.appendChild(buttonContainer);
if (closeText != null) {
var cancelBtn = document.createElement('div');
cancelBtn.style.padding = "3% 5% 3% 5%";
cancelBtn.style.borderRadius = "999px";
cancelBtn.classList = "redGradient";
cancelBtn.innerText = closeText;
cancelBtn.addEventListener('click', () => {
preventInput.remove();
});
buttonContainer.appendChild(cancelBtn);
}
if (continueText != null && continueFunction != null) {
var continueBtn = document.createElement('div');
continueBtn.style.padding = "3% 5% 3% 5%";
continueBtn.style.borderRadius = "999px";
continueBtn.classList = "blueGradient";
continueBtn.innerText = continueText;
continueBtn.addEventListener('click', () => {
continueFunction(...continueArgs);
preventInput.remove();
});
buttonContainer.appendChild(continueBtn);
}
}
function displayLoadingPopup(message) {
var topText = message + "<br><br>";
displayPopup(topText + "◦•••••");
function changeEllipsis() {
var ellipsisElement = document.getElementById('preventInput').querySelector('div > div > div');
var ellipsisText = ellipsisElement.innerHTML;
if (ellipsisText == topText + "◦•••••") {
ellipsisElement.innerHTML = topText + "•◦••••";
} else if (ellipsisText == topText + "•◦••••") {
ellipsisElement.innerHTML = topText + "••◦•••";
} else if (ellipsisText == topText + "••◦•••") {
ellipsisElement.innerHTML = topText + "•••◦••";
} else if (ellipsisText == topText + "•••◦••") {
ellipsisElement.innerHTML = topText + "••••◦•";
} else if (ellipsisText == topText + "••••◦•") {
ellipsisElement.innerHTML = topText + "•••••◦";
} else if (ellipsisText == topText + "•••••◦") {
ellipsisElement.innerHTML = topText + "◦•••••";
} else {
return;
}
setTimeout(changeEllipsis, 250);
}
changeEllipsis();
}
// functions for favorites page
function toggleHeart(gameID) {
var theElement = document.getElementById(gameID);
var heartElem = theElement.querySelector("div>img");
if (heartElem.src.includes("fill")) {
heartElem.src = "icons/general/heart-outline.svg";
removeFavorites(gameID);
} else {
heartElem.src = "icons/general/heart-fill.svg";
addToFavorites(gameID);
}
// now try to toggle the heart on the duplicate div too
var theElement = document.getElementById(gameID + "-fave");
if (theElement == null) {
return;
}
var heartElem = theElement.querySelector("div>img");
if (heartElem.src.includes("fill")) {
heartElem.src = "icons/general/heart-outline.svg";
} else {
heartElem.src = "icons/general/heart-fill.svg";
}
}
function getFavorites() {
var faves = localStorage.getItem("favorites");
if (faves !== null) {
return JSON.parse(faves);
}
return [];
}
function addToFavorites(newValue) {
var favesList = getFavorites();
favesList.push(newValue);
setFavorites(favesList);
}
function setFavorites(favesList) {
var string = JSON.stringify(favesList);
localStorage.setItem("favorites", string);
}
function removeFavorites(valueToRemove) {
var favesList = getFavorites();
var updatedList = favesList.filter(value => value !== valueToRemove);
setFavorites(updatedList);
}
function markFavorites() {
var favesList = getFavorites();
favesList.forEach(value => {
var theElement = document.getElementById(value);
var heartElem = theElement.querySelector("div>img");
heartElem.src = "icons/general/heart-fill.svg";
});
}
function hideOtherElements(unhiddenElementClass) {
var list = ['allGames', 'favorites', 'create', 'settings'];
for (var i = 0; i < list.length; i++) {
var value = list[i];
var element = document.getElementById(value);
if (value == unhiddenElementClass) {
element.classList.remove('hidden');
controlStretch(element);
allowScroll(element);
document.querySelector("body > div.menu-bar > div:nth-child(" + (i + 1) + ")").classList.add("active");
} else {
element.classList.add('hidden');
document.querySelector("body > div.menu-bar > div:nth-child(" + (i + 1) + ")").classList.remove("active");
}
}
}
function displayFavorites() {
var favesPage = document.getElementById('favorites');
favesPage.innerHTML = '<div class="gameCard buffer"></div><div class="gameCard stretch" style="height: 0px;"></div>';
var buffer = favesPage.querySelector(".buffer");
var favesList = getFavorites();
favesList.forEach(value => {
var element = document.getElementById(value);
var clone = element.cloneNode(true);
clone.id = value + "-fave";
var dropDownBtn = clone.querySelector('.dropDownButton');
if (dropDownBtn != null) {
dropDownBtn.remove();
}
favesPage.insertBefore(clone, buffer);
});
hideOtherElements('favorites');
// sometimes the stretch function doesn't work because not all elements have been appended, so delay it
setTimeout(function () {
var element = document.getElementById('favorites');
controlStretch(element);
}, 100);
}
// functions for create page
function createSet() {
// reveal the create-page
var page = document.getElementById('create-page');
page.classList.remove('hidden');
selectedColor = "blueGradient";
// allow the cards text box and description input to scroll again
var cardsTextBox = document.querySelector("#enterCards");
allowScroll(cardsTextBox);
var descriptionTextBox = document.querySelector("#description");
allowScroll(descriptionTextBox);
}
function discardSet() {
var page = document.getElementById('create-page');
page.classList.add('hidden');
var inputs = document.querySelectorAll('input, textarea');
inputs.forEach(element => {
element.value = '';
});
selectColor('blueGradient');
// make sure that the scrolling bug in iOS doesn't mess with the user experience
preventScroll();
// make sure random scrolling doesn't happen on iOS
addEmptyClickEvent();
}
function selectColor(color) {
var circles = document.querySelectorAll('.color-circle');
circles.forEach(element => {
if (element.classList.contains(color)) {
element.classList.add('selectedColor');
selectedColor = color;
} else {