-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainMain.js
1008 lines (896 loc) · 29.2 KB
/
mainMain.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
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
//V2.0
//Import the functions needed from firebase sdk's
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-analytics.js";
import {
getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, browserLocalPersistence,
browserSessionPersistence,
inMemoryPersistence
} from
"https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js";
import { getDatabase, set, onValue, get, ref, update, child, remove, push }
from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
// The web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDYX1BCgVLKCy3SMFl88XxXG9ne-t1ub7U",
authDomain: "absorb-284b3.firebaseapp.com",
projectId: "absorb-284b3",
storageBucket: "absorb-284b3.appspot.com",
messagingSenderId: "180851909476",
appId: "1:180851909476:web:bb263a466041a8bdbc5071",
measurementId: "G-2QDJM7BV0H",
databaseURL: "https://absorb-284b3-default-rtdb.firebaseio.com/"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth();
const db = getDatabase(app);
var data = null;
//current user stuff
auth.onAuthStateChanged(function (user) {
if (user) {
//sets the variable on the global scale
globalThis.user = auth.currentUser;
if (currentView = 'blankPage') {
navigator('landingscreen', 'blankPage');
}
//get deckList from database and update it locally
const dbref = ref(db);
get(child(dbref, "users/" + user.uid + '/deckList' )).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
deckList = data;
populateLists();
}
else {
console.log("There is no data here")
}
});
} else {
// No user is signed in.
navigator("firstPagescreen", "blankPage");
}
});
//start navigation
var currentView = 'firstPagescreen'
const navigator = (shown, hidden) => {
document.getElementById(hidden).style.display = 'none';
document.getElementById(shown).style.display = 'block';
currentView = shown;
};
//global variables
var deckList = ["Empty"];
var cardList = ["Empty"];
var definitionList = ["Empty"];
var currDeck = null;
var guest = false;
//start blankPage
const blankPage = document.getElementById("blankPage");
//start firstPage --------------------------------------------------------------------------
const firstPage = document.getElementById('firstPagescreen');
firstPage.style.display = 'none';
const createAccountButton = document.getElementById("CreateAccountButton");
createAccountButton.addEventListener("click", function () {
navigator('createaccountscreen', 'firstPagescreen');
});
const logInButton = document.getElementById('LogInButton');
logInButton.addEventListener("click", function () {
navigator('loginscreen', 'firstPagescreen');
});
//Guest Login Info
const guestButton = document.getElementById("guestButton");
guestButton.addEventListener("click", function () {
signInGuest();
})
function signInGuest() {
signInWithEmailAndPassword(auth, "[email protected]", "guestguest")
.then((userCredential) => {
const user = userCredential.user;
console.log(user)
//navigate to landing page
navigator('landingscreen', 'firstPagescreen');
guest = true;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(errorMessage);
});
}
//start create account page ----------------------------------------------------------------
const createaccount = document.getElementById("createaccountscreen");
//set it to be invisible
createaccount.style.display = 'none';
const initCreateAccountPage = () => {
//add listeners
//back button
const backButtonCreate = document.getElementById("backButtonCreate");
backButtonCreate.addEventListener("click", function () {
navigator("firstPagescreen", "createaccountscreen");
});
//submitting the form
const entryForm = document.getElementById("createAccountForm");
entryForm.addEventListener("submit", (event) => {
event.preventDefault();
//Collect user-inputted data
var email = document.getElementById('newEmail').value;
var password = document.getElementById("newPassword").value;
//Register new user in Firebase
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
const userId = user.uid;
alert("Registration Successful!");
//clear text entry fields
document.getElementById('newEmail').value='';
document.getElementById('newPassword').value='';
//setup database
set(ref(db, 'users/' + userId), {
email: user.email,
deckList: deckList
});
//Log new user in
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
console.log(user)
//navigate to landing page
navigator('landingscreen', 'createaccountscreen');
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(errorMessage);
});
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(error);
});
});
};
initCreateAccountPage();
//Start Logging In Page ---------------------------------------------------------------------
const loginscreen = document.getElementById("loginscreen");
//initially set not to be diplayed
loginscreen.style.display = "none";
//back button listener
const backButtonLogIn = document.getElementById("backButtonLogIn");
backButtonLogIn.addEventListener("click", function () {
navigator("firstPagescreen", "loginscreen");
});
//Grabbing Log-in Information
const entryForm = document.getElementById("LogInForm");
entryForm.addEventListener("submit", (event) => {
event.preventDefault();
//Collect user-inputted data
var email = document.getElementById('inputEmail').value;
var password = document.getElementById("inputPassword").value;
//clear data from entry fields
document.getElementById('inputEmail').value ='';
document.getElementById('inputPassword').value='';
//send data to firebase auth
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
//get deckList data from firebase
const dbref = ref(db);
get(child(dbref, "users/" + user.uid + '/deckList' )).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
deckList = data;
}
else {
console.log("There is no data here")
}
});
//navigate to landing page
navigator("landingscreen", "loginscreen");
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(errorMessage);
});
});
//Start Landing Page-------------------------------------------------------------------------
const landingpagescreen = document.getElementById("landingscreen");
//initially set not to be displayed
landingpagescreen.style.display = "none";
//puts the user's email up at the top of the landing page
auth.onAuthStateChanged(function (user) {
if (user) {
document.getElementsByClassName("pagetitle")[0].innerText = "Account: " + user.email;
if (user.email == "[email protected]") {
document.getElementsByClassName("pagetitle")[0].innerText = "Absorb: Guest Mode";
guest = true;
}
}
});
//sign out button
const signOutButton = document.getElementById("signOutButton");
signOutButton.addEventListener("click", function () {
navigator("firstPagescreen", "landingscreen");
signOut(auth);
deleteContents();
console.log(data);
deleteGlobalVariables();
});
//Create Deck Screen
const createdeckscreen = document.getElementById("createdeckscreen");
createdeckscreen.style.display = 'none';
//create deck button
const createDeckButton = document.getElementById("createDeckButton");
createDeckButton.addEventListener("click", function () {
//start create deck window
createdeckscreen.style.display = 'block';
createDeckButton.style.display = 'none';
});
const deckNameEntryForm = document.getElementById("newDeckNameInput");
const createDeckNameButton = document.getElementById("newDeckNameInputButton");
deckNameEntryForm.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
createDeckNameButton.click();
}
});
createDeckNameButton.addEventListener("click", function () {
//Collect user-inputted data
var newDeckName = deckNameEntryForm.value;
//clear the input
deckNameEntryForm.value='';
//add deck to database
if (guest == true) {
alert("Guest Editing Prohibited");
}
else {
deckToDatabase(auth.currentUser.uid, newDeckName);
deleteContents();
populateLists();
}
});
//populate list of decks
function populateLists() {
if (deckList[0] == "Empty") {
console.log("This user has no decks")
}
else {
for (let i = 0; i < deckList.length; i++) {
buildListItem(deckList[i]);
}
}
}
//populate list of decks in GUI
const buildListItem = (deckName) => {
const isoContainer = document.createElement("div");
isoContainer.className = "isoContainer";
const div = document.createElement("div");
div.className = "deckListDiv";
const options = document.createElement("div");
options.className = "options";
//make a paragraph
const para = document.createElement("p");
const node = document.createTextNode(deckName);
para.appendChild(node);
//make the study button
const doer = document.createElement("button");
doer.className = "doButton";
doer.innerHTML = "Study";
//study button functionality
doer.onclick = function () {
if (document.getElementById("mcTerm" != null)) {
deleteMC();
}
if (document.getElementById("termInput") != null) {
deleteTyped();
}
const dbref = ref(db);
currDeck = deckName;
//get the list of cards and check first to see if it is empty
get(child(dbref, "users/" + user.uid + '/' + convertToPath(deckName) + '/definitionList')).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
definitionList = data;
if (definitionList[0] == "Empty") {
alert("No Cards to Study");
}
//if not empty, navigate on
else {
const studyHeader = document.getElementById("studyScreenHeader");
studyHeader.innerHTML = (deckName);
navigator('studyscreen', 'landingscreen');
//get the card list
get(child(dbref, "users/" + user.uid + '/' + convertToPath(deckName) + "/cardList")).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
cardList = data;
startStudy();
}
else {
console.log("There be no cards here")
}
});
}
}
else {
alert("No Cards to Study");
}
});
}
//make the delete button
const deleter = document.createElement("button");
deleter.className="deleteButton";
deleter.innerHTML = "Delete";
//make the edit button
const editor = document.createElement("button");
editor.className = "editButton";
editor.innerHTML = "Edit";
//edit button functionality
editor.onclick = function () {
navigator('editorscreen', 'landingscreen');
const editHeader = document.getElementById('editScreenTitle')
editHeader.innerHTML = deckName;
currDeck = deckName;
//get cardList from database and update it locally
const dbref = ref(db);
get(child(dbref, "users/" + user.uid + '/' + deckName + '/cardList')).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
cardList = data;
}
else {
console.log("There be no cards here")
}
});
//get definitionList from database and update it locally
get(child(dbref, "users/" + user.uid + '/' + deckName + '/definitionList')).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
definitionList = data;
populateCardLists();
}
else {
console.log("There are no definitions for some reason")
}
});
};
//delete button functionality
deleter.onclick = function () {
if (guest == false) {
deleteDeck(user.uid, deckName);
}
else {
alert("Guest Editing Prohibited")
}
};
//add individual section to parent div
div.appendChild(para);
options.appendChild(editor);
options.appendChild(doer);
options.appendChild(deleter);
div.appendChild(options);
const container = document.getElementById("listFlashcards");
isoContainer.appendChild(div);
container.appendChild(isoContainer);
};
//Clear list in GUI
const deleteContents = () => {
const parentElement = document.getElementById("listFlashcards");
let child = parentElement.lastElementChild;
while (child) {
parentElement.removeChild(child);
child = parentElement.lastElementChild;
};
};
//Start Edit Screen -------------------------------------------------------------------------------->
const editdeckscreen = document.getElementById("editorscreen");
//initially set not to be displayed
editdeckscreen.style.display = "none";
//create card screen controls
const cardNameEntry = document.getElementById("newCardNameInput");
const cardTermEntry = document.getElementById("newCardTermInput");
cardTermEntry.addEventListener("keydown", function(event) {
if (event.code === "Tab") {
event.preventDefault();
createCardButton.click();
cardNameEntry.focus();
}
});
const createCardButton = document.getElementById("newCardButton");
//back button controls
const backEditButton = document.getElementById("backButtonEdit");
backEditButton.addEventListener("click", function () {
deleteGlobalVariablesExceptDeckList();
navigator("landingscreen", "editorscreen");
deleteEditorContents("listEditFlashcards");
});
//create card button
createCardButton.addEventListener("click", function () {
if (guest == true) {
alert("Guest Editing Prohibited");
}
else {
//Collect user-inputted data
var newCardName = cardNameEntry.value;
var newCardTerm = cardTermEntry.value;
//clear the input
cardNameEntry.value='';
cardTermEntry.value='';
//add card to database
console.log(newCardName + ' : ' + newCardTerm);
cardToDeck(user.uid, currDeck, newCardName, newCardTerm);
//update GUI
deleteEditorContents();
populateCardLists();
cardNameEntry.focus();
}
});
//populate list of cards in GUI
const buildCardListItem = (cardTerm, cardDefinition) => {
const div = document.createElement("div");
const termDiv = document.createElement("div");
termDiv.className = "termDiv";
const defDiv = document.createElement("div");
defDiv.className = "defDiv";
div.className = "cardListDiv";
//make the term
const termPara = document.createElement("p");
termPara.innerHTML = convertToText(cardTerm); //converts text to HTML
const defPara = document.createElement("p");
defPara.innerHTML = convertToText(cardDefinition);
const deleter = document.createElement("button");
deleter.className = "deleteCardButton";
deleter.innerHTML = "Delete";
//delete button functionality
deleter.onclick = function () {
console.log(guest);
if (guest == true) {
alert("Guest Editing Prohibited");
}
else {
deleteCard(user.uid, currDeck, cardTerm);
}
};
//add individual section to parent div
termDiv.append(termPara);
defDiv.append(defPara);
div.appendChild(termDiv);
div.appendChild(defDiv);
div.appendChild(deleter);
const container = document.getElementById("listEditFlashcards");
container.appendChild(div);
};
//populate list of cards
function populateCardLists() {
if (cardList[0] == "Empty") {
console.log("This user has no cards")
}
else {
for (let i = 0; i < cardList.length; i++) {
buildCardListItem(cardList[i], definitionList[i]);
}
}
}
//Clear list in GUI
const deleteEditorContents = () => {
const parentElement = document.getElementById("listEditFlashcards");
let child = parentElement.lastElementChild;
while (child) {
parentElement.removeChild(child);
child = parentElement.lastElementChild;
};
};
//Start Study Screen ------------------------------------------------------------------------------->
const studyPage = document.getElementById('studyscreen');
studyPage.style.display = 'none';
//back button
const backer = document.getElementById("backButtonStudy");
backer.addEventListener("click", function () {
navigator("landingscreen", "studyscreen");
const mcTerm = document.getElementById("mcTerm");
deleteMC();
deleteTyped();
deleteGlobalVariablesExceptDeckList();
});
//start study mode function
function startStudy() {
const genMode = generateRandomIntegerInRange(0,1);
if (document.getElementById("mcTerm" != null)) {
deleteMC();
}
if (document.getElementById("termInput") != null) {
deleteTyped();
}
if (genMode == 0) {
genTyped();
}
else {
if (cardList.length < 5) {
genTyped();
}
else {
genMC();
}
}
}
function genMC() {
//generate visuals
if (document.getElementById("mcTerm" != null)) {
deleteMC();
}
if (document.getElementById("termInput") != null) {
deleteTyped();
}
const mcDiv = document.createElement("div");
mcDiv.className = "mcDiv";
mcDiv.id="mcDiv";
const options = document.createElement("div");
options.className = "options";
const mcTerm = document.createElement('h2');
mcTerm.id = "mcTerm";
const option1 = document.createElement("button");
option1.id = "a";
const option2 = document.createElement("button");
option2.id = 'b';
const option3 = document.createElement("button");
option3.id = 'c';
const option4 = document.createElement("button");
option4.id='d';
//get a card from the list
const cardNum = generateRandomIntegerInRange(0, cardList.length -1 );
mcTerm.innerHTML = cardList[cardNum];
//populate the options
var list = [];
var i = 0;
while (i < 4) {
const temp = generateRandomIntegerInRange(0, cardList.length - 1);
console.log("Num: ", temp);
if (temp != cardNum && !list.includes(temp)) {
list.push(temp);
i++;
}
}
i = 0;
option1.innerHTML = convertToText(definitionList[list[0]]);
option2.innerHTML = convertToText(definitionList[list[1]]);
option3.innerHTML = convertToText(definitionList[list[2]]);
option4.innerHTML = convertToText(definitionList[list[3]]);
list = [];
//randomly replace one of the options with the correct value
const correctOption = generateRandomIntegerInRange(0, 3);
switch (correctOption) {
case 0:
option1.innerHTML = definitionList[cardNum];
break;
case 1:
option2.innerHTML = definitionList[cardNum];
break;
case 2:
option3.innerHTML = definitionList[cardNum];
break;
case 3:
option4.innerHTML = definitionList[cardNum];
break;
}
//listeners for each option
option1.addEventListener('click', function onClick(event) {
revealAnswer();
});
option2.onclick = function () {
revealAnswer();
}
option3.onclick = function () {
revealAnswer();
}
option4.onclick = function () {
revealAnswer();
}
//add everything into the HTML
const studyScreen = document.getElementById("StudyScreenContainer");
options.appendChild(option1);
options.appendChild(option2);
options.appendChild(option3);
options.appendChild(option4);
mcDiv.appendChild(mcTerm);
mcDiv.appendChild(options);
studyScreen.appendChild(mcDiv);
function revealAnswer() {
option1.style.backgroundColor = "#d90f3b";
option2.style.backgroundColor = "#d90f3b";
option3.style.backgroundColor = "#d90f3b";
option4.style.backgroundColor = "#d90f3b";
console.log(correctOption);
switch (correctOption) {
case 0:
option1.style.backgroundColor = "#1A970A";
break;
case 1:
option2.style.backgroundColor = "#1A970A";
break;
case 2:
option3.style.backgroundColor = "#1A970A";
break;
case 3:
option4.style.backgroundColor = "#1A970A";
break;
}
setTimeout(function(){
deleteMC();
startStudy();
}, 1300);
}
}
function genTyped() {
//generate visuals
if (document.getElementById("mcTerm" != null)) {
deleteMC();
}
if (document.getElementById("termInput") != null) {
deleteTyped();
}
const buttonDiv = document.createElement("div");
buttonDiv.className = "buttonDiv";
const showAnswer = document.createElement("button");
showAnswer.innerHTML = "Show Answer";
showAnswer.id = "showAnswer";
showAnswer.class = "showAnswer";
buttonDiv.appendChild(showAnswer);
const defHeader = document.createElement('h2');
defHeader.id = "defHeader";
const studyContainer = document.getElementById("StudyScreenContainer");
studyContainer.appendChild(defHeader);
const termInput = document.createElement("input");
termInput.id = ("termInput");
termInput.style = "resize: none;"
termInput.placeholder = "Enter Term";
studyContainer.appendChild(termInput);
//get a card from the list
const cardNum = generateRandomIntegerInRange(0, cardList.length - 1);
defHeader.innerHTML = convertToText(definitionList[cardNum]);
//retrieve and evaluate response
termInput.focus();
termInput.addEventListener("keypress", (event) => {
if (event.key == "Enter") {
//Collect user-inputted data
var guess = termInput.value;
guess = guess.toLowerCase();
var answer = cardList[cardNum];
answer = convertToTextCompare(answer).toLowerCase();
console.log("ANSWER: ", answer);
console.log(cardList)
if (guess == answer) {
deleteTyped();
startStudy();
}
else {
termInput.value = "";
termInput.placeholder ="Try Again"
studyContainer.appendChild(buttonDiv);
}
}
showAnswer.addEventListener("click", function () {
termInput.placeholder = cardList[cardNum];
termInput.focus();
});
});
}
function deleteMC() {
const a = document.getElementById('a');
const b = document.getElementById('b');
const c = document.getElementById('c');
const d = document.getElementById('d');
while (document.getElementById('a') != null) {
a.remove();
b.remove();
c.remove();
d.remove();
}
while (document.getElementById("mcTerm") != null) {
document.getElementById("mcTerm").remove();
}
while (document.getElementById("mcDiv") != null) {
document.getElementById("mcDiv").remove();
}
console.log(document.getElementById("mcTerm"));
}
function deleteTyped() {
const defHeader = document.getElementById("defHeader");
const termInput = document.getElementById("termInput");
const showAnswer = document.getElementById("showAnswer");
while (document.getElementById("defHeader") != null) {
defHeader.remove();
termInput.remove();
}
while (document.getElementById("showAnswer") != null) {
showAnswer.remove();
}
}
//Database Manipulation Functions------------------------------------------------------------------->
//get the value at a certain path
function getData(path){
//get deckList data from firebase
const dbref = ref(db);
get(child(dbref, path)).then((snapshot)=> {
if(snapshot.exists()) {
data = snapshot.val();
console.log("Data within get Data "+ data);
const temp = document.getElementById("studyScreenHeader");
return data;
}
else {
console.log("There be no cards here")
}
});
}
//create Deck in Database
function deckToDatabase(userUid, deckName) {
update(ref(db, 'users/' + userUid +'/' + convertToPath(deckName)), {
cardList: cardList,
definitionList: definitionList
});
//is this the first deck?
if (deckList[0] == "Empty") {
deckList[0] = deckName;
}
else {
//update the lists of deck for the user locally and on server
deckList.push(deckName);
}
update(ref(db, 'users/' + userUid), {
deckList: deckList
});
}
//create card in deck in database
function cardToDeck(userUid, deckName, cardName, cardDefinition) {
console.log(convertToPath(cardName));
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName)), {
name: cardName,
definition: cardDefinition,
mc: 0,
typed: 0
});
//is this the first card?
if (cardList[0] == "Empty") {
cardList[0] = cardName;
definitionList[0] = cardDefinition;
}
else {
//update the lists of deck for the user locally and on server
cardList.push(cardName);
definitionList.push(cardDefinition);
}
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName)), {
cardList: cardList,
definitionList: definitionList
});
}
//delete deck from the database
function deleteDeck(userUid, deckName) {
remove(ref(db, 'users/' + userUid + '/' + convertToPath(deckName)));
var index = deckList.indexOf(deckName);
if (index > -1) {
deckList.splice(index, 1);
console.log("Deck Deleted From Array");
}
//update on firebase
update(ref(db, 'users/' + userUid), {
deckList: deckList
});
deleteContents();
populateLists();
cardList = ["Empty"];
definitionList = ["Empty"];
currDeck = null;
}
//delete card from deck in database
function deleteCard(userUid, deckName, cardName) {
remove(ref(db, 'users/' + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName)))
var index = cardList.indexOf(cardName);
if (index > -1) {
cardList.splice(index, 1);
definitionList.splice(index, 1);
console.log("Card Deleted From Array");
}
//TODO: Delete card list in gui and re populate it
//update on firebase
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName)), {
cardList: cardList,
definitionList: definitionList
});
deleteEditorContents();
populateCardLists();
}
//set the card's multiple choice value
function setCardMC(userUid, deckName, cardName, value) {
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName)), {
mc: value
});
}
function getCardMC(userUid, deckName, cardName) {
const path = "users/" + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName) + "/mc";
data = getData(path);
return data;
}
//set the card's typed value
function setCardTyped(userUid, deckName, cardName, value) {
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName) + '/' + cardName), {
typed: value
});
setCardMC(userUid, deckName, cardName, value);
}
function getCardTyped(userUid, deckName, cardName) {
const path = "users/" + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName) + '/typed';
data = getData(path);
return data;
}
//change the definition on a specific card
function setCardDefinition(userUid, deckName, cardName, cardDefinition) {
update(ref(db, 'users/' + userUid + '/' + convertToPath(deckName) + '/' + convertToPath(cardName)), {
definition: cardDefinition
});
}
// --------------------------------------------------------------------------------------
//Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
//Parser Functions for User Input
function convertToPath(string) {
length = string.length;
for (let i = 0; i < length; i++) {
string = string.replace('.', '--PERIOD--');
string = string.replace('#', '--HASHTAG--');
string = string.replace('$', '--DOLLAR--');
string = string.replace('[', '--LEFTBRACKET--');
string = string.replace(']', '--RIGHTBRACKET--');
string = string.replace('\n', '--NEWLINE--')
};
return(string);
}
function convertToText(string) {
length = string.length;
for (let i = 0; i < length; i++) {
string = string.replace('--PERIOD--', '.');
string = string.replace('--HASHTAG--', '#');
string = string.replace('--DOLLAR--', '$');
string = string.replace('--LEFTBRACKET--', '[');
string = string.replace('--RIGHTBRACKET--', ']');
//convert all line breaks to HTML
string = string.replace('--NEWLINE--', '<br>')
string = string.replace('\n', '<br>');
};
return(string);
}
function convertToTextCompare(string) {
length = string.length;
for (let i = 0; i < length; i++) {
string = string.replace('--PERIOD--', '.');
string = string.replace('--HASHTAG--', '#');
string = string.replace('--DOLLAR--', '$');
string = string.replace('--LEFTBRACKET--', '[');
string = string.replace('--RIGHTBRACKET--', ']');
//convert all line breaks to HTML
string = string.replace('--NEWLINE--', '')
string = string.replace('\n', '');
};
return(string);
}
//random number generator
function generateRandomIntegerInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//Delete local variables
function deleteGlobalVariables() {
deckList = ["Empty"];
cardList = ["Empty"];
definitionList = ["Empty"];
currDeck = null;
guest = false;