From 955510dae876f144e790aac6a38ca901f7cbf4f4 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 4 Sep 2024 11:17:13 -0500 Subject: [PATCH] Add highlighting of routes --- public/n/lines.json | 16 +++++-- src/new_types.ts | 2 + src/pages/Index2.tsx | 78 +++++++++++++++++++++++++------ vite.config.ts => vite.config.mts | 0 4 files changed, 79 insertions(+), 17 deletions(-) rename vite.config.ts => vite.config.mts (100%) diff --git a/public/n/lines.json b/public/n/lines.json index bc99870..f9de578 100644 --- a/public/n/lines.json +++ b/public/n/lines.json @@ -2,21 +2,29 @@ { "id": 1, "name": "1", - "color": "#ea4fb2" + "color": "#ea4fb2", + "hover_bg": "#ea4fb2", + "hover_on_bg": "white" }, { "id": 2, "name": "2", - "color": "black" + "color": "black", + "hover_bg": "black", + "hover_on_bg": "white" }, { "id": 3, "name": "3", - "color": "#eab308" + "color": "#eab308", + "hover_bg": "#eab308", + "hover_on_bg": "white" }, { "id": 4, "name": "4", - "color": "#ff7300" + "color": "#ff7300", + "hover_bg": "#ff7300", + "hover_on_bg": "white" } ] \ No newline at end of file diff --git a/src/new_types.ts b/src/new_types.ts index c2e1962..3ba19fe 100644 --- a/src/new_types.ts +++ b/src/new_types.ts @@ -7,6 +7,8 @@ export interface Line { name: string /** Hex code */ color: string + "hover_bg": string + "hover_on_bg": string } export type Routes = Array diff --git a/src/pages/Index2.tsx b/src/pages/Index2.tsx index 1ab3d20..4eba3df 100644 --- a/src/pages/Index2.tsx +++ b/src/pages/Index2.tsx @@ -1,23 +1,29 @@ -import { Map, map, polyline, tileLayer } from "leaflet"; +import L from "leaflet"; import { createResource, For, onMount, Suspense } from "solid-js"; import { LineSegmentsIcon } from "../icons/LineSegmentsIcon"; import { Line, Lines, Route, Routes } from "../new_types"; -let g_map: Map | null = null; +const LINE_OPACITY = 0.8; + +let global_map: L.Map | null = null; + +// Stores the polyline rendered in the map. +// The first object stores Lines, the second stores routes +// Indexed by their names +const lines_store: Map> = new Map(); export function Index2() { const container =
; onMount(() => { - const l_map = map(container as HTMLElement) + global_map = L.map(container as HTMLElement) .setView([-16.40171, -71.53040], 13); - g_map = l_map; - tileLayer("/tiles/{z}/{x}/{y}.jpg", { + L.tileLayer("/tiles/{z}/{x}/{y}.jpg", { maxZoom: 17, minZoom: 12, attribution: "© Map data OpenStreetMap", - }).addTo(l_map); + }).addTo(global_map); }); return ( @@ -68,7 +74,9 @@ function LineEl(props: { line: Line }) { }); return ( -
+
Linea {props.line.name} @@ -76,7 +84,11 @@ function LineEl(props: { line: Line }) { {(r) => ( - + )} @@ -84,17 +96,57 @@ function LineEl(props: { line: Line }) { ); } -function RouteEl(props: { route: Route, color: string }) { +function RouteEl(props: { line_name: string, route: Route, color: string }) { // Render the dots into the map - const coords = props.route.points.map(([lat,lng]) => ({lat, lng})); - const line = polyline(coords, { color: props.color}); - line.addTo(g_map!); + const coords = props.route.points.map(([lat, lng]) => ({ lat, lng })); + const line = L.polyline(coords, { color: props.color, opacity: LINE_OPACITY }); + line.addTo(global_map!); + + // Store the route in the global map + // Create line map if neccesary + const line_name = props.line_name; + if (!lines_store.has(line_name)) { + lines_store.set(line_name, new Map()); + } + // store + lines_store.get(line_name)!.set(props.route.name, line); return ( -
+
highlight_route(props.line_name, props.route.name)} + onMouseLeave={() => unhighlight_route()} + > Ruta {props.route.name}
); } +function highlight_route(line_name: string, route_name: string) { + for (const [line_key, line_map] of lines_store) { + for (const [route_key, route_polilyne] of line_map) { + if (line_key === line_name && route_key === route_name) { + // Do nothing + } else { + // make transparent + route_polilyne.setStyle({ + opacity: 0, + }); + } + } + } +} + +function unhighlight_route() { + for (const [, line_map] of lines_store) { + for (const [, route_polilyne] of line_map) { + // restore + route_polilyne.setStyle({ + opacity: LINE_OPACITY, + }); + } + } +} + + diff --git a/vite.config.ts b/vite.config.mts similarity index 100% rename from vite.config.ts rename to vite.config.mts