-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
136 lines (97 loc) · 3.91 KB
/
logic.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
$(document).ready(function() {
//Firebase link
var config = {
apiKey: "AIzaSyC1zUvc5K6NJXFxwm74405gegKYfPSNT-4",
authDomain: "train-schedule-db.firebaseapp.com",
databaseURL: "https://train-schedule-db.firebaseio.com",
projectId: "train-schedule-db",
storageBucket: "train-schedule-db.appspot.com",
messagingSenderId: "60174437847"
};
firebase.initializeApp(config);
var database = firebase.database();
//collect input from the submit button and store it in specific variables
$(".submitInput").on("click", function (event) {
// console.log("this works");
var nameInput = $("#nameInput").val().trim();
var numberInput = $("#numberInput").val().trim();
var destinationInput = $("#destInput").val().trim();
var timeInput = $("#timeInput").val().trim();
var frequencyInput = $("#freqInput").val().trim();
//input validation
if (nameInput != "" &&
numberInput != "" &&
destinationInput != "" &&
timeInput.length === 4 &&
frequencyInput != "") {
//use the collected input (above) and port it to firebase db
database.ref().push({
name: nameInput,
number: numberInput,
destination: destinationInput,
time: timeInput,
frequency: frequencyInput,
});
} else {
alert("Please enter valid train data");
$("input").val("");
return false;
}
//console.log(database);
$("input").val("");
});
database.ref().on("child_added", function (childSnapshot) {
// console.log(childSnapshot.val());
var name = childSnapshot.val().name;
var number = childSnapshot.val().number;
var destination = childSnapshot.val().destination;
var time = childSnapshot.val().time;
var frequency = childSnapshot.val().frequency;
// console.log(name, number, destination, time, frequency);
//time formatting
//this required a LOT of googling to figure out
var frequency = parseInt(frequency);
var currentTime = moment();
//console.log("Current time: " + moment().format("HHmm"));
//originally used mil format of HHMM but that failed with a null value
//looked up potential faults and it turns out that moment.js must use
//HH:mm for mil/euro time format
var dateConvert = moment(childSnapshot.val().time, "HHmm").subtract(1, "years");
//console.log("DATE CONVERTED: " + dateConvert);
var trainTime = moment(dateConvert).format("HHmm");
//console.log("Train time : " + trainTime);
//difference bw the times
var timeConvert = moment(trainTime, "HHmm").subtract(1, "years");
var timeDifference = moment().diff(moment(timeConvert), "minutes");
//console.log("Difference in time: " + timeDifference);
//remainder
var timeRemaining = timeDifference % frequency;
//console.log("Time remaining: " + timeRemaining);
//time until next train
var timeAway = frequency - timeRemaining;
//console.log("Minutes until next train: " + timeAway);
//next train arrival
var nextArrival = moment().add(timeAway, "minutes");
//figured out that adding "A" at the end of HH:mm will add AM or PM
//given that this is mil/euro format, the AM/PM is not necessary
//console.log("Arrival time: " + moment(nextArrival).format("HHmm"));
var arrivalDisplay = moment(nextArrival).format("HHmm");
//append data to table
$("#boardText").append(
"<tr><td id='nameDisplay'>" + childSnapshot.val().name +
"<td id='numberDisplay'>" + childSnapshot.val().number +
"<td id='destinationDisplay'>" + childSnapshot.val().destination +
"<td id='frequencyDisplay'>" + childSnapshot.val().frequency +
"<td id='arrivalDisplay'>" + arrivalDisplay +
"<td id='awayDisplay'>" + timeAway + " minutes until arrival" + "</td></tr>");
// console.log(arrivalDisplay);
// console.log(timeAway);
});
//reset functionality
$(".resetInput").on("click", function(event){
location.reload();
});
//auto refresh per 1 minute passed
//updates the train data upon refresh
setInterval("window.location.reload()", 60000);
});