-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowse.js
More file actions
868 lines (554 loc) · 23.6 KB
/
browse.js
File metadata and controls
868 lines (554 loc) · 23.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
/**
* Title: browse.js
*
* Javascript for browse.html, used to store data for displaying and interactions
*/
//Scholarships JSON
const apiURL = "http://localhost:8080/scholarship"
//Bookmarks JSON
const bookURL = "http://localhost:8080/bookmarks"
//User JSON
const userURL = "http://localhost:8080/user"
//Vue Instance
new Vue({
el: '#app',
//Variables
data() {
return {
scholarships:[], //scholarships to display
searchQuery: '', //value from search
sorting: 'Alphabetical', //value from sorting
filterUniversity: [], //value from filtering university
filterDiscipline: [], //value from filtering discipline
filterRenewable: [], //value from filtering renewable
filterLevel: [], //value from filtering level
filterSupplemental: [], //value from filtering supplemental
active: '', //navigation
themeSwitch: false, //value from switch
bookmarked: [], //boolean array if scholarship has been bookmarked by the user
scholarshipclicked: 2, //id of scholarship clicked
bookmarks:[], //bookmarks by user
newBookmark: '', //Bookmark object of new bookmark to add
user: '', //user information
userInitial: '', //Initials for user's name
index: '', //index of scholarship in array
dynamicArray: [], //scholarships array that is displayed
thirdArray: [] //scholarship array that is being sorted, searched, and filtered from
}
},
//Created Lifecycle Hook
created() {
//Fetch Bookmarks
fetch (bookURL)
.then (response => {
return response.json();
})
.then (bookmarks => {
this.bookmarks = bookmarks;
//Fetch Scholarships
fetch (apiURL)
.then (response => {
return response.json();
})
.then (scholarships => {
this.scholarships = scholarships;
this.dynamicArray = this.scholarships;
this.thirdArray = this.scholarships;
/*
This is used to initalize the bookmarked array, which identifies whether the user has bookmarked a scholarship before or not
We go through the scholarship array and bookmark array to find matching ones, to set to true, if none is found, it is false
*/
for (var i = 0; i < this.scholarships.length; i++) {
for (var j = 0; j<this.bookmarks.length; j++) {
//same user id and same scholarship id makes bookmarked true, aids in initializing the bookmark icon as full.
if (this.scholarships[i].id == this.bookmarks[j].scholarshipID && this.bookmarks[j].userID == parseInt(localStorage.getItem("id"))) {
this.bookmarked[i] = true;
break;
}
//else, initialize the boolean array to false, which will aid in initializing the bookmark icon as empty.
else {
this.bookmarked[i] = false;
}
}
}
})
}),
//Fetch the logged-in user's information
fetch (userURL + "/" + localStorage.getItem("id"))
.then (response => {
return response.json();
})
//Use the user's name to create an initial to display on the avatar button
.then (user => {
this.user = user;
this.userInitial = this.user.name.substring(0,1).toUpperCase();
var a= this.user.name;
//If there is a space in their name (ex. user entered first and last name) make another initial for their last name
if (a.indexOf(" ") != -1) {
this.userInitial += this.user.name.charAt(this.user.name.indexOf(" ") + 1).toUpperCase();
a="";
}
}),
//sets theme for user
this.getTheme();
},
//methods
methods: {
//when a scholarship is clicked, finds index in array
setIndex(scholarship) {
for (var i = 0; i < this.scholarships.length; i++){
if (scholarship.id == this.scholarships[i].id) {
return i;
}
}
return 0;
},
//when a bookmark is clicked
buttonClicked(id) {
//passes in the id (position) of the scholarship the user would like to bookmark
const scholarship = this.scholarships.find(s => s.id === id);
//checks to see if any scholarship in the array has the same id as the one pass in
this.scholarshipclicked = scholarship.id;
//sets "index" variable by passing in the id (position) of the scholarship the user would like to bookmark
this.index = this.setIndex(scholarship);
//if the bookmark has been clicked or not, false means add bookmark, true means remove bookmark
if (!this.bookmarked[this.index]) {
//change scholarship numBookmarks in javascript
this.scholarships[this.index].numBookmarks += 1;
//change scholarship numBookmarks in java, calls the JSON of the scholarship based on ID
const scholarshipId = "http://localhost:8080/scholarship/" + id;
//patches/updates the number of bookmarks that the specific bookmark has
fetch(scholarshipId, {
method: "PATCH",
body: JSON.stringify ({
numBookmarks: this.scholarships[this.index].numBookmarks
}),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then (response => response.json())
.then (json => console.log(json))
//add bookmark to bookmark array in javascript
this.newBookmark = {id:this.bookmarks.length + 1, userID: parseInt(localStorage.getItem("id")), scholarshipID:id};
this.bookmarks.push(this.newBookmark);
//adds bookmark to java array
fetch (bookURL, {
method: "POST",
body: JSON.stringify ({
id: this.newBookmark.id,
userID: this.newBookmark.userID,
scholarshipID: this.newBookmark.scholarshipID
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then (response => response.json())
.then (json => console.log(json))
//change bookmarked
this.bookmarked[this.index] = true;
}
else {
//change numBookmarks for scholarship in javascript
this.scholarships[this.index].numBookmarks -=1;
//change numBookmarks for scholarship in java+database
const scholarshipId = "http://localhost:8080/scholarship/" + id;
fetch(scholarshipId, {
method: "PATCH",
body: JSON.stringify ({
numBookmarks: this.scholarships[this.index].numBookmarks
})
,
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then (response => response.json())
.then (json => console.log(json))
//after removing bookmark, all ids after bookmark need to shift down to keep things coherent
for (var i = 0; i <this.bookmarks.length; i++) {
if (scholarship.id == this.bookmarks[i].scholarshipID && parseInt(localStorage.getItem("id")) == this.bookmarks[i].userID) {
this.bookmarks.splice(i,1); //delete bookmark in javascript
//delete bookmark in java
fetch (bookURL + "/" + (i+1), {
method: "DELETE"
})
//change id of bookmarks in javascript
for (var j = i; j <this.bookmarks.length; j++){
this.bookmarks[j].id -=1;
//change id of bookmarks in java
fetch(bookURL + "/" + (j+2), {
method: "PATCH",
body: JSON.stringify ({
id: this.bookmarks[j].id
})
,
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then (response => response.json())
.then (json => console.log(json))
}
break;
}
}
//change bookmarked
this.bookmarked[this.index] = false;
}
},
/* When the user clicks on the button, toggle between hiding and showing the dropdown content */
profileDropdown() {
document.getElementById("myDropdown").classList.toggle("show");
},
//Logs the user out of the session and reflects this change in the localStorage
logout() {
localStorage.setItem("status", false);
window.location.replace("index.html");
},
//Switches Between Light Mode and Dark Mode
theme() {
document.body.classList.toggle('dark-theme'); //Class to change all elements
//LocalStorage Switches
if(localStorage.getItem("theme") == "light") {
localStorage.setItem("theme", "dark");
}
else if (localStorage.getItem("theme") == "dark") {
localStorage.setItem("theme", "light");
}
//Swap Footer Logo
if (document.getElementById("Footer-Logo").src == "/assets/darkmodelogo.png") {
document.getElementById("Footer-Logo").src = "/assets/scholarsearch.png";
}
else {
document.getElementById("Footer-Logo").src = "/assets/darkmodelogo.png";
}
},
//Retrieves User's choice of Theme
getTheme() {
try {
this.mode = localStorage.getItem("theme");
if (this.mode == "light") {
this.themeSwitch = false;
} else {
this.themeSwitch = true;
this.firstTime();
}
}
catch (err) {
this.mode = "light";
localStorage.setItem("theme", "light");
this.themeSwitch = false;
}
},
//Sets theme when first opened
firstTime() {
document.body.classList.toggle('dark-theme'); //Class to change all elements
//Swap Footer Logo
if (document.getElementById("Footer-Logo").src == "https://friendly-bell-b1c52f.netlify.app/assets/darkmodelogo.png") {
document.getElementById("Footer-Logo").src = "https://friendly-bell-b1c52f.netlify.app/assets/scholarsearch.png";
}
else {
document.getElementById("Footer-Logo").src = "https://friendly-bell-b1c52f.netlify.app/assets/darkmodelogo.png";
}
},
//Filters Everything After Filter Change
mainFilter() {
/*It first filters through university, adding matching scholarships from this.scholarships
to this.dynamicArray. After this however, all filters will be based on the resultant array so we created thirdArray
to filter from afterwards.*/
//UNIVERSITY
//sets dynamicArray empty so it can add only matching scholarships
this.dynamicArray = [];
//makes sure if a filter is set
if (this.filterUniversity.length != 0) {
//goes through filters selected
for (var i=0; i<this.filterUniversity.length; i++) {
//goes through all scholarships and adds them to dynamicArray if they match query
for (var j = 0; j < this.thirdArray.length; j++) {
if (this.filterUniversity[i] == this.thirdArray[j].university) {
this.dynamicArray.push(this.thirdArray[j]);
}
}
}
}
else {
this.dynamicArray = this.thirdArray.slice(0); //since no filters are set, dynamicArray is set to scholarships
}
//thirdArray is set to dynamicArray to filter from in the future
this.thirdArray = this.dynamicArray.slice(0);
//sets dynamicArray empty for next filter
this.dynamicArray = [];
//DISCIPLINE
//same process as above for filtering by university, but this time taking the already filtered array and narrowing it down even more
if (this.filterDiscipline.length != 0) {
for (var i = 0; i < this.filterDiscipline.length; i++) {
for (var j = 0; j < this.thirdArray.length; j++) {
if (this.filterDiscipline[i] == this.thirdArray[j].discipline) {
this.dynamicArray.push(this.thirdArray[j]);
}
}
}
}
else {
this.dynamicArray = this.thirdArray.slice(0);
}
this.thirdArray = this.dynamicArray.slice(0);
this.dynamicArray = [];
//RENEWABLE
if (this.filterRenewable.length != 0) {
for (var i = 0; i < this.filterRenewable.length; i++) {
for (var j = 0; j < this.thirdArray.length; j++) {
if (this.filterRenewable[i] == this.thirdArray[j].renewable) {
this.dynamicArray.push(this.thirdArray[j]);
}
}
}
}
else {
this.dynamicArray = this.thirdArray.slice(0);
}
this.thirdArray = this.dynamicArray.slice(0);
this.dynamicArray = [];
//LEVEL
if (this.filterLevel.length != 0) {
for (var i = 0; i < this.filterLevel.length; i++) {
for (var j = 0; j < this.thirdArray.length; j++) {
if (this.filterLevel[i] == this.thirdArray[j].level) {
this.dynamicArray.push(this.thirdArray[j]);
}
}
}
}
else {
this.dynamicArray = this.thirdArray.slice(0);
}
this.thirdArray = this.dynamicArray.slice(0);
this.dynamicArray = [];
//SUPPLEMENTAL
if (this.filterSupplemental.length != 0) {
for (var i = 0; i < this.filterSupplemental.length; i++) {
for (var j = 0; j < this.thirdArray.length; j++) {
if (this.filterSupplemental[i] == this.thirdArray[j].supplemental) {
this.dynamicArray.push(this.thirdArray[j]);
}
}
}
}
else {
this.dynamicArray = this.thirdArray.slice(0);
}
//Sort
this.sortArray();
},
//Sorts Array based on Preference
sortArray() {
//Switch For Different Sorting Strings
switch(this.sorting) {
//Sort By Closest Deadline
case "Closest Deadline":
//Gets Current Date
var cDate = new Date();
//Bubble Sort to Sort Array
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Compares Months of Deadlines, and puts No Deadline at end of array
if (Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j].date.substring(5,7))) < Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j + 1].date.substring(5,7))) || this.dynamicArray[j].date == "No Deadline") {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
//If Months are Same, compares day
else if(Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j].date.substring(5,7))) == Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j + 1].date.substring(5,7)))) {
if (Math.abs(cDate.getDate() - parseInt(this.dynamicArray[j].date.substring(8))) > Math.abs((cDate.getDate()) - parseInt(this.dynamicArray[j + 1].date.substring(8)))) {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
}
break;
//Sorts By Furthest Deadline
case "Furthest Deadline":
//retrieves the current date and assigns it to the cDate variable
var cDate = new Date();
//bubble sort to sort the array
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//checks to if see the MAGNITUDE/distance between deadline month and current month is greater or lesser between the 2 current elements
if (Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j].date.substring(5,7))) > Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j + 1].date.substring(5,7))) || this.dynamicArray[j+1].date == "No Deadline") {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
//if months are equal, check the deadline and current Date
else if(Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j].date.substring(5,7))) == Math.abs((cDate.getMonth() + 1) - parseInt(this.dynamicArray[j + 1].date.substring(5,7)))) {
if (Math.abs(cDate.getDate() - parseInt(this.dynamicArray[j].date.substring(8))) < Math.abs((cDate.getDate()) - parseInt(this.dynamicArray[j + 1].date.substring(8)))) {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
}
break;
//Sorts By Highest Value
case "Highest Value":
//Bubble Sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Swaps if Value is Higher
if (this.dynamicArray[j].value < this.dynamicArray[j+1].value) {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
//Sorts By Lowest Value
case "Lowest Value":
//Bubble Sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Swaps if Value is Lower
if (this.dynamicArray[j].value > this.dynamicArray[j+1].value) {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
//Sorts by Most Bookmarks
case "Highest Popularity":
//Bubble Sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Swaps if numBookmarks is Higher
if (this.dynamicArray[j].numBookmarks < this.dynamicArray[j+1].numBookmarks) {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
//Sorts by least amount of bookmarks
case "Lowest Popularity":
//Swaps if numBookmarks is lower
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
if (this.dynamicArray[j].numBookmarks > this.dynamicArray[j+1].numBookmarks) {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
//Sorts by University Name, then Scholarship Name
case "Alphabetical":
//Bubble Sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Compares University Name
if (this.dynamicArray[j].university.localeCompare(this.dynamicArray[j + 1].university) > 0) {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
else if (this.dynamicArray[j].university.localeCompare(this.dynamicArray[j + 1].university) == 0){
//Compares Scholarship Name
if (this.dynamicArray[j].name.localeCompare(this.dynamicArray[j + 1].name) > 0) {
var temp = this.dynamicArray[j + 1];
this.dynamicArray[j + 1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
}
break;
//Sorts by highest amount of spots available (Scholarships with unlimited spots will show up at the top)
case "Highest Availability":
//bubble sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Compares numSpots, swaps if higher
if (this.dynamicArray[j].numSpots < this.dynamicArray[j+1].numSpots || this.dynamicArray[j+1].numSpots == "Unlimited") {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
//Sorts by lowest amount of spots available (Scholarships with unlimited spots will be shown at the bottom)
case "Lowest Availability":
//Bubble Sort
for (var i = 0; i < this.dynamicArray.length; i++) {
for (var j = 0; j < this.dynamicArray.length - 1; j++) {
//Swaps if numSpots is lower or if numSpots is unlimited
if (this.dynamicArray[j].numSpots > this.dynamicArray[j+1].numSpots || this.dynamicArray[j].numSpots == "Unlimited") {
var temp = this.dynamicArray[j+1];
this.dynamicArray[j+1] = this.dynamicArray[j];
this.dynamicArray[j] = temp;
}
}
}
break;
}
},
//searchArray is used to search between scholarships, which then calls mainFilter() to filter and then sort
searchArray() {
this.dynamicArray = [];
if (this.searchQuery != "") {
//Goes Through Array to check if scholarship contains search query
for (var i = 0; i < this.scholarships.length; i++) {
if((this.scholarships[i].name.toUpperCase().includes(this.searchQuery.toUpperCase())) || (this.scholarships[i].university.toUpperCase().includes(this.searchQuery.toUpperCase()))) {
this.dynamicArray.push(this.scholarships[i]);
}
}
}
else {
this.dynamicArray = this.scholarships.slice(0);
}
this.thirdArray = this.dynamicArray.slice(0);
this.mainFilter();
},
expand(scholarshipid){
const scholarship = this.scholarships.find(s => s.id === scholarshipid);
localStorage.setItem("scholarship", scholarship.id);
window.location.href = ("scholarship.html");
}
},
//methods that run when any change in value of a specific variable has been detected
watch: {
//university filter is changed
filterUniversity: function() {
this.searchArray();
},
//discipline filter is changed
filterDiscipline: function() {
this.searchArray();
},
//renewable filter is changed
filterRenewable: function() {
this.searchArray();
},
//level filter is changed
filterLevel: function() {
this.searchArray();
},
//supplemental filter is changed
filterSupplemental: function() {
this.searchArray();
},
//sorting is changed
sorting: function() {
this.sortArray();
},
}
});