Skip to content

Commit 8f5a8df

Browse files
ngonzalezpazFCjavier-godoy
authored andcommitted
refactor: use GoogleMapClickEvent for both right and left click events
Both events are distinguished when registering them via Element API, instead of having a class for each one
1 parent 0d76d4f commit 8f5a8df

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import com.vaadin.flow.component.ClickEvent;
2525
import com.vaadin.flow.component.Component;
2626
import com.vaadin.flow.component.ComponentEventListener;
27-
import com.vaadin.flow.component.DomEvent;
2827
import com.vaadin.flow.component.EventData;
2928
import com.vaadin.flow.component.HasSize;
3029
import com.vaadin.flow.component.Synchronize;
3130
import com.vaadin.flow.component.Tag;
3231
import com.vaadin.flow.component.dependency.JsModule;
3332
import com.vaadin.flow.component.dependency.NpmPackage;
33+
import com.vaadin.flow.dom.DomListenerRegistration;
3434
import com.vaadin.flow.shared.Registration;
3535
import elemental.json.JsonObject;
3636
import elemental.json.JsonValue;
@@ -242,7 +242,6 @@ public int getMinZoom() {
242242
return this.getElement().getProperty("minZoom", 1);
243243
}
244244

245-
@DomEvent("google-map-click")
246245
public static class GoogleMapClickEvent extends ClickEvent<GoogleMap> {
247246
private final double lat;
248247
private final double lon;
@@ -264,32 +263,26 @@ public Registration addClickListener(
264263
ComponentEventListener<GoogleMapClickEvent> listener) {
265264
this.getElement().setProperty("clickable", true);
266265
this.getElement().setProperty("clickEvents", true);
267-
return addListener(GoogleMapClickEvent.class, listener);
268-
}
269-
270-
@DomEvent("google-map-rightclick")
271-
public static class GoogleMapRightClickEvent extends ClickEvent<GoogleMap> {
272-
private final double lat;
273-
private final double lon;
274-
public double getLatitude() {
275-
return this.lat;
276-
}
277-
public double getLongitude() {
278-
return this.lon;
279-
}
280-
public GoogleMapRightClickEvent(GoogleMap source, boolean fromClient,
281-
@EventData(value = "event.detail.latLng") JsonValue latLng) {
282-
super(source);
283-
this.lat = ((JsonObject)latLng).getNumber("lat");
284-
this.lon = ((JsonObject)latLng).getNumber("lng");
285-
}
266+
DomListenerRegistration registration = this.getElement()
267+
.addEventListener("google-map-click", ev -> {
268+
JsonObject latLng = ev.getEventData().get("event.detail.latLng");
269+
listener.onComponentEvent(
270+
new GoogleMapClickEvent(this, true, latLng));
271+
}).addEventData("event.detail.latLng");
272+
return registration::remove;
286273
}
287274

288275
public Registration addRightClickListener(
289-
ComponentEventListener<GoogleMapRightClickEvent> listener) {
276+
ComponentEventListener<GoogleMapClickEvent> listener) {
290277
this.getElement().setProperty("clickable", true);
291278
this.getElement().setProperty("clickEvents", true);
292-
return addListener(GoogleMapRightClickEvent.class, listener);
279+
DomListenerRegistration registration = this.getElement()
280+
.addEventListener("google-map-rightclick", ev -> {
281+
JsonObject latLng = ev.getEventData().get("event.detail.latLng");
282+
listener.onComponentEvent(
283+
new GoogleMapClickEvent(this, true, latLng));
284+
}).addEventData("event.detail.latLng");
285+
return registration::remove;
293286
}
294287

295288
}

0 commit comments

Comments
 (0)