]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/maplibre.combinedcontrolgroup.js
Add `OSM.MapLibre` and `OSM.MapLibre.Styles`
[rails.git] / app / assets / javascripts / maplibre.combinedcontrolgroup.js
1 class CombinedControlGroup {
2   constructor(controls) {
3     // array of MapLibre controls
4     this.controls = controls;
5     // DOM containers returned by onAdd()
6     this.containers = [];
7   }
8
9   onAdd(map) {
10     this._container = document.createElement("div");
11     this._container.className = "maplibregl-ctrl maplibregl-ctrl-group";
12
13     for (const ctrl of this.controls) {
14       const ctrlContainer = ctrl.onAdd(map);
15       this.containers.push(ctrlContainer);
16
17       // Extract buttons from the control's container and add to our wrapper
18       const buttons = ctrlContainer.querySelectorAll("button");
19       buttons.forEach(button => {
20         this._container.appendChild(button);
21       });
22     }
23
24     return this._container;
25   }
26
27   onRemove() {
28     for (const ctrl of this.controls) ctrl.onRemove?.();
29
30     if (this._container) this._container.remove();
31   }
32 }
33
34 OSM.MapLibre.CombinedControlGroup = CombinedControlGroup;