Skip to content

Commit

Permalink
feat: add/remove custom controls dynamically
Browse files Browse the repository at this point in the history
Close #128
  • Loading branch information
paodb committed Sep 23, 2024
1 parent 42e4769 commit 03998ed
Showing 1 changed file with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<CustomControl> customControls = new HashSet<CustomControl>();

/** Base map types supported by Google Maps. */
public enum MapType {
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 03998ed

Please sign in to comment.