implements HasSize, HasStyle, H
{
private final LMap lMap;
+ private Consumer
afterInitialResize;
+
public MapContainer(final LComponentManagementRegistry reg)
{
+ this(reg, null);
+ }
+
+ /**
+ * @param afterInitialResize This is called after the map was initially resized/is ready.
+ *
+ * This is ONLY required when calling certain methods like e.g.
+ * {@link LMap#fitBounds(LLatLngBounds)} instantly after the map is created.
+ *
+ * For performance reasons it's highly recommended to only use this when required.
+ */
+ public MapContainer(
+ final LComponentManagementRegistry reg,
+ final Consumer afterInitialResize)
+ {
+ this.afterInitialResize = afterInitialResize;
this.getContent().setSizeFull();
+ this.fixZIndex();
this.lMap = new LMap(reg, this.getContent());
+ this.fixInitialSizeAfterCreation();
+ }
+
+ protected String ensureId()
+ {
+ // Id is auto assigned by LMap so this will never throw
+ return this.getContent().getId().orElseThrow();
+ }
+
+ protected void fixZIndex()
+ {
+ LMap.fixZIndex(this.getContent());
+ }
+
+ protected void fixInitialSizeAfterCreation()
+ {
+ this.lMap.fixInvalidSizeAfterCreation(this.afterInitialResize != null
+ ? "document.getElementById('" + this.ensureId() + "').$server.onInitialResize();"
+ : null);
+ }
+
+ @ClientCallable
+ public void onInitialResize()
+ {
+ if(this.afterInitialResize == null)
+ {
+ return;
+ }
+
+ this.afterInitialResize.accept(this.getlMap());
+ // Free up
+ this.afterInitialResize = null;
}
public LMap getlMap()
diff --git a/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/base/LEvented.java b/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/base/LEvented.java
index ed8cd641..54b0583a 100644
--- a/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/base/LEvented.java
+++ b/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/base/LEvented.java
@@ -103,7 +103,7 @@ default S once(final String type, final String function, final String context)
*/
default S once(final String type, final String function)
{
- return this.on(type, function, null);
+ return this.once(type, function, null);
}
/**
diff --git a/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/map/LMap.java b/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/map/LMap.java
index edfb97dc..dd6cff8c 100644
--- a/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/map/LMap.java
+++ b/vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/map/LMap.java
@@ -50,8 +50,6 @@ public LMap(
final LMapOptions options)
{
super(compReg, "L.map($0" + compReg.writeOptionsOptionalParameter(options) + ")", id);
- // https://stackoverflow.com/q/53879753
- this.fixInvalidSizeAfterCreation();
}
public LMap(
@@ -65,23 +63,6 @@ public LMap(
final LComponentManagementRegistry compReg,
final Div bindDiv,
final LMapOptions options)
- {
- this(compReg, bindDiv, options, true);
- }
-
- public LMap(
- final LComponentManagementRegistry compReg,
- final Div bindDiv,
- final boolean fixDivZIndex)
- {
- this(compReg, bindDiv, null, fixDivZIndex);
- }
-
- public LMap(
- final LComponentManagementRegistry compReg,
- final Div bindDiv,
- final LMapOptions options,
- final boolean fixDivZIndex)
{
this(
compReg,
@@ -91,10 +72,6 @@ public LMap(
return dynamicId;
}),
options);
- if(fixDivZIndex)
- {
- fixZIndex(bindDiv);
- }
}
/**
@@ -105,10 +82,15 @@ public static void fixZIndex(final Div div)
div.getStyle().set("z-index", "1");
}
- protected void fixInvalidSizeAfterCreation()
+ public void fixInvalidSizeAfterCreation(final String callback)
{
+ // https://stackoverflow.com/q/53879753
+ // This should no longer be required starting in Leaflet v2 https://github.com/Leaflet/Leaflet/pull/8612
this.componentRegistry().execJs("let tempMap = " + this.clientComponentJsAccessor() + "; "
- + "setTimeout(function () { tempMap.invalidateSize(false); }, 100)");
+ + "setTimeout(function () { "
+ + " tempMap.invalidateSize(false); "
+ + (callback != null ? callback : "")
+ + " }, 100)");
}
// endregion