Skip to content

Commit d315aed

Browse files
committed
Refactored code
1 parent 607bd5c commit d315aed

File tree

4 files changed

+44
-29
lines changed
  • vaadin-maps-leaflet-flow-demo/src/main/java/software/xdev/vaadin/maps/leaflet/flow/demo
  • vaadin-maps-leaflet-flow/src/main

4 files changed

+44
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void mapTest()
9292
this.map.setHeight("700px");
9393
this.map.setWidth("1000px");
9494
this.add(this.map);
95-
this.map.setListener(e -> { System.out.println(e);}); // add some logic here for called Markers (token)
95+
this.map.addMarkerClickListener(ev -> { System.out.println(ev.getTag());}); // add some logic here for called Markers (token)
9696

9797
this.markerZob = new LMarker(49.673470, 12.160108,"ZoB");
9898
this.markerZob.setPopup("Central bus station");

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626

2727
import com.vaadin.flow.component.ClientCallable;
2828
import com.vaadin.flow.component.Component;
29+
import com.vaadin.flow.component.ComponentEvent;
30+
import com.vaadin.flow.component.ComponentEventListener;
31+
import com.vaadin.flow.component.ComponentUtil;
2932
import com.vaadin.flow.component.HasSize;
3033
import com.vaadin.flow.component.Tag;
3134
import com.vaadin.flow.component.dependency.JsModule;
3235
import com.vaadin.flow.component.dependency.NpmPackage;
36+
import com.vaadin.flow.shared.Registration;
3337

3438
import software.xdev.vaadin.maps.leaflet.flow.data.LCenter;
3539
import software.xdev.vaadin.maps.leaflet.flow.data.LCircle;
3640
import software.xdev.vaadin.maps.leaflet.flow.data.LComponent;
3741
import software.xdev.vaadin.maps.leaflet.flow.data.LMarker;
38-
import software.xdev.vaadin.maps.leaflet.flow.data.LMarkerEventListener;
3942
import software.xdev.vaadin.maps.leaflet.flow.data.LPolygon;
4043
import software.xdev.vaadin.maps.leaflet.flow.data.LTileLayer;
4144

@@ -56,7 +59,6 @@ public class LMap extends Component implements HasSize
5659

5760
private LCenter center;
5861
private final List<LComponent> items = new ArrayList<>();
59-
private LMarkerEventListener listener;
6062

6163
public LMap(final double lat, final double lon, final int zoom)
6264
{
@@ -155,16 +157,29 @@ public void removeItem(final LComponent... items)
155157
}
156158

157159
@ClientCallable
158-
private void markerCall(final String id)
160+
protected void onMarkerClick(final String tag)
159161
{
160-
if(this.listener != null)
161-
{
162-
this.listener.onMarkerClickEvent(id);
163-
}
162+
ComponentUtil.fireEvent(this, new MarkerClickEvent(this, true, tag));
163+
}
164+
165+
public Registration addMarkerClickListener(final ComponentEventListener<MarkerClickEvent> listener)
166+
{
167+
return ComponentUtil.addListener(this, MarkerClickEvent.class, listener);
164168
}
165169

166-
public void setListener(final LMarkerEventListener listener)
170+
public class MarkerClickEvent extends ComponentEvent<LMap>
167171
{
168-
this.listener = listener;
172+
private final String tag;
173+
174+
public MarkerClickEvent(final LMap source, final boolean fromClient, final String tag)
175+
{
176+
super(source, fromClient);
177+
this.tag = tag;
178+
}
179+
180+
public String getTag()
181+
{
182+
return this.tag;
183+
}
169184
}
170185
}

vaadin-maps-leaflet-flow/src/main/java/software/xdev/vaadin/maps/leaflet/flow/data/LMarker.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public class LMarker implements LComponent
3434
private static final String MARKER_TYPE = "Point";
3535
private LMarkerGeometry geometry;
3636
private LMarkerOptions properties;
37-
private String token;
37+
/**
38+
* Tag for custom meta-data
39+
*/
40+
private String tag;
3841

3942

4043
/**
@@ -49,11 +52,11 @@ public LMarker(final double lat, final double lon)
4952
this(lat, lon, "empty");
5053
}
5154

52-
public LMarker(final double lat, final double lon, final String token)
55+
public LMarker(final double lat, final double lon, final String tag)
5356
{
5457
this.geometry = new LMarkerGeometry(MARKER_TYPE, lat, lon);
5558
this.properties = new LMarkerOptions();
56-
this.token = token;
59+
this.tag = tag;
5760
}
5861

5962

@@ -134,14 +137,14 @@ public void setPopup(final String popup)
134137
this.properties.setPopup(popup);
135138
}
136139

137-
public String getToken()
140+
public String getTag()
138141
{
139-
return this.token;
142+
return this.tag;
140143
}
141144

142-
public void setToken(final String token)
145+
public void setTag(final String tag)
143146
{
144-
this.token = token;
147+
this.tag = tag;
145148
}
146149

147150
public JsonObject toJson()
@@ -153,7 +156,7 @@ public JsonObject toJson()
153156
jsonObject.put("type", Json.create("Feature"));
154157
jsonObject.put("geometry", Json.parse(mapper.writeValueAsString(this.geometry)));
155158
jsonObject.put("properties", Json.parse(mapper.writeValueAsString(this.properties)));
156-
jsonObject.put("token", Json.create(this.token));
159+
jsonObject.put("tag", Json.create(this.tag));
157160

158161
}
159162
catch(final JsonProcessingException e)

vaadin-maps-leaflet-flow/src/main/resources/META-INF/resources/frontend/leaflet/leafletCon.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,14 @@ export class LeafletMap extends PolymerElement {
9494

9595
if (obj.properties.popup != null) {
9696
item.bindPopup(obj.properties.popup);
97-
98-
if (obj.token != "empty")
99-
{
100-
var vaadinServer = this.$server;
101-
item.on('click', markerOnClick);
102-
function markerOnClick(e)
103-
{
104-
vaadinServer.markerCall(obj.token);
105-
}
106-
}
107-
97+
}
98+
99+
if (obj.tag != "empty") {
100+
var vaadinServer = this.$server;
101+
102+
item.on('click', function (e) {
103+
vaadinServer.onMarkerClick(obj.tag);
104+
});
108105
}
109106

110107
this.items.push(item);

0 commit comments

Comments
 (0)