Skip to content

Commit 08c8b1c

Browse files
committed
Add TransLoc announcements
1 parent bff28e2 commit 08c8b1c

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Things on the map you can tap:
1919
Other features:
2020

2121
- it shows line segments for bus routes! But only routes that currently have buses running, so it's not too cluttered
22+
- it shows [Rutgers bus service announcements](https://rutgers.transloc.com/announcements)
2223
- there's a button that lets you pick which routes will be shown in the rest of the app. This setting will be remembered
2324
for the next time you open XBus. E.g. if you uncheck the box for Route A, then:
2425
- you won't see the line segments for Route A on the map

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</head>
1717
<body>
1818
<div id="map"></div>
19-
<div id="dialog" title="Routes"></div>
19+
<div id="dialog"></div>
2020
<script src="const.js"></script>
2121
<script src="map.js"></script>
2222
</body>

map.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
let routes, segments, stops, vehicles, lastFullTransLocFetchTime,
22
routeIdToRouteMap, stopIdToStopMap, vehicleIdToVehicleMap, oldVehicleIdToVehicleMap,
33
selectedLayerId, selectedFeature, selectedPlaceSheet, shownRouteIds,
4-
buildingsGeoJSON, parkingLotsGeoJSON, stopsGeoJSON;
4+
buildingsGeoJSON, parkingLotsGeoJSON, stopsGeoJSON,
5+
announcements;
56
let startPos = nbPos;
67
if (document.cookie) {
78
const campusCookie = document.cookie.split('; ').find(it => it.startsWith('campus'));
@@ -110,6 +111,7 @@ class RouteFilterControl {
110111
maxHeight: window.innerHeight * 0.9,
111112
modal: true,
112113
resizable: false,
114+
title: 'Routes',
113115
buttons: {
114116
'None': () => {
115117
document.querySelectorAll('input[name="route"]').forEach(it => it.checked = false);
@@ -141,6 +143,43 @@ class RouteFilterControl {
141143
}
142144
}
143145

146+
class AnnouncementsControl {
147+
onAdd(map) {
148+
this._container = document.createElement('div');
149+
this._container.classList.add('mapboxgl-ctrl', 'mapboxgl-ctrl-group');
150+
this._container.addEventListener('contextmenu', (e) => e.preventDefault());
151+
152+
const button = domCreate('button', 'announcementsButton', 'custom-map-control-button', this._container);
153+
button.type = 'button';
154+
button.style.display = 'none';
155+
button.addEventListener('click', () => {
156+
const announcementsHtml = announcements.map(it => `<h3>${it.title}</h3>${it.html}`)
157+
.join('<br><hr><br>');
158+
$("#dialog")
159+
.html(announcementsHtml)
160+
.dialog({
161+
autoOpen: false,
162+
draggable: false,
163+
maxHeight: window.innerHeight * 0.9,
164+
modal: true,
165+
resizable: false,
166+
title: 'Announcements',
167+
buttons: {
168+
'Close': () => {
169+
$("#dialog").dialog('close');
170+
},
171+
},
172+
})
173+
.dialog('open');
174+
});
175+
return this._container;
176+
}
177+
178+
onRemove() {
179+
this._container.parentNode.removeChild(this._container);
180+
}
181+
}
182+
144183
function onSearchClick(layerID, featureID) {
145184
const geoJsonToSearch = layerID === 'Rutgers buildings' ? buildingsGeoJSON
146185
: layerID === 'Rutgers parking lots' ? parkingLotsGeoJSON
@@ -390,6 +429,16 @@ async function refreshSegmentsVehiclesAndSelectedPlace(userChangedRoutesShown) {
390429
}
391430
}
392431

432+
function refreshAnnouncementsButton() {
433+
const announcementsButton = document.getElementById('announcementsButton');
434+
if (announcements && announcements.length > 0) {
435+
announcementsButton.textContent = `Announcements (${announcements.length})`;
436+
announcementsButton.style.display = 'block';
437+
} else {
438+
announcementsButton.style.display = 'none';
439+
}
440+
}
441+
393442
mapboxgl.accessToken = mapboxKey;
394443
const map = new mapboxgl.Map({
395444
container: 'map', // container ID
@@ -443,6 +492,8 @@ map.on('load', async () => {
443492

444493
map.addControl(new RouteFilterControl(), 'top-left');
445494

495+
map.addControl(new AnnouncementsControl(), 'top-left');
496+
446497
// Add controls to fly to NB/NWK/CMDN
447498
map.addControl(new FlyToCampusControl());
448499

@@ -593,5 +644,19 @@ map.on('load', async () => {
593644
setTimeout(fetchBusStuff, 5000);
594645
}
595646

647+
async function fetchAnnouncements() {
648+
try {
649+
announcements = (await (await fetch('https://feeds.transloc.com/3/announcements?contents=true&agencies=1323')).json()).announcements;
650+
} catch (error) {
651+
setTimeout(fetchAnnouncements, 5 * 60 * 1000);
652+
return;
653+
}
654+
655+
refreshAnnouncementsButton();
656+
657+
setTimeout(fetchAnnouncements, 5 * 60 * 1000);
658+
}
659+
596660
fetchBusStuff();
661+
fetchAnnouncements();
597662
});

0 commit comments

Comments
 (0)