From 03998ed00d94b2571a99c8ae67d6e081b01e75ee Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Mon, 23 Sep 2024 17:03:42 -0300 Subject: [PATCH] feat: add/remove custom controls dynamically Close #128 --- .../vaadin/addons/googlemaps/GoogleMap.java | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java b/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java index 0ecf69c..13d5f49 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java +++ b/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java @@ -42,7 +42,9 @@ import elemental.json.JsonArray; import elemental.json.JsonObject; import elemental.json.JsonValue; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; import org.apache.commons.lang3.StringUtils; @@ -54,7 +56,9 @@ @JsModule("./googlemaps/geolocation.js") public class GoogleMap extends Component implements HasSize { - private Integer trackLocationId = null; + private Integer trackLocationId = null; + + private Set customControls = new HashSet(); /** Base map types supported by Google Maps. */ public enum MapType { @@ -776,21 +780,44 @@ public void addCustomControls(CustomControl... customControls) { } this.getElement().setPropertyJson("customControls", jsonArray); } - + /** * Sets the custom control buttons to be displayed in the map. * * @param customControls list of custom controls to add to the map */ public void setCustomControls(CustomControl... customControls) { - JsonArray jsonArray = Json.createArray(); - for (int i = 0; i < customControls.length; i++) { - CustomControl customControl = customControls[i]; - jsonArray.set(i, customControl.getJson(i)); - customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i); - this.getElement().appendChild(customControl.getControlButton().getElement()); - } - this.getElement().setPropertyJson("customControls", jsonArray); + this.getElement().executeJs("this._removeCustomControls()").then((e) -> { + JsonArray jsonArray = Json.createArray(); + for (int i = 0; i < customControls.length; i++) { + CustomControl customControl = customControls[i]; + jsonArray.set(i, customControl.getJson(i)); + customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i); + this.getElement().appendChild(customControl.getControlButton().getElement()); + this.customControls.add(customControl); + } + this.getElement().setPropertyJson("customControls", jsonArray); + }); + } + + /** + * Adds a custom control to be displayed in the map. + * + * @param customControl the custom control to add to the map + */ + public void addCustomControl(CustomControl customControl) { + this.customControls.add(customControl); + this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new)); + } + + /** + * Removes a custom control added to the map. + * + * @param customControl the custom control to be removed + */ + public void removeCustomControl(CustomControl customControl) { + this.customControls.remove(customControl); + this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new)); } /**