about summary refs log tree commit diff
path: root/map/controls.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--map/controls.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/map/controls.js b/map/controls.js
new file mode 100644
index 0000000..e407a0c
--- /dev/null
+++ b/map/controls.js
@@ -0,0 +1,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;
+    }
+}