|
1 | 1 | let routes, segments, stops, vehicles, lastFullTransLocFetchTime,
|
2 | 2 | routeIdToRouteMap, stopIdToStopMap, vehicleIdToVehicleMap, oldVehicleIdToVehicleMap,
|
3 | 3 | selectedLayerId, selectedFeature, selectedPlaceSheet, shownRouteIds,
|
4 |
| - buildingsGeoJSON, parkingLotsGeoJSON, stopsGeoJSON; |
| 4 | + buildingsGeoJSON, parkingLotsGeoJSON, stopsGeoJSON, |
| 5 | + announcements; |
5 | 6 | let startPos = nbPos;
|
6 | 7 | if (document.cookie) {
|
7 | 8 | const campusCookie = document.cookie.split('; ').find(it => it.startsWith('campus'));
|
@@ -110,6 +111,7 @@ class RouteFilterControl {
|
110 | 111 | maxHeight: window.innerHeight * 0.9,
|
111 | 112 | modal: true,
|
112 | 113 | resizable: false,
|
| 114 | + title: 'Routes', |
113 | 115 | buttons: {
|
114 | 116 | 'None': () => {
|
115 | 117 | document.querySelectorAll('input[name="route"]').forEach(it => it.checked = false);
|
@@ -141,6 +143,43 @@ class RouteFilterControl {
|
141 | 143 | }
|
142 | 144 | }
|
143 | 145 |
|
| 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 | + |
144 | 183 | function onSearchClick(layerID, featureID) {
|
145 | 184 | const geoJsonToSearch = layerID === 'Rutgers buildings' ? buildingsGeoJSON
|
146 | 185 | : layerID === 'Rutgers parking lots' ? parkingLotsGeoJSON
|
@@ -390,6 +429,16 @@ async function refreshSegmentsVehiclesAndSelectedPlace(userChangedRoutesShown) {
|
390 | 429 | }
|
391 | 430 | }
|
392 | 431 |
|
| 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 | + |
393 | 442 | mapboxgl.accessToken = mapboxKey;
|
394 | 443 | const map = new mapboxgl.Map({
|
395 | 444 | container: 'map', // container ID
|
@@ -443,6 +492,8 @@ map.on('load', async () => {
|
443 | 492 |
|
444 | 493 | map.addControl(new RouteFilterControl(), 'top-left');
|
445 | 494 |
|
| 495 | + map.addControl(new AnnouncementsControl(), 'top-left'); |
| 496 | + |
446 | 497 | // Add controls to fly to NB/NWK/CMDN
|
447 | 498 | map.addControl(new FlyToCampusControl());
|
448 | 499 |
|
@@ -593,5 +644,19 @@ map.on('load', async () => {
|
593 | 644 | setTimeout(fetchBusStuff, 5000);
|
594 | 645 | }
|
595 | 646 |
|
| 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 | + |
596 | 660 | fetchBusStuff();
|
| 661 | + fetchAnnouncements(); |
597 | 662 | });
|
0 commit comments