aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dumbyUtils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r--src/dumbyUtils.mjs39
1 files changed, 27 insertions, 12 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs
index 170cb16..2f92c18 100644
--- a/src/dumbyUtils.mjs
+++ b/src/dumbyUtils.mjs
@@ -1,6 +1,8 @@
1import LeaderLine from 'leader-line' 1import LeaderLine from 'leader-line'
2import { insideWindow, insideParent } from './utils' 2import { insideWindow, insideParent } from './utils'
3 3
4export 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,26 +141,32 @@ const addLeaderLine = (link, target) => {
138 */ 141 */
139export const createGeoLink = (link) => { 142export 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
159 // LeaderLine 166 // LeaderLine
160 link.onmouseover = () => { 167 link.onmouseover = () => {
168 if (link.dataset.valid === 'false') return
169
161 const anchors = getMarkersFromMaps(link) 170 const anchors = getMarkersFromMaps(link)
162 anchors 171 anchors
163 .filter(isAnchorVisible) 172 .filter(isAnchorVisible)
@@ -169,8 +178,14 @@ export const createGeoLink = (link) => {
169 link.onmouseout = () => removeLeaderLines(link) 178 link.onmouseout = () => removeLeaderLines(link)
170 link.onclick = (event) => { 179 link.onclick = (event) => {
171 event.preventDefault() 180 event.preventDefault()
181 if (link.dataset.valid === 'false') return
182
172 removeLeaderLines(link) 183 removeLeaderLines(link)
173 getMarkersFromMaps(link).forEach(updateMapCameraByMarker(link.xy)) 184 getMarkersFromMaps(link)
185 .forEach(updateMapCameraByMarker([
186 Number(link.dataset.lon),
187 Number(link.dataset.lat),
188 ]))
174 } 189 }
175 return true 190 return true
176} 191}
@@ -229,9 +244,9 @@ const removeLeaderLines = link => {
229 * @param {Number[]} xy 244 * @param {Number[]} xy
230 * @return {Function} function 245 * @return {Function} function
231 */ 246 */
232const updateMapCameraByMarker = xy => marker => { 247const updateMapCameraByMarker = lonLat => marker => {
233 const renderer = marker.closest('.mapclay')?.renderer 248 const renderer = marker.closest('.mapclay')?.renderer
234 renderer.updateCamera({ center: xy }, true) 249 renderer.updateCamera({ center: lonLat }, true)
235} 250}
236 251
237/** 252/**