diff options
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r-- | src/dumbyUtils.mjs | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index 170cb16..2e71ee6 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs | |||
@@ -1,6 +1,8 @@ | |||
1 | import LeaderLine from 'leader-line' | 1 | import LeaderLine from 'leader-line' |
2 | import { insideWindow, insideParent } from './utils' | 2 | import { insideWindow, insideParent } from './utils' |
3 | 3 | ||
4 | export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/ | ||
5 | |||
4 | /** | 6 | /** |
5 | * focusNextMap. | 7 | * focusNextMap. |
6 | * | 8 | * |
@@ -100,11 +102,12 @@ const getMarkersFromMaps = link => { | |||
100 | .filter(map => link.targets ? link.targets.includes(map.id) : true) | 102 | .filter(map => link.targets ? link.targets.includes(map.id) : true) |
101 | .map(map => { | 103 | .map(map => { |
102 | const renderer = map.renderer | 104 | const renderer = map.renderer |
103 | const markerTitle = `${link.targets ?? 'all'}@${link.xy}` | 105 | const markerTitle = `${link.targets ?? 'all'}@${link.dataset.xy}` |
106 | const lonLat = [Number(link.dataset.lon), Number(link.dataset.lat)] | ||
104 | 107 | ||
105 | return map.querySelector(`.marker[title="${markerTitle}"]`) ?? | 108 | return map.querySelector(`.marker[title="${markerTitle}"]`) ?? |
106 | renderer.addMarker({ | 109 | renderer.addMarker({ |
107 | xy: link.xy, | 110 | xy: lonLat, |
108 | title: markerTitle, | 111 | title: markerTitle, |
109 | type: link.type, | 112 | type: link.type, |
110 | }) | 113 | }) |
@@ -138,21 +141,25 @@ const addLeaderLine = (link, target) => { | |||
138 | */ | 141 | */ |
139 | export const createGeoLink = (link) => { | 142 | export const createGeoLink = (link) => { |
140 | const url = new URL(link.href) | 143 | const url = new URL(link.href) |
141 | const xyInParams = url.searchParams.get('xy')?.split(',')?.map(Number) | 144 | const params = new URLSearchParams(link.search) |
142 | const xy = xyInParams ?? url?.href | 145 | const xyInParams = params.get('xy')?.split(',')?.map(Number) |
143 | ?.match(/^geo:([-]?[0-9.]+),([-]?[0-9.]+)/) | 146 | const [lon, lat] = url.href |
147 | ?.match(coordPattern) | ||
144 | ?.splice(1) | 148 | ?.splice(1) |
145 | ?.reverse() | 149 | ?.reverse() |
146 | ?.map(Number) | 150 | ?.map(Number) |
151 | const xy = xyInParams ?? [lon, lat] | ||
147 | 152 | ||
148 | if (!xy || isNaN(xy[0]) || isNaN(xy[1])) return false | 153 | if (!xy || isNaN(xy[0]) || isNaN(xy[1])) return false |
149 | 154 | ||
150 | // Geo information in link | 155 | // Geo information in link |
151 | link.url = url | 156 | link.dataset.xy = xy |
152 | link.xy = xy | 157 | link.dataset.lon = lon |
158 | link.dataset.lat = lat | ||
153 | link.classList.add('with-leader-line', 'geolink') | 159 | link.classList.add('with-leader-line', 'geolink') |
154 | link.targets = link.url.searchParams.get('id')?.split(',') ?? null | 160 | // TODO refactor as data attribute |
155 | link.type = link.url.searchParams.get('type') ?? null | 161 | link.targets = params.get('id')?.split(',') ?? null |
162 | link.type = params.get('type') ?? null | ||
156 | 163 | ||
157 | link.lines = [] | 164 | link.lines = [] |
158 | 165 | ||
@@ -170,7 +177,11 @@ export const createGeoLink = (link) => { | |||
170 | link.onclick = (event) => { | 177 | link.onclick = (event) => { |
171 | event.preventDefault() | 178 | event.preventDefault() |
172 | removeLeaderLines(link) | 179 | removeLeaderLines(link) |
173 | getMarkersFromMaps(link).forEach(updateMapCameraByMarker(link.xy)) | 180 | getMarkersFromMaps(link) |
181 | .forEach(updateMapCameraByMarker([ | ||
182 | Number(link.dataset.lon), | ||
183 | Number(link.dataset.lat) | ||
184 | ])) | ||
174 | } | 185 | } |
175 | return true | 186 | return true |
176 | } | 187 | } |
@@ -229,9 +240,9 @@ const removeLeaderLines = link => { | |||
229 | * @param {Number[]} xy | 240 | * @param {Number[]} xy |
230 | * @return {Function} function | 241 | * @return {Function} function |
231 | */ | 242 | */ |
232 | const updateMapCameraByMarker = xy => marker => { | 243 | const updateMapCameraByMarker = lonLat => marker => { |
233 | const renderer = marker.closest('.mapclay')?.renderer | 244 | const renderer = marker.closest('.mapclay')?.renderer |
234 | renderer.updateCamera({ center: xy }, true) | 245 | renderer.updateCamera({ center: lonLat }, true) |
235 | } | 246 | } |
236 | 247 | ||
237 | /** | 248 | /** |