-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
1005 lines (852 loc) · 29.6 KB
/
script.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
let root_url = "http://comp426.cs.unc.edu:3001/";
let city = '';
let trip_type = "round trip";
let airport_cities = [];
let airport_codes = [];
let select1, select2 = ''; //selected to and return flights
let trips = [];
let date1, date2 = '';
/* Apply zomato API and impliment loadMyTrip() and loadRestaurant() functions (12/10/2018 updated by jie)*/
var zomato_url = "https://developers.zomato.com/api/v2.1/";
var zomato_key = "2ee136670cff3a1bd2e4a6d8427f1e36"; //as obtained from [Zomato API](https://developers.zomato.com/apis)
var map;
var center_lat = 38.889931
var center_lng = 77.009003;
var markers, infoWindowContent;
var restSaved = [];
$(document).ready(() => {
getAirports();
$("#login").on("click", () => {
let data = {
user: {
username: $("#user").val(),
password: $("#pass").val()
}
};
$.ajax(root_url + "/sessions", {
type: 'POST',
data: data,
xhrFields: {
withCredentials: true
},
success: () => {
loadHome();
},
error: function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 0) {
$("#status").html(
'<strong class="text-danger">Unable to reach server. Make sure you are online. If you are off-campus, make sure you are connected to the VPN.</strong>'
);
} else if (jqXHR.status === 401) {
$("#status").html(
'<strong class="text-danger">Incorrect username and/or password.</strong>'
);
} else {
$("#status").html(
'<strong class="text-danger">An unknown error occurred.</strong>'
);
}
}
});
});
$(".city-block p").on("click", () => {
loadCity($(event.target).html());
});
$(".nav-home").on("click", () => {
loadHome();
});
$(".nav-myTrips").on("click", () => {
loadMyTrip();
});
$("#flight").on("click", () => {
loadFlight();
});
$('#create').on('click', (e) => {
let trip = '';
let target = $(e.target);
if (trip_type == 'round trip') {
let flight1 = $($('#selected1').parents('.flight')[0]);
let flight2 = $($('#selected2').parents('.flight')[0]);
trip = new RoundTrip(flight1.attr('airport1'), flight1.attr('airport2'),
date1, date2, flight1.attr('number'), flight2.attr('number'),
$(flight1.children('.date1')[0]).html(), $(flight1.children('.date2')[0]).html(),
$(flight2.children('.date1')[0]).html(), $(flight2.children('.date2')[0]).html());
trips.push(trip);
console.log("adding " + trip.airport1 + "___to___"+ trips);
console.log("adding " + trip.airport2 + "___to___"+ trips);
} else {
let flight = $($('#selected1').parents('.flight')[0]);
trip = new SingleTrip(flight.attr('airport1'), flight.attr('airport2'), date1,
flight.attr('number'), $(flight.children('.date1')[0]).html(),
$(flight.children('.date2')[0]).html());
trips.push(trip);
console.log("adding " + trip.airport1 + "___to___"+ trips);
console.log("adding " + trip.airport2 + "___to___"+ trips);
}
console.log(trip);
});
$('input[type=radio][name=flight]').change(function() {
$('#flight2').hide();
$('#flight1').show();
$('#prev').hide();
if (this.value == 'round-trip') {
$('#flight').html('Flight: Departure');
$('#return-date').show();
trip_type = 'round trip';
$('#create').hide();
if ($('#selected')) {
$('#next-return').show();
}
} else if (this.value == 'one-way') {
$('#flight').html('Flight');
$('#return-date').hide();
trip_type = 'one way';
$('#flight1').show();
$('#flight2').hide();
$('#next-return').hide();
if ($('#selected')) {
$('#create').show();
}
}
});
$("#search").on("click", () => {
$('#buttom-b').children().each(function() {
$(this).hide();
});
$('#flight2').html('');
findFlight(1);
});
$('#next-return').on("click", () => {
$('#next-return').hide();
$('#prev').show();
//make sure do not load again
if ($('#flight2').children().length == 0) {
findFlight(2);
} else {
$('#flight1').hide();
$('#flight2').show();
if ($('#selected')) {
$('#create').show();
}
}
$('#flight').html('Flight: Arrival');
});
$('#prev').on('click', () => {
$('#prev').hide();
$('#create').hide();
$('#flight1').show();
$('#flight2').hide();
$('#next-return').show();
});
$('body').on('click', 'button', (e) => {
let target = $(e.target);
if (target.html() == 'Select') {
if ($('#flight1').css('display') == 'block') {
select1 = target;
if (trip_type == 'round trip') {
$('#next-return').show();
} else {
$('#create').show();
}
$('#selected1').html('Select');
$('#selected1').parent().css("background-color", "#fbf9f1");
$('#selected1').removeAttr('id');
target.html('Selected');
target.attr('id', 'selected1');
target.parent().css("background-color", "#bcbaba");
} else {
select2 = target;
$('#create').show();
$('#selected2').html('Select');
$('#selected2').parent().css("background-color", "#fbf9f1");
$('#selected2').removeAttr('id');
target.html('Selected');
target.attr('id', 'selected2');
target.parent().css("background-color", "#bcbaba");
}
}
});
});
function loadHome() {
$("#home-page").show();
$("#city-page").hide();
$(".login").hide();
$(".cities").show();
$("#nav").show();
//upon clicking, build a new interface: myTrip
$(".nav-myTrips").on("click", () => {
loadMyTrip();
});
}
/* loadMyTrip(): when clicked on MyTrip buttom (12/11/2018 updated by jie)
-- (1) show saved flights
-- (2) show saved restaurants */
function loadMyTrip () {
$('body').empty();
$("#home-page").hide();
$("#city-page").hide();
$("#trip-page").show();
$("#trip-page").append("<div class = 'trip-panel'></div>");
let tpanel = $(".trip-panel");
for (let i=0; i<trips.length; i++){
var cityName;
tpanel.append("<div class = 'flight-mask' id = 'fmId_"+i+"'></div>");
let fi = $("#fmId_"+i);
//append images to each saved flight
console.log("trip[i].airport1: " + trip[i].airport1);
console.log("trip[i].airport2: " + trip[i].airport2);
if (trip[i].airport2 == "SFO") {
cityName = "San Francisco";
fi.css("background-image", "url(pic/sfo.jpg)");
} else if (trip[i].airport2 == "JFK" || trip[i].airport2 == "LGA" || trip[i].airport2 == "NOP"
|| trip[i].airport2 == "TSS" || trip[i].airport2 == "JRA") {
cityName = "New York City";
fi.css("background-image", "url(pic/nyc.jpg)");
} else if (trip[i].airport2 == "ORD" || trip[i].airport2 == "MDW") {
cityName = "Chicago";
fi.css("background-image", "url(pic/chi.jpeg)");
} else if (trip[i].airport2 == "LAX" || trip[i].airport2 == "LGB" || trip[i].airport2 == "BUR"
|| trip[i].airport2 == "VNY") {
cityName = "Los Angeles";
fi.css("background-image", "url(pic/la.jpg)");
}
fi.append("<h3>"+trips[i].airport1+" to "+trips[i].airport2 +"</h3>");
fi.append("<div id = 'nav-trip_"+i+"'><p class='savedFlight'>"
+ trips[i].date1 + " " + trips[i].time11 + "" + trips[i].time12 + " " + trips[i].number1
+"</p></div>");
if(trips[i] instanceof RoundTrip){
$("#nav-trip_"+i).append(
"<p class='savedFlight'>"
+ trips[i].date2 + " " + trips[i].time21 + "" + trips[i].time22 + " " + trips[i].number2
+"</p>");
}
//append restaurants
fi.append("<button class = 'button-savedRest' id = 'bRid_" + i + "'>Click to view saved restaurants at this destination</button>");
fi.append("<div class = 'saved-rest' id = 'fir_" + i + "' display = 'none'></div>");
let bRid = $("bRid_"+i);
let fir = $(".fir_"+i);
for (let j=0; j<restSaved.length; j++){
if(restSaved[i].city == cityName){
fir.append(
"<div class='fir-header'>"
+ restSaved[i].name+" "+ restSaved[i].cuisine
+ " "+ restSaved[i].rate
+"<br>"+restSaved[i].address
+ "</div>"
);
}
}
bRid.on('click', () => {
fir.toggle();
});
}
}
function loadCity(cityName) {
$("#home-page").hide();
$("#city-page").show();
$("#city-header-title").text(cityName);
if (cityName == "San Francisco") {
city = 'San Francisco';
$(".city").css("background-image", "url(pic/sfo.jpg)");
} else if (cityName == "New York") {
city = 'New York';
$(".city").css("background-image", "url(pic/nyc.jpg)");
} else if (cityName == "Chicago") {
city = 'Chicago';
$(".city").css("background-image", "url(pic/chi.jpeg)");
} else if (cityName == "Los Angeles") {
city = 'Los Angeles';
$(".city").css("background-image", "url(pic/la.jpg)");
}
autocomplete(document.getElementById("depart_val"), airport_cities);
$("#rest").on("click", () => {
loadRestaurant(cityName);
});
}
/* loadRestaurant(): when clicked on restaurant buttom (12/10/2018 updated by jie)
-- (1) restaurantList(): list restaurants
-- (2) setMapRestMarkers(): show restaurant markers on the map and 'save to MyTrip' */
function loadRestaurant(cityName) {
$("#rest").css("background-color", "#c8255b");
$("#flight").css("background-color", "#86193d");
$('#flight-result').hide();
$('#rest-result').show();
$('#buttom-b').hide();
$('#restBar').show();
$('#flightBar').hide();
//try getting entity_id and entity_type from the city
$.ajax(zomato_url + 'locations?query=' + cityName, {
type: "GET", //send it through get method
dataType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
"user-key": zomato_key
},
success: function(response) {
// console.log(response.location_suggestions[0].entity_id);
// console.log(response.location_suggestions[0].entity_type);
center_lat = response.location_suggestions[0].latitude;
center_lng = response.location_suggestions[0].longitude;
console.log(center_lat);
console.log(center_lng);
setMapCenterMarker(cityName);
restaurantDetails(response.location_suggestions[0].entity_id,
response.location_suggestions[0].entity_type);
},
error: (jqxhr, status, error) => {
alert(error);
}
});
}
/* restaurantDetails(): list restaurants (12/10/2018 updated by jie)*/
function restaurantDetails(entity_id, entity_type) {
let rlist = $('#rest-result');
rlist.append("<div class = 'container rest-panel' hidden = 'true'></div>");
let rpanel = $('.rest-panel');
var rnearby_array, rbest_objs;
$.ajax(zomato_url + "location_details?entity_id=" + entity_id + "&entity_type=" + entity_type, {
type: "GET",
dataType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
"user-key": zomato_key
},
success: function(response) {
rnearby_array = response.nearby_res;
rbest_objs = response.best_rated_restaurant;
},
error: (jqxhr, status, error) => {
alert(error);
}
});
//list 5 nearby restaurants
$("#rest-nearby").on('click', () => {
rpanel.empty();
rpanel.show();
markers = [];
infoWindowContent = [];
for (let i = 0; i < Math.min(5, rnearby_array.length); i++) {
$.ajax(zomato_url + "restaurant?res_id=" + rnearby_array[i], {
type: "GET", //send it through get method
dataType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
"user-key": zomato_key
},
success: function(response) {
//show map; infor = name, cuise, price, url, rate; + = add to mytrip;
rpanel.append(
"<div class='rest-header' id='rid_" + i + "'>" +
"NAME: " + response.name + "***CUISINES: " + response.cuisines +
"***PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.rating_text +
" (" + response.user_rating.votes + " votes)</div>"
);
markers.push([response.name, response.location.latitude, response.location.longitude]);
infoWindowContent.push(
['<div class="info_content">' +
'<h3>' + response.name + '</h3>' +
'<p>CUISINES: ' + response.cuisines +
"<br>PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.aggregate_rating + " - " + response.user_rating.rating_text +
" (" + response.user_rating.votes + ' votes)</p>' +
'</div>'
]);
},
error: (jqxhr, status, error) => {
alert(error);
}
});
}
// console.log(markers);
// console.log(infoWindowContent);
setTimeout(setMapRestMarkers, 2000);
})
//list 5 best-rated restaurants
$("#rest-best").on('click', () => {
rpanel.empty();
rpanel.show();
markers = [];
infoWindowContent = [];
for (let i = 0; i < Math.min(5, rnearby_array.length); i++) {
$.ajax(zomato_url + "restaurant?res_id=" + rbest_objs[i].restaurant.R.res_id, {
type: "GET", //send it through get method
dataType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
"user-key": zomato_key
},
success: function(response) {
//show map; infor = name, cuise, price, rate; + = add to mytrip;
rpanel.append(
"<div class='rest-header' id='rid_" + i + "'>" +
"NAME: " + response.name + "***CUISINES: " + response.cuisines +
"***PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.rating_text +
" (" + response.user_rating.votes + " votes)</div>"
);
markers.push([response.name, response.location.latitude, response.location.longitude]);
infoWindowContent.push(
['<div class="info_content">' +
'<h3>' + response.name + '</h3>' +
'<p>CUISINES: ' + response.cuisines +
"<br>PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.aggregate_rating + " - " + response.user_rating.rating_text +
" (" + response.user_rating.votes + ' votes)</p>' +
'</div>'
]);
},
error: (jqxhr, status, error) => {
alert(error);
}
});
}
// console.log(markers);
// console.log(infoWindowContent);
setTimeout(setMapRestMarkers, 2000);
})
//list 5 choices for given cuisines
$("#rest-search").on('click', () => {
rpanel.empty();
rpanel.show();
markers = [];
infoWindowContent = [];
let q = $('#rest-search-val').val();
$.ajax(zomato_url + "search?entity_id=" + entity_id + "&entity_type=" + entity_type + "&q=" + q, {
type: "GET", //send it through get method
dataType: 'json',
xhrFields: {
withCredentials: false
},
headers: {
"user-key": zomato_key
},
success: function(response) {
//show map; infor = name, cuise, price, rate; + = add to mytrip;
let temp = response;
for (let i = 0; i < Math.min(5, temp.restaurants.length); i++) {
response = temp.restaurants[i].restaurant;
rpanel.append(
"<div class='rest-header' id='rid_" + i + "'>" +
"NAME: " + response.name + "***CUISINES: " + response.cuisines +
"***PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.rating_text +
" (" + response.user_rating.votes + " votes)</div>"
);
markers.push([response.name, response.location.latitude, response.location.longitude]);
infoWindowContent.push(
['<div class="info_content">' +
'<h3>' + response.name + '</h3>' +
'<p>CUISINES: ' + response.cuisines +
"<br>PRICE RANGE: " + response.price_range + response.currency +
"<br>RATING: " + response.user_rating.aggregate_rating + " - " + response.user_rating.rating_text +
" (" + response.user_rating.votes + ' votes)</p>' +
'</div>'
]);
}
},
error: (jqxhr, status, error) => {
alert(error);
}
});
// console.log(markers);
// console.log(infoWindowContent);
setTimeout(setMapRestMarkers, 2000);
})
}
/* attach markers on map for restaurants (12/11/2018) */
function setMapCenterMarker(cityName) {
var myLatlng = new google.maps.LatLng(center_lat, center_lng);
var mapOptions = {
zoom: 4,
center: myLatlng
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title: cityName
});
// To add the marker to the map, call setMap();
marker.setMap(map);
marker.addListener('click', () => {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
});
}
function setMapRestMarkers() {
var bounds = new google.maps.LatLngBounds();
var myLatlng = new google.maps.LatLng(center_lat, center_lng);
var mapOptions = {
zoom: 10,
center: myLatlng
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map"), mapOptions);
map.setTilt(45);
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow();
var marker;
// console.log(markers);
// console.log(markers.length);
// console.log(infoWindowContent);
// Loop through our array of markers & place each one on the map
for (let i = 0; i < markers.length; i++) {
setTimeout(function() {
// console.log(markers[i][1]+" "+markers[i][2]+" "+markers[i][0]);
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
animation: google.maps.Animation.DROP,
title: markers[i][0]
});
marker.setMap(map);
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}, i * 2000);
}
}
function initMap() {
// The map, centered at map_center
map = new google.maps.Map(
document.getElementById('map'), {
zoom: 4,
center: {
lat: center_lat,
lng: center_lng
}
});
}
function loadFlight() {
$("#rest").css("background-color", "#86193d");
$("#flight").css("background-color", "#c8255b");
$('#flight-result').show();
$('#rest-result').hide();
$('#buttom-b').show();
$('#restBar').hide();
$('#flightBar').show();
}
function findFlight(number) {
loadFlight();
let date, airport, flight, logoURL, airlineName, flightDiv = '';
let arrivalId, departId = [];
let flights = [];
let flight_result_div = '';
airport = $('#depart_val').val();
if (airport == '') {
alert('Please add airports');
return;
}
if (number == 1) {
flight_result_div = $('#flight1');
$('#flight2').hide();
flight_result_div.show();
date1 = $('#date-1').val();
date = date1;
//arrivalId=findAirportId(airport);
findAirportId(airport, function(output) {
arrivalId = output;
});
findAirportId(city, function(output) {
departId = output;
});
} else {
$('#prev').show();
$('#flight1').hide();
flight_result_div = $('#flight2');
flight_result_div.show();
date2 = $('#date-2').val();
date = date2;
findAirportId(airport, function(output) {
departId = output;
});
findAirportId(city, function(output) {
arrivalId = output;
});
}
flight_result_div.html('');
$('#loading').show();
// Find instances on that day
$.ajax(root_url + "/instances?filter[date]=" + date + "&filter[is_cancelled]=false", {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
flight_result_div.html('');
if (response != null && response != '' && departId != null && arrivalId != null) {
//alert("here3 "+response.length+" "+departId.length+" "+arrivalId.length);
console.log(response.length);
for (let i = 0; i < response.length; i++) {
getFlight(response[i].flight_id, function(output) {
flight = output;
for (let j = 0; j < departId.length; j++) {
for (let k = 0; k < arrivalId.length; k++) {
if (flight.departure_id == departId[j].id && flight.arrival_id == arrivalId[k].id) {
console.log(flight);
flightDiv = $('<div class="flight" id=f' + response[i].flight_id + ' airport1=' + departId[j].code + ' airport2=' + arrivalId[k].code + ' number=' + flight.number + '></div>');
getAirlineInfo(flight.airline_id, flightDiv, function(output) {
$('#loading').hide();
flightDiv = output;
flightDiv.append('<span class="date1">' + ' Departs at: ' + flight.departs_at.substr(11, 5) + '</span><span class="date2">' + ' Arrives at: ' + flight.arrives_at.substr(11, 5) + '</span>');
flightDiv.append('<button class="select">Select</button>');
flight_result_div.append(output);
});
flights.push(flight);
}
}
}
});
}
} else {
$("#result").html("Uh oh, no instances found. Try departing from different city or choose a different date.");
}
},
error: function(jqXHR, textStatus, errorThrown) {
$("#result").html("Uh oh, no instances found. Try departing from different city or choose a different date.");
}
});
}
function getFlight(id, handleData) {
let url = root_url + "/flights/" + id;
//console.log(url);
$.ajax(url, {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
if (response != null && response != '') {
handleData(response);
} else {
console.log("no suitable flight");
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("no suitable flight");
}
});
}
function getAirports(flight_id, departId, arrivalId) {
$.ajax(root_url + "airports", {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
if (response != null || response != '') {
for (let i = 0; i < response.length; i++) {
airport_cities.push(response[i].city);
airport_codes.push(response[i].code);
}
console.log(airport_cities);
} else {
alert("Can't get airports!");
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Can't get airports!");
}
});
}
function findAirportId(nameOrCode, handleData) {
let airportId = [];
$.ajax(root_url + "/airports?filter[city]=" + nameOrCode, {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
//console.log(response);
if (response != null || response != '') {
handleData(response);
} else {
alert("Can't get airort ID!");
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Can't get airort ID!");
}
});
/* $.ajax(root_url + "/airports?filter[code]="+nameOrCode, {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
if(response!=null || response!=''){
airportId=response[0].id;
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Can't get airort ID!");
}
}); */
}
function getAirlineInfo(id, flightDiv, handleData) {
$.ajax(root_url + "/airlines/" + id, {
type: 'GET',
xhrFields: {
withCredentials: true
},
success: (response) => {
console.log(response);
if (response != '') {
response.name;
flightDiv.append($('<span class="airlineName">' + response.name + ' </span>'));
handleData(flightDiv);
} else {
flightDiv.html('No Airline Info Found');
handleData(flightDiv);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('Find Airline Error');
}
});
}
//when round trip
let RoundTrip = function(airport1, airport2, date1, date2, number1, number2, time11, time12, time21, time22) {
this.airport1 = airport1;
this.airport2 = airport2;
this.date1 = date1;
this.date2 = date2;
this.number1 = number1;
this.number2 = number2;
this.time11 = time11;
this.time12 = time12;
this.time21 = time21;
this.time22 = time22;
}
//when one way trip
let SingleTrip = function(airport1, airport2, date1, number1, time11, time12) {
this.airport1 = airport1;
this.airport2 = airport2;
this.date1 = date1;
this.number1 = number1;
this.time11 = time11;
this.time12 = time12;
}
//-------------------------------------------------------------------auto complete part
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) {
return false;
}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}