about summary refs log tree commit diff
path: root/map/controls.js
blob: e407a0cab5908d25bef0cd58e73b5c9f3f794ad9 (plain) (blame)
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
export class CustomControl {
    _map = undefined;
    _container = undefined;
    _data = undefined;

    constructor(data) {
        if (Object.hasOwn(data, "label") &&
            Object.hasOwn(data, "icon") &&
            Object.hasOwn(data, "onclick")) {
            this._data = data;
        } else {
            throw new Error("missing required property");
        }
    }

    onAdd(map) {
        this._map = map;
        this._container = document.createElement('div');
        this._container.className = 'maplibregl-ctrl maplibregl-ctrl-group';

        const button = document.createElement('button');
        button.type = 'button';
        button.title = this._data.label;
        button.textContent = this._data.icon;
        button.onclick = () => {
            this._data.onclick();
        }

        this._container.appendChild(button);
        // this._map.on('mousedown', () => { });
        return this._container;
    }

    onRemove() {
        this._container.parentNode.removeChild(this._container);
        // this._map.off('mousedown', () => { });
        this._map = undefined;
    }
}