-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbta.js
61 lines (56 loc) · 2.4 KB
/
mbta.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
var populate_table = function ($table, data) {
var platformKeys = []
$.map($table.find('th.PlatformKey'), function(e) { platformKeys.push(e.id);});
// Get the trip ids for the starting station
var tripIdsFromStart = []
var tripsFromStart = []
data.map( function(element) {
if( element.PlatformKey === platformKeys[0] ) {
tripIdsFromStart.push(element.Trip);
tripsFromStart.push(element);
}
});
// Sort those by the time they arrive at the starting station
tripsFromStart.sort( function (a,b) {
var da = new Date(Date.parse(a.Time));
var db = new Date(Date.parse(b.Time));
if ( da > db ) return 1;
if ( da < db ) return -1;
return 0;
});
if ( tripIdsFromStart.length === 0) {
$table.after("<em>No times reported</em>");
} else {
// Get the info for each desired station for each trip that comes through the starting station
var trips = {}
data.map( function(element) {
if( platformKeys.indexOf(element.PlatformKey) >= 0 && tripIdsFromStart.indexOf(element.Trip) >= 0){
if( trips[element.Trip] === undefined) {
trips[element.Trip] = {}
}
trips[element.Trip][element.PlatformKey] = element;
}
});
// Create a row in the table for each train
$table.find('tr.arrival').remove();
for( var i in tripsFromStart) {
var trip = trips[tripsFromStart[i].Trip]
var $row = $("<tr class='arrival'></tr>");
for( j in platformKeys ) {
var platformKey = platformKeys[j];
var station = trip[platformKey];
time = new Date(Date.parse(station.Time));
var amPm = (time.getHours() > 11) ? "PM" : "AM";
var hours = time.getHours() === 0 ? 12 : time.getHours() % 12;
var minutes = (time.getMinutes() < 10) ? "0"+time.getMinutes() : ""+time.getMinutes();
var $cell = $("<td>"+hours+":"+minutes+" "+amPm+"</td>");
$cell.append('<br />')
$cell.append('<abbr class="timeago" title="'+time.toISOString()+'">'+station.TimeRemaining+'<abbr>');
$row.append($cell);
}
$table.append($row);
}
$.timeago.settings.allowFuture = true;
$("abbr.timeago").timeago();
}
}