Skip to content

Commit 2a5e5f5

Browse files
committed
Create Event demo
1 parent 065af8c commit 2a5e5f5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

vaadin-maps-leaflet-flow-demo/src/main/java/software/xdev/vaadin/maps/leaflet/flow/DemoView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.vaadin.flow.router.Route;
1515

1616
import software.xdev.vaadin.maps.leaflet.flow.demo.ComplexDemo;
17+
import software.xdev.vaadin.maps.leaflet.flow.demo.EventDemo;
1718
import software.xdev.vaadin.maps.leaflet.flow.demo.FreeingUpResourceBenchmarkDemo;
1819
import software.xdev.vaadin.maps.leaflet.flow.demo.InitialResizeDemo;
1920
import software.xdev.vaadin.maps.leaflet.flow.demo.MinimalisticDemo;
@@ -71,6 +72,11 @@ protected void onAttach(final AttachEvent attachEvent)
7172
"Complex",
7273
"A complex example with various leaflet components and methods"
7374
),
75+
new Example(
76+
EventDemo.NAV,
77+
"Event",
78+
"A showcase how events can work"
79+
),
7480
new Example(
7581
NotOfThisEarthDemo.NAV,
7682
"Not of this earth",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package software.xdev.vaadin.maps.leaflet.flow.demo;
2+
3+
import com.vaadin.flow.component.button.Button;
4+
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
5+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
6+
import com.vaadin.flow.router.Route;
7+
8+
import software.xdev.vaadin.maps.leaflet.MapContainer;
9+
import software.xdev.vaadin.maps.leaflet.basictypes.LLatLng;
10+
import software.xdev.vaadin.maps.leaflet.layer.raster.LTileLayer;
11+
import software.xdev.vaadin.maps.leaflet.layer.ui.LMarker;
12+
import software.xdev.vaadin.maps.leaflet.map.LMap;
13+
import software.xdev.vaadin.maps.leaflet.registry.LComponentManagementRegistry;
14+
import software.xdev.vaadin.maps.leaflet.registry.LDefaultComponentManagementRegistry;
15+
16+
17+
@Route(EventDemo.NAV)
18+
@SuppressWarnings("checkstyle:MagicNumber")
19+
public class EventDemo extends VerticalLayout
20+
{
21+
public static final String NAV = "/event";
22+
23+
public EventDemo()
24+
{
25+
// Let the view use 100% of the site
26+
this.setSizeFull();
27+
28+
// Create the registry which is needed so that components can be reused and their methods invoked
29+
// Note: You normally don't need to invoke any methods of the registry and just hand it over to the components
30+
final LComponentManagementRegistry reg = new LDefaultComponentManagementRegistry(this);
31+
32+
// Create and add the MapContainer (which contains the map) to the UI
33+
final MapContainer mapContainer = new MapContainer(reg);
34+
mapContainer.setSizeFull();
35+
this.add(mapContainer);
36+
37+
final LMap map = mapContainer.getlMap();
38+
39+
// Add a (default) TileLayer so that we can see something on the map
40+
map.addLayer(LTileLayer.createDefaultForOpenStreetMapTileServer(reg));
41+
42+
// Set what part of the world should be shown
43+
map.setView(new LLatLng(reg, 49.6751, 12.1607), 17);
44+
45+
// Create a new marker
46+
new LMarker(reg, new LLatLng(reg, 49.6756, 12.1610))
47+
// Bind a popup which is displayed when clicking the marker
48+
.bindPopup("XDEV Software")
49+
// Add it to the map
50+
.addTo(map);
51+
52+
final HorizontalLayout hlButtons = new HorizontalLayout();
53+
54+
// Bind clickFunc to map so that it gets disposed when the map is disposed
55+
final String clickFuncReference = map.clientComponentJsAccessor() + ".myCoolClickFunc";
56+
reg.execJs(clickFuncReference + "=e => alert('You clicked: ' + e.latlng)");
57+
58+
final String type = "click";
59+
hlButtons.add(new Button("Register click", ev -> map.on(type, clickFuncReference)));
60+
hlButtons.add(new Button("Register click once", ev -> map.once(type, clickFuncReference)));
61+
hlButtons.add(new Button("Unregister click", ev -> map.off(type, clickFuncReference)));
62+
63+
hlButtons.setWidthFull();
64+
this.add(hlButtons);
65+
}
66+
}

0 commit comments

Comments
 (0)