-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
183 lines (164 loc) · 4.83 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
MBTA_API_KEY = 'dLQxHTh91UuZU7ks7OLwMQ';
MBTA_API_URL = 'http://realtime.mbta.com/developer/api/v2/';
DEFAULT_ROUTE = 'All';
ERRORS_SEEN = {};
// sequence for select menu
ROUTE_ORDER = ['All', 'Red', 'Orange', 'Blue',
'Green-B', 'Green-C', 'Green-D', 'Green-E',
701, 747,
66, 741, 742];
// map key onto properties
ROUTES = { 'All': {name: 'All'},
'Green-B': {name:'Green-B', color:'green'},
'Green-C': {name:'Green-C', color:'green'},
'Green-D': {name:'Green-D', color:'green'},
'Green-E': {name:'Green-E', color:'green'},
'Red': {name:'Red', color:'red'},
'Orange': {name:'Orange', color:'orange'},
'Blue':{name:'Blue', color:'blue'},
'66':{name:'66', color:'#BBB'},
'701':{name:'CT1', color:'#BBB'},
'747':{name:'CT2', color:'#BBB'},
'741': {name: 'Silver SL1', color: '#BBB'},
'742': {name: 'Silver SL2', color: '#BBB'}
};
// MAP_P is a promise that waits for the map to be loaded.
// When map is loaded, store it in document.MAP
MAP_P = $.Deferred().done(init_map);
$(document).data('MAP_P', MAP_P);
$(document).ready(function(){
init_routes();
$('button#refresh').click(plot_mbta);
$('#routes').change(plot_mbta);
plot_mbta();});
function init_map(map){
$(document).data('MAP', map);
}
function change_route(){
var val = null;
vv = this;
if (this == window){
val = DEFAULT_ROUTE;
} else {
val = $(this).attr('value');
}
$('#routes').attr('value', val);
$('#routes a.routename').text(val);
}
// reset menu of available routes
function init_routes(){
$('li#routes ul li').remove();
for (var i in ROUTE_ORDER){
var key = ROUTE_ORDER[i];
var val = ROUTES[key]['name'];
var option = $('<option/>', {value:key}).html(val);
if (DEFAULT_ROUTE == key){
option.attr('selected', true);
}
$('#routes').append(option);
}
}
function plot_mbta() {
deleteMarkers();
update_my_position(); // refresh 'you are here' marker position
var route = $('#routes').val();
if (route == 'All'){
for (var i in ROUTE_ORDER){
var r = ROUTE_ORDER[i];
if (r == 'All'){
continue;
}
plot_one_route(r);
}
} else {
plot_one_route(route);
}
}
function plot_one_route(route){
var params = { route: route,
api_key: MBTA_API_KEY,
jsonpcallback: 'handle_callback',
format: 'jsonp'
};
var url = get_mbta_api_url('predictionsbyroute', params);
$.getJSON(url).always(handle_api);
}
// fcn = 'predictionsbyroute'
// params is an object
function get_mbta_api_url(fcn, params){
return MBTA_API_URL + fcn + '?' + $.param( params );
}
function handle_api(data){
if (data.status == 200){
var msg = data.responseText;
eval(msg);
} else {
var status_code = data.status;
var err = find_err_msg(data.responseText);
if (err == null){
err = HtmlEncode(data.responseText);
}
err = "MBTA API error (" + status_code + "): " + err;
show_alert_if_unseen(err, err);
}
}
function find_err_msg(msg){
var rx = /\<message\>(.*)\<\/message\>/;
var found = rx.exec(msg);
if (found != null){
return found[1];
}
}
function handle_callback (data){
MAP_P.done(function() {plot_data(data);});
}
function plot_data(data){
var route_name = data.route_name;
var route_type = data.route_type;
var mode_name = data.mode_name;
var directions = data.direction;
for (var dir_i in directions){
var dir = directions[dir_i];
var direction_name = dir.direction_name;
var trips = dir.trip;
for (var trip_i in trips){
var trip = trips[trip_i];
var trip_headsign = trip.trip_headsign;
var vehicle = trip.vehicle;
if (vehicle == undefined){
continue;
}
var route_id = data.route_id;
var bearing = vehicle.vehicle_bearing;
var id = vehicle.vehicle_id;
var lat = parseFloat(vehicle.vehicle_lat);
var lng = parseFloat(vehicle.vehicle_lon);
var timestamp = vehicle.vehicle_timestamp;
var title = trip_headsign + ' (' + direction_name + ')<br/>'
+ route_name + '<br/>'
+ '#' + id + ' (' + mode_name.toLowerCase() + ')';
drawTrainMarker(lat, lng, title, bearing, route_id, route_type);
}
}
}
// Logs msgs (using key) to avoid repeating it once seen.
function show_alert_if_unseen(msg, key){
var seen = ERRORS_SEEN[key];
if (seen != true){
show_alert(msg);
ERRORS_SEEN[key] = true;
}
}
function show_alert(msg){
var alert = $('<div/>', {role: 'alert'}).addClass('alert alert-warning alert-dismissable');
var button = $('<button/>', {type: 'button', 'data-dismiss':'alert', 'aria-label':'close'}).addClass('close');
var span = $('<span/>', {'aria-hidden':true}).html('×');
alert.append(button.append(span), msg);
$('nav').after(alert);
}
function HtmlEncode(s){
var el = document.createElement("div");
el.innerText = el.textContent = s;
s = el.innerHTML;
return s;
}