Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 13x 13x 8x 7x 8x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 3x 6x 3x 3x 6x 3x 5x 5x 5x 5x 1x 1x 4x 4x 5x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 3x 2x 2x 3x 3x 3x 3x 3x 3x 6x 3x 6x 6x 6x 6x 3x 5x 3x 5x 4x 3x 4x 4x 9x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { FeatureCollection, Point } from "geojson";
import { LngLatBounds, Map as MapboxMap, MapLayerMouseEvent, Popup } from "mapbox-gl";
import type { RouteConfigRoute as TrimetRoute } from "@overworld/trimet-client";
import { STOP_LOCATION_LAYER } from "./consts";
import { addMapboxLayer, removeRoutes } from "./routeLines";
import type { RouteGeometryLoader } from "./transitTypes";
const STOPS = "route-info-stops";
const routeInfoControls = new WeakMap<MapboxMap, { highlight: (stopId?: number) => void }>();
function paintHighlight(map: MapboxMap, stopId?: number) {
if (!map?.getLayer(STOPS)) return;
map.setPaintProperty(STOPS, "circle-color", stopId === undefined
? "#4264fb" : ["case", ["==", ["get", "locid"], stopId], "#ff0000", "#4264fb"]);
map.setPaintProperty(STOPS, "circle-radius", stopId === undefined
? 6 : ["case", ["==", ["get", "locid"], stopId], 12, 6]);
}
export function highlightRouteInfoStop(map: MapboxMap, stopId?: number) {
const controls = map && routeInfoControls.get(map);
if (controls) controls.highlight(stopId);
else paintHighlight(map, stopId);
}
export function showRouteInfo(
map: MapboxMap,
route: TrimetRoute,
onError: () => void,
getRouteGeometry: RouteGeometryLoader,
onStopClick?: (stopId: number) => void,
canFit = () => true,
isActive = () => true
): () => void {
const abort = new AbortController();
let closed = false;
const previousCenter = map.getCenter();
const previousZoom = map.getZoom();
const previousVisibility = map.getLayer(STOP_LOCATION_LAYER)
? map.getLayoutProperty(STOP_LOCATION_LAYER, "visibility") : undefined;
Eif (map.getLayer(STOP_LOCATION_LAYER)) {
map.setLayoutProperty(STOP_LOCATION_LAYER, "visibility", "none");
}
const stops = Array.from(new Map(route.dir.flatMap((direction) =>
direction.stop.filter((stop) => Number.isFinite(stop.lng) && Number.isFinite(stop.lat))
.map((stop) => [stop.locid, stop] as const)
)).values());
const data: FeatureCollection<Point> = {
type: "FeatureCollection",
features: stops.map((stop) => ({
type: "Feature",
geometry: { type: "Point", coordinates: [stop.lng, stop.lat] },
properties: { locid: stop.locid, desc: stop.desc }
}))
};
map.addSource(STOPS, { type: "geojson", data });
map.addLayer({
id: STOPS, type: "circle", source: STOPS,
paint: {
"circle-color": "#4264fb", "circle-emissive-strength": 1,
"circle-radius": 6, "circle-stroke-color": "#ffffff", "circle-stroke-width": 2
}
});
const stopsById = new Map(stops.map((stop) => [stop.locid, stop]));
const popup = new Popup({ closeButton: false, closeOnClick: false });
let listStop: number | undefined;
let mapStop: number | undefined;
let mapLongitude: number | undefined;
function updateHighlight() {
const id = mapStop ?? listStop;
paintHighlight(map, id);
const stop = id === undefined ? undefined : stopsById.get(id);
if (!stop) {
popup.remove();
return;
}
const coordinates: [number, number] = [stop.lng, stop.lat];
const longitude = mapStop === undefined ? map.getCenter().lng : mapLongitude;
while (longitude !== undefined && Math.abs(longitude - coordinates[0]) > 180) {
coordinates[0] += longitude > coordinates[0] ? 360 : -360;
}
popup.setLngLat(coordinates)
.setText(`${stop.desc}\nStop ID: ${stop.locid}\nRoute: ${route.route}`)
.addTo(map);
}
function handleMapHover(event: MapLayerMouseEvent) {
Iif (!isActive()) return;
const id = Number(event.features?.[0]?.properties?.locid);
mapStop = stopsById.has(id) ? id : undefined;
mapLongitude = event.lngLat.lng;
map.getCanvas().style.cursor = "pointer";
updateHighlight();
}
function handleMapLeave() {
mapStop = undefined;
mapLongitude = undefined;
map.getCanvas().style.cursor = "";
updateHighlight();
}
function handleMapClick(event: MapLayerMouseEvent) {
Iif (!isActive()) return;
const id = Number(event.features?.[0]?.properties?.locid);
if (stopsById.has(id)) onStopClick?.(id);
}
const controls = {
highlight: (stopId?: number) => {
listStop = stopId;
updateHighlight();
}
};
routeInfoControls.set(map, controls);
map.on("mouseenter", STOPS, handleMapHover);
map.on("mousemove", STOPS, handleMapHover);
map.on("mouseleave", STOPS, handleMapLeave);
map.on("click", STOPS, handleMapClick);
const bounds = new LngLatBounds();
stops.forEach((stop) => bounds.extend([stop.lng, stop.lat]));
const fit = () => {
Iif (!canFit() || bounds.isEmpty()) return;
const container = map.getContainer();
const horizontal = Math.min(48, container.clientWidth / 5);
map.fitBounds(bounds, {
padding: { left: horizontal, right: horizontal, top: Math.min(48, container.clientHeight / 5), bottom: Math.min(48, container.clientHeight / 5) },
maxZoom: 16, duration: 500
});
};
fit();
const ids = route.dir.map((direction) => `route-info-${direction.dir}`);
route.dir.forEach((direction, index) => {
getRouteGeometry(String(route.route), direction.dir, abort.signal).then((feature) => {
if (closed) return;
addMapboxLayer(map, feature, ids[index], {
"line-color": index % 2 ? "#c55316" : "#0080ff",
"line-opacity": 0.9, "line-width": 4
});
map.moveLayer(ids[index], STOPS);
const lines = feature.geometry.type === "LineString"
? [feature.geometry.coordinates] : feature.geometry.coordinates;
lines.forEach((line) => line.forEach((point) => bounds.extend([point[0], point[1]])));
fit();
}).catch(() => { Eif (!closed) onError(); });
});
return () => {
closed = true;
abort.abort();
map.off("mouseenter", STOPS, handleMapHover);
map.off("mousemove", STOPS, handleMapHover);
map.off("mouseleave", STOPS, handleMapLeave);
map.off("click", STOPS, handleMapClick);
popup.remove();
map.getCanvas().style.cursor = "";
Eif (routeInfoControls.get(map) === controls) routeInfoControls.delete(map);
removeRoutes(map, [...ids, STOPS]);
Eif (map.getLayer(STOP_LOCATION_LAYER)) {
map.setLayoutProperty(STOP_LOCATION_LAYER, "visibility", previousVisibility ?? "visible");
}
Eif (isActive()) map.jumpTo({ center: previousCenter, zoom: previousZoom });
};
}
|