|
3 | 3 | import Map from "./lib/Map.svelte"; |
4 | 4 | import SlidingPane from "./lib/SlidingPane.svelte"; |
5 | 5 | import { getGeoEntriesInBounds, getUniqueByGeoHash } from "./lib/geodata"; |
| 6 | + import { updateURLParams, readURLParams } from "./lib/urlState"; |
6 | 7 |
|
7 | 8 | let markers = []; |
8 | 9 |
|
9 | | - let dataStatus = "not started"; |
10 | | -
|
11 | 10 | // State to control the sliding pane |
12 | 11 | let isPaneOpen = false; |
13 | 12 | let wikiPage = ""; |
|
23 | 22 | zoom: 1, |
24 | 23 | }; |
25 | 24 |
|
| 25 | + // Track if we need to select a marker once data is loaded |
| 26 | +
|
26 | 27 | // Function to open the pane with a Wikipedia page |
27 | 28 | function openWikiPane(page) { |
28 | 29 | wikiPage = page; |
29 | 30 | isPaneOpen = true; |
30 | 31 | } |
31 | 32 |
|
32 | 33 | // Function to handle marker clicks |
33 | | - function handleMarkerClick(event) { |
| 34 | + async function handleMarkerClick(event) { |
34 | 35 | const marker = event.detail; |
35 | 36 | selectedMarker = marker; |
36 | | -
|
37 | | - // Set the target location instead of directly setting center/zoom |
| 37 | + const hashlevel = Math.max(1, Math.min(8, mapZoom / 2)); |
| 38 | + markers = addMarkerClasses([...markers], hashlevel); |
| 39 | + openWikiPane(marker.page_title); |
38 | 40 | targetMapLocation = { |
39 | 41 | lat: marker.lat, |
40 | 42 | lon: marker.lon, |
41 | 43 | zoom: Math.max(13, mapZoom), // Ensure zoom is at least 13 |
42 | 44 | }; |
43 | 45 |
|
44 | | - openWikiPane(marker.page_title); |
| 46 | + // Update URL only for user-initiated actions |
| 47 | + updateURLParams(targetMapLocation, selectedMarker); |
45 | 48 | } |
46 | 49 |
|
47 | | - function addCosmeticsToEntries(entries, hashlevel) { |
| 50 | + function addMarkerClasses(entries, hashlevel) { |
| 51 | + for (const entry of entries) { |
| 52 | + entry.isSelected = selectedMarker |
| 53 | + ? entry.page_title === selectedMarker.page_title && |
| 54 | + entry.geohash === selectedMarker.geohash |
| 55 | + : false; |
| 56 | + } |
48 | 57 | if (hashlevel > 7.5) { |
49 | 58 | for (const entry of entries) { |
50 | 59 | entry.sizeClass = "full"; |
|
69 | 78 | } |
70 | 79 | } |
71 | 80 | // Sort entries so "full" size class appears last |
| 81 | + // TODO: probably better done via the CSS classes |
72 | 82 | entries.sort((a, b) => { |
73 | | - if (a.sizeClass === "full" && b.sizeClass !== "full") return 1; |
74 | | - if (a.sizeClass !== "full" && b.sizeClass === "full") return -1; |
| 83 | + if (a.isSelected) { |
| 84 | + console.log("here"); |
| 85 | + return -1; |
| 86 | + } |
| 87 | + if (a.sizeClass === "full" && b.sizeClass !== "full") return -1; |
| 88 | + if (a.sizeClass !== "full" && b.sizeClass === "full") return 1; |
75 | 89 | return 0; |
76 | 90 | }); |
| 91 | + return entries; |
77 | 92 | } |
78 | 93 | async function handleBoundsChange(event) { |
79 | 94 | const center = event.detail.center; |
80 | 95 | mapZoom = event.detail.zoom; |
81 | | - console.log(event.detail.center); |
| 96 | +
|
| 97 | + // Update targetMapLocation with the new center and zoom |
| 98 | + const urlTargetMapLocation = { |
| 99 | + lat: center.lat, |
| 100 | + lon: center.lng, |
| 101 | + zoom: mapZoom, |
| 102 | + }; |
| 103 | + updateURLParams(urlTargetMapLocation, selectedMarker); |
| 104 | +
|
82 | 105 | const bounds = { |
83 | 106 | minLat: event.detail.bounds._southWest.lat, |
84 | 107 | maxLat: event.detail.bounds._northEast.lat, |
85 | 108 | minLon: event.detail.bounds._southWest.lng, |
86 | 109 | maxLon: event.detail.bounds._northEast.lng, |
87 | 110 | }; |
| 111 | +
|
88 | 112 | const entries = await getGeoEntriesInBounds(bounds); |
89 | 113 | const hashlevel = Math.max(1, Math.min(8, mapZoom / 2)); |
90 | 114 | const uniqueEntries = getUniqueByGeoHash({ |
91 | 115 | entries, |
92 | 116 | hashLength: hashlevel, |
93 | 117 | scoreField: "page_len", |
94 | 118 | }); |
95 | | - addCosmeticsToEntries(uniqueEntries, hashlevel); |
| 119 | + if ( |
| 120 | + selectedMarker && |
| 121 | + !uniqueEntries.some( |
| 122 | + (entry) => |
| 123 | + entry.page_title === selectedMarker.page_title && |
| 124 | + entry.geohash === selectedMarker.geohash |
| 125 | + ) |
| 126 | + ) { |
| 127 | + uniqueEntries.push(selectedMarker); |
| 128 | + } |
| 129 | +
|
| 130 | + addMarkerClasses(uniqueEntries, hashlevel); |
96 | 131 | markers = uniqueEntries; |
97 | 132 | } |
98 | 133 | onMount(async () => { |
99 | 134 | console.log("App starting"); |
| 135 | +
|
| 136 | + // Read URL parameters when the app loads |
| 137 | + const urlState = readURLParams(); |
| 138 | + if (urlState.targetLocation) { |
| 139 | + targetMapLocation = urlState.targetLocation; |
| 140 | + } |
| 141 | + if (urlState.selectedMarker) { |
| 142 | + selectedMarker = urlState.selectedMarker; |
| 143 | + openWikiPane(selectedMarker.page_title); |
| 144 | + } |
100 | 145 | }); |
101 | 146 | </script> |
102 | 147 |
|
|
0 commit comments