-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathinfotip.mjs
70 lines (46 loc) · 1.74 KB
/
infotip.mjs
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
/**
## Mapview.infotip()
@module /mapview/infotip
@param {Object} content
*/
const infotip = {}
export default function(content) {
const mapview = this;
// Remove infotip node element
infotip.node?.remove()
// The mapview must have a position to place the infotip.
if (!mapview.position) return;
// Remove infotip positioning event from mapview Map.
mapview.Map.un('pointermove', position)
// Just clears the infotip.
if (!content) return;
if (content instanceof HTMLElement) {
infotip.node = content
// Content type object but not HTMLElement cannot be rendered.
} else if (typeof content === 'object') {
console.warn(`Content type object cannot be rendered in infotip: ${JSON.stringify(content)}`)
return;
} else {
// Check for braces in content string.
if (mapview.locale.xss_check && /[()]/.test(content)) {
console.warn(`Potential XSS detected in infotip content ${content}`)
}
// Creates the infotip node.
infotip.node = mapp.utils.html.node`<div class="infotip box-shadow">`
// Assigns the infotip content.
infotip.node.innerHTML = content
}
// Appends the infotip to the mapview.Map.
mapview.Map.getTargetElement().append(infotip.node)
// Assign infotip positioning event to mapview.Map.
mapview.Map.on('pointermove', position)
// Set the position of the infotip.
position()
function position() {
// The infotip class has a default opacity of 0, with transition effect.
infotip.node.style.opacity = 1;
// Set the infotip position from mapview pointerLocation.
infotip.node.style.left = mapview.pointerLocation.x - infotip.node.offsetWidth / 2 + 'px'
infotip.node.style.top = mapview.pointerLocation.y - infotip.node.offsetHeight - 15 + 'px'
}
}