Skip to content

Commit 9ae9dc8

Browse files
paodbjavier-godoy
authored andcommitted
feat: add/remove custom controls dynamically
Close #128
1 parent 42e4769 commit 9ae9dc8

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
import elemental.json.JsonArray;
4343
import elemental.json.JsonObject;
4444
import elemental.json.JsonValue;
45+
import java.util.HashSet;
4546
import java.util.List;
47+
import java.util.Set;
4648
import java.util.concurrent.CompletableFuture;
4749
import org.apache.commons.lang3.StringUtils;
4850

@@ -54,7 +56,9 @@
5456
@JsModule("./googlemaps/geolocation.js")
5557
public class GoogleMap extends Component implements HasSize {
5658

57-
private Integer trackLocationId = null;
59+
private Integer trackLocationId = null;
60+
61+
private Set<CustomControl> customControls = new HashSet<CustomControl>();
5862

5963
/** Base map types supported by Google Maps. */
6064
public enum MapType {
@@ -776,21 +780,44 @@ public void addCustomControls(CustomControl... customControls) {
776780
}
777781
this.getElement().setPropertyJson("customControls", jsonArray);
778782
}
779-
783+
780784
/**
781785
* Sets the custom control buttons to be displayed in the map.
782786
*
783787
* @param customControls list of custom controls to add to the map
784788
*/
785789
public void setCustomControls(CustomControl... customControls) {
786-
JsonArray jsonArray = Json.createArray();
787-
for (int i = 0; i < customControls.length; i++) {
788-
CustomControl customControl = customControls[i];
789-
jsonArray.set(i, customControl.getJson(i));
790-
customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i);
791-
this.getElement().appendChild(customControl.getControlButton().getElement());
792-
}
793-
this.getElement().setPropertyJson("customControls", jsonArray);
790+
this.getElement().executeJs("this._removeCustomControls()").then((e) -> {
791+
JsonArray jsonArray = Json.createArray();
792+
for (int i = 0; i < customControls.length; i++) {
793+
CustomControl customControl = customControls[i];
794+
jsonArray.set(i, customControl.getJson(i));
795+
customControl.getControlButton().getElement().setAttribute("slot", "customControlSlot_" + i);
796+
this.getElement().appendChild(customControl.getControlButton().getElement());
797+
this.customControls.add(customControl);
798+
}
799+
this.getElement().setPropertyJson("customControls", jsonArray);
800+
});
801+
}
802+
803+
/**
804+
* Adds a custom control to be displayed in the map.
805+
*
806+
* @param customControl the custom control to add to the map
807+
*/
808+
public void addCustomControl(CustomControl customControl) {
809+
this.customControls.add(customControl);
810+
this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new));
811+
}
812+
813+
/**
814+
* Removes a custom control added to the map.
815+
*
816+
* @param customControl the custom control to be removed
817+
*/
818+
public void removeCustomControl(CustomControl customControl) {
819+
this.customControls.remove(customControl);
820+
this.setCustomControls(this.customControls.stream().toArray(CustomControl[]::new));
794821
}
795822

796823
/**

0 commit comments

Comments
 (0)