-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseMapPickerControl.ts
78 lines (65 loc) · 2.82 KB
/
BaseMapPickerControl.ts
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
import { IControl, Map } from "mapbox-gl"
import { BASEMAPS } from "@/classes/BaseMap"
import { store } from "@/main"
export class BaseMapPickerControl implements IControl {
private map?: Map
private readonly container: HTMLDivElement
constructor () {
this.container = document.createElement("div")
}
onAdd (map: Map): HTMLElement {
this.map = map
this.container.className = "BaseMapPicker"
const buttonGroup = document.createElement("div")
buttonGroup.className = "mapboxgl-ctrl-group mapboxgl-ctrl"
const button = document.createElement("button")
button.className = "mapboxgl-ctrl-icon mapbox-gl-change_layer icon icon-layers BaseMapPicker__btn"
button.onclick = () => {
BaseMapPickerControl.toggleDropDownButtonVisibility()
}
buttonGroup.append(button)
const dropDown = document.createElement("div")
dropDown.id = "BaseMapPicker__dropdownItem"
dropDown.className = "BaseMapPicker__dropdownContent"
for (const basemap of BASEMAPS) {
const currentBaseMapContainer = document.createElement("div")
currentBaseMapContainer.className = "mapboxgl-ctrl-group mapboxgl-ctrl BaseMapPicker__itemBtnGrp"
const baseMapButton = document.createElement("button")
baseMapButton.className = "mapboxgl-ctrl-icon BaseMapPicker__btn"
baseMapButton.style.backgroundImage = `url(${process.env.BASE_URL}${basemap.img})`
baseMapButton.onclick = () => {
BaseMapPickerControl.toggleDropDownButtonVisibility()
store.updateBasemap(basemap.id)
}
currentBaseMapContainer.append(baseMapButton)
dropDown.appendChild(currentBaseMapContainer)
}
this.container.append(buttonGroup, dropDown)
BaseMapPickerControl.hideOnClickOutside(this.container)
return this.container
}
onRemove (): void {
this.container?.parentNode?.removeChild(this.container)
this.map = undefined
}
private static toggleDropDownButtonVisibility () {
const item = document.getElementById("BaseMapPicker__dropdownItem")
if (!item) {
throw new Error("Could not find base map picker")
}
item.classList.toggle("BaseMapPicker__show")
}
private static hideOnClickOutside (element:HTMLElement) {
const outsideClickListener = (event: MouseEvent) => {
const item = document.getElementById("BaseMapPicker__dropdownItem")
if (!element.contains(event.target as HTMLElement) && BaseMapPickerControl.isVisible(element) &&
item && item.classList.contains("BaseMapPicker__show")) { // or use: event.target.closest(selector) === null
BaseMapPickerControl.toggleDropDownButtonVisibility()
}
}
document.addEventListener("click", outsideClickListener)
}
private static isVisible (elem:HTMLElement) {
return !!elem && !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length)
}
}