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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
import LeaderLine from 'leader-line'
import { insideWindow, insideParent, replaceTextNodes } from './utils'
import proj4 from 'proj4'
export const coordPattern = /^geo:([-]?[0-9.]+),([-]?[0-9.]+)/
/**
* focusNextMap.
* @param {Boolean} reverse - focus previous map
*/
export function focusNextMap (reverse = false) {
const renderedList = this.utils.renderedMaps()
const index = renderedList.findIndex(e => e.classList.contains('focus'))
const nextIndex = (index + (reverse ? -1 : 1)) % renderedList.length
const nextMap = renderedList.at(nextIndex)
nextMap.classList.add('focus', 'focus-manual')
nextMap.scrollIntoView({ behavior: 'smooth' })
}
/**
* focusNextBlock.
*
* @param {Boolean} reverse - focus previous block
*/
export function focusNextBlock (reverse = false) {
const blocks = this.blocks.filter(b =>
b.checkVisibility({
contentVisibilityAuto: true,
opacityProperty: true,
visibilityProperty: true,
}),
)
const index = blocks.findIndex(e => e.classList.contains('focus'))
const nextIndex = (index + (reverse ? -1 : 1)) % blocks.length
blocks.forEach(b => b.classList.remove('focus'))
const nextBlock = blocks.at(nextIndex)
nextBlock?.classList?.add('focus')
scrollToBlock(nextBlock)
}
/**
* scrollToBlock. Smoothly scroll to target block.
* If block is bigger than viewport, then pick strategy wisely.
*
* @param {HTMLElement} block - Scroll to this element
*/
export const scrollToBlock = block => {
const parentRect = block.parentElement.getBoundingClientRect()
const scrollBlock =
block.getBoundingClientRect().height > parentRect.height * 0.8
? 'nearest'
: 'center'
block.scrollIntoView({ behavior: 'smooth', block: scrollBlock })
}
/**
* focusDelay. Delay of throttle, value changes by cases
*/
export function focusDelay () {
return window.window.getComputedStyle(this.showcase).display === 'none' ? 50 : 300
}
/**
* switchToNextLayout.
*
* @param {Boolean} reverse - Switch to previous one
*/
export function switchToNextLayout (reverse = false) {
const layouts = this.layouts
const currentLayoutName = this.container.dataset.layout
const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName)
const padding = reverse ? -1 : 1
const nextIndex =
currentIndex === -1
? 0
: (currentIndex + padding + layouts.length) % layouts.length
const nextLayout = layouts[nextIndex]
this.container.dataset.layout = nextLayout.name
}
/**
* removeBlockFocus.
*/
export function removeBlockFocus () {
this.blocks.forEach(b => b.classList.remove('focus'))
}
/**
* getMarkersFromMaps. Get marker elements by GeoLink
*
* @param {HTMLAnchorElement} link
* @return {HTMLElement[]} markers
*/
const getMarkersFromMaps = link => {
const maps = Array.from(
link.closest('.Dumby')
.querySelectorAll('.mapclay[data-render="fulfilled"]'),
)
return maps
.filter(map => link.targets ? link.targets.includes(map.id) : true)
.map(map => {
const renderer = map.renderer
const lonLat = [Number(link.dataset.lon), Number(link.dataset.lat)]
const marker = map.querySelector(`.marker[data-xy="${lonLat}"]`) ??
renderer.addMarker({
xy: lonLat,
type: link.type,
})
marker.dataset.xy = lonLat
marker.title = new URLSearchParams(link.search).get('xy') ?? lonLat
const crs = link.dataset.crs
if (crs && crs !== 'EPSG:4326') {
marker.title += '@' + link.dataset.crs
}
return marker
})
}
/**
* addLeaderLine, from link element to target element
*
* @param {HTMLAnchorElement} link
* @param {Element} target
*/
const addLeaderLine = (link, target) => {
const labelText = new URL(link).searchParams.get('text') ?? link.textContent
const line = new LeaderLine({
start: link,
end: target,
hide: true,
middleLabel: labelText,
path: 'magnet',
})
line.show('draw', { duration: 300 })
return line
}
/**
* Create geolinks, which points to map by geo schema and id
*
* @param {HTMLElement} Elements contains anchor elements for GeoLinks
* @returns {Boolean} ture is link is created, false if coordinates are invalid
*/
export const createGeoLink = (link) => {
const url = new URL(link.href)
const params = new URLSearchParams(link.search)
const xyInParams = params.get('xy')?.split(',')?.map(Number)
const [lon, lat] = url.href
?.match(coordPattern)
?.slice(1)
?.reverse()
?.map(Number)
const xy = xyInParams ?? [lon, lat]
if (!xy || isNaN(xy[0]) || isNaN(xy[1])) return false
// Geo information in link
link.dataset.lon = lon
link.dataset.lat = lat
link.dataset.crs = params.get('crs')
link.classList.add('with-leader-line', 'geolink')
link.classList.remove('not-geolink')
// TODO refactor as data attribute
link.targets = params.get('id')?.split(',') ?? null
link.type = params.get('type') ?? null
link.title = 'Left-Click to move Camera, Middle-Click to clean anchor'
link.lines = []
// Hover link for LeaderLine
link.onmouseover = () => {
if (link.dataset.valid === 'false') return
const anchors = getMarkersFromMaps(link)
anchors
.filter(isAnchorVisible)
.forEach(anchor => {
const line = addLeaderLine(link, anchor)
link.lines.push(line)
})
}
link.onmouseout = () => removeLeaderLines(link)
// Click to move camera
link.onclick = (event) => {
event.preventDefault()
if (link.dataset.valid === 'false') return
removeLeaderLines(link)
getMarkersFromMaps(link).forEach(marker => {
const map = marker.closest('.mapclay')
map.scrollIntoView({ behavior: 'smooth' })
updateMapCameraByMarker([
Number(link.dataset.lon),
Number(link.dataset.lat),
])(marker)
})
}
// Use middle click to remove markers
link.onauxclick = (e) => {
if (e.which !== 2) return
e.preventDefault()
removeLeaderLines(link)
getMarkersFromMaps(link)
.forEach(marker => marker.remove())
}
return true
}
/**
* CreateDocLink.
*
* @param {HTMLElement} Elements contains anchor elements for doclinks
*/
export const createDocLink = link => {
const label = decodeURIComponent(link.href.split('#')[1])
const selector = link.title.split('=>')[1] ?? (label ? '#' + label : null)
if (!selector) return false
link.classList.add('with-leader-line', 'doclink')
link.lines = []
link.onmouseover = () => {
const targets = document.querySelectorAll(selector)
targets.forEach(target => {
if (!target?.checkVisibility()) return
// highlight selected target
target.dataset.style = target.style.cssText
const rect = target.getBoundingClientRect()
const isTiny = rect.width < 100 || rect.height < 100
if (isTiny) {
target.style.background = 'lightPink'
} else {
target.style.outline = 'lightPink 6px dashed'
}
// point to selected target
const line = new LeaderLine({
start: link,
end: target,
middleLabel: LeaderLine.pathLabel({
text: label,
fontWeight: 'bold',
}),
hide: true,
path: 'magnet',
})
link.lines.push(line)
line.show('draw', { duration: 300 })
})
}
link.onmouseout = () => {
link.lines.forEach(line => line.remove())
link.lines.length = 0
// resume targets from highlight
const targets = document.querySelectorAll(selector)
targets.forEach(target => {
target.style.cssText = target.dataset.style
delete target.dataset.style
})
}
}
/**
* removeLeaderLines. clean lines start from link
*
* @param {HTMLAnchorElement} link
*/
export const removeLeaderLines = link => {
if (!link.lines) return
link.lines.forEach(line => {
line.hide('draw', { duration: 300 })
setTimeout(() => {
line.remove()
}, 300)
})
link.lines = []
}
/**
* updateMapByMarker. get function for updating map camera by marker
*
* @param {Number[]} xy
* @return {Function} function
*/
const updateMapCameraByMarker = lonLat => marker => {
const renderer = marker.closest('.mapclay')?.renderer
renderer.updateCamera({ center: lonLat }, true)
}
/**
* isAnchorVisible. check anchor(marker) is visible for current map camera
*
* @param {Element} anchor
*/
const isAnchorVisible = anchor => {
const mapContainer = anchor.closest('.mapclay')
return insideWindow(anchor) && insideParent(anchor, mapContainer)
}
/**
* addMarkerByPoint.
*
* @param {Number[]} options.point - page XY
* @param {HTMLElement} options.map
*/
export const addMarkerByPoint = ({ point, map }) => {
const rect = map.getBoundingClientRect()
const [lon, lat] = map.renderer
.unproject([point[0] - rect.left, point[1] - rect.top])
.map(value => parseFloat(value.toFixed(6)))
const marker = map.renderer.addMarker({
xy: [lon, lat],
type: 'circle',
})
marker.dataset.xy = `${lon},${lat}`
return marker
}
/**
* setGeoSchemeByCRS.
* @description Add more information into Anchor Element within Geo Scheme by CRS
* @param {String} crs - EPSG/ESRI Code for CRS
* @return {Function} - Function for link
*/
export const setGeoSchemeByCRS = (crs) => (link) => {
const transform = proj4(crs, 'EPSG:4326')
const params = new URLSearchParams(link.search)
let xy = params.get('xy')?.split(',')?.map(Number)
// Set coords for Geo Scheme
if (link.href.startsWith('geo:0,0')) {
if (!xy) return null
const [lon, lat] = transform.forward(xy)
.map(value => parseFloat(value.toFixed(6)))
link.href = `geo:${lat},${lon}`
}
const [lat, lon] = link.href
.match(coordPattern)
.slice(1)
.map(Number)
if (!xy) {
xy = transform.inverse([lon, lat])
params.set('xy', xy)
}
// set query strings
params.set('crs', crs)
params.set('q', `${lat},${lon}`)
link.search = params
const unit = proj4(crs).oProj.units
const invalidDegree = unit === 'degrees' &&
(lon > 180 || lon < -180 || lat > 90 || lat < -90)
const invalidMeter = unit === 'm' && xy.find(v => v < 100)
if (invalidDegree || invalidMeter) {
link.replaceWith(document.createTextNode(link.textContent))
return null
}
return link
}
/**
* dragForAnchor.
*
* @param {HTMLElement} container
* @param {Range} range
*/
export const dragForAnchor = (container, range, endOfLeaderLine) => {
// link placeholder when dragging
container.classList.add('dragging-geolink')
const geoLink = document.createElement('a')
geoLink.textContent = range.toString()
geoLink.classList.add('with-leader-line', 'geolink', 'drag')
// Replace current content with link
const originContent = range.cloneContents()
const resumeContent = () => {
range.deleteContents()
range.insertNode(originContent)
}
range.deleteContents()
range.insertNode(geoLink)
// Add leader-line
const line = new LeaderLine({
start: geoLink,
end: endOfLeaderLine,
path: 'magnet',
})
const positionObserver = new window.MutationObserver(() => {
line.position()
})
positionObserver.observe(endOfLeaderLine, {
attributes: true,
attributeFilter: ['style'],
})
// Handler for dragend
container.onmouseup = (e) => {
container.classList.remove('dragging-geolink')
container.onmousemove = null
container.onmouseup = null
geoLink.classList.remove('drag')
positionObserver.disconnect()
line.remove()
const map = document.elementFromPoint(e.clientX, e.clientY)
.closest('.mapclay[data-render="fulfilled"]')
if (!map) {
resumeContent()
return
}
const marker = addMarkerByPoint({ point: [e.clientX, e.clientY], map })
if (!marker) {
resumeContent()
return
}
geoLink.href = `geo:${marker.dataset.xy.split(',').reverse()}`
createGeoLink(geoLink)
}
}
export const addGeoSchemeByText = async (node) => {
const coordPatterns = /(-?\d+\.?\d*)([,\x2F\uFF0C])(-?\d+\.?\d*)/
const re = new RegExp(coordPatterns, 'g')
return replaceTextNodes(node, re, match => {
const [x, y] = [match.at(1), match.at(3)]
// Don't process string which can be used as date
if (Date.parse(match.at(0) + ' 1990')) return null
const a = document.createElement('a')
a.className = 'not-geolink'
a.href = `geo:0,0?xy=${x},${y}`
a.textContent = match.at(0)
return a
})
}
|