Class DomEvent
- All Implemented Interfaces:
Serializable
- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
-
Field Summary
Fields inherited from class java.util.EventObject
source -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiontools.jackson.databind.JsonNodeGets additional data related to the event.<T> TgetEventData(Class<T> type) Gets the event data deserialized as the given type.<T> TgetEventData(tools.jackson.core.type.TypeReference<T> typeReference) Gets the event data deserialized as the type specified by theTypeReference.getEventDataElement(String eventDataExpression) Gets the closestElementcorresponding to the given event data expression.<T> TgetEventDetail(Class<T> type) Gets theevent.detailproperty from the event, deserialized as the given type.<T> TgetEventDetail(tools.jackson.core.type.TypeReference<T> typeReference) Gets theevent.detailproperty from the event, deserialized as the type specified by theTypeReference.Gets the closestElementthat corresponds to theevent.targetfor the DOM event.getPhase()Gets the debounce phase for which this event is fired.Returns the element on which the listener has been attached.getType()Gets the type of the event.Methods inherited from class java.util.EventObject
toString
-
Constructor Details
-
DomEvent
Creates a new DOM event.- Parameters:
source- the element on which the listener has been attached, notnulleventType- the type of the event, notnulleventData- additional data related to the event, notnull- See Also:
-
-
Method Details
-
getSource
Returns the element on which the listener has been attached.- Overrides:
getSourcein classEventObject- Returns:
- The element on which the listener has been attached.
- See Also:
-
getType
Gets the type of the event.- Returns:
- the type of the event
-
getEventData
public tools.jackson.databind.JsonNode getEventData()Gets additional data related to the event. An empty JSON object is returned if no event data is available.- Returns:
- a JSON object containing event data, never
null - See Also:
-
getEventData
Gets the event data deserialized as the given type. This method supports arbitrary bean types through Jackson deserialization.Example usage:
MyDto dto = domEvent.getEventData(MyDto.class);
- Type Parameters:
T- the type to deserialize to- Parameters:
type- the class to deserialize the event data to, notnull- Returns:
- the event data deserialized as the given type
- See Also:
-
getEventData
public <T> T getEventData(tools.jackson.core.type.TypeReference<T> typeReference) Gets the event data deserialized as the type specified by theTypeReference. This method supports generic types such asList<MyBean>andMap<String, MyBean>through Jackson's TypeReference mechanism.Example usage:
TypeReference<List<MyDto>> typeRef = new TypeReference<List<MyDto>>() { }; List<MyDto> dtos = domEvent.getEventData(typeRef);- Type Parameters:
T- the type to deserialize to- Parameters:
typeReference- the type reference describing the target type, notnull- Returns:
- the event data deserialized as the given type
- See Also:
-
getPhase
Gets the debounce phase for which this event is fired. This is used internally to only deliver the event to the appropriate listener in cases where there are multiple listeners for the same event with different debounce settings.- Returns:
- the debounce phase
-
getEventTarget
Gets the closestElementthat corresponds to theevent.targetfor the DOM event. This is always inside the child hierarchy of the element returned bygetSource().To get this reported, you need to call
DomListenerRegistration.mapEventTargetElement()or an empty optional is always returned.The returned element is the same as
getSource()only if the event originated from that element on the browser (and not from its child).- Returns:
- the element that corresponds to
event.targetor an empty optional - Since:
- 9.0
-
getEventDataElement
Gets the closestElementcorresponding to the given event data expression. NOTE: this only works if you have added the expression usingDomListenerRegistration.addEventDataElement(String).If the evaluated JS expression returned an element that is not created or controlled by the server side, the closest parent element that is controlled is returned instead. Invisible elements are not reported.
In case you want the
event.targetelement, usegetEventTarget()instead.- Parameters:
eventDataExpression- the expression that was executed on the client to retrieve the element, notnull- Returns:
- the element that corresponds to the given expression or an empty optional
- Since:
- 9.0
-
getEventDetail
Gets theevent.detailproperty from the event, deserialized as the given type. This method supports arbitrary bean types and Java records through Jackson deserialization.The
event.detailproperty must have been included in the event data usingDomListenerRegistration.addEventDetail().Example usage:
record RgbColor(int r, int g, int b) { } element.addEventListener("color-change", e -> { RgbColor color = e.getEventDetail(RgbColor.class); System.out.println("R: " + color.r() + ", G: " + color.g() + ", B: " + color.b()); }).addEventDetail();- Type Parameters:
T- the type to deserialize to- Parameters:
type- the class to deserialize the event detail to, notnull- Returns:
- the event detail deserialized as the given type, or
nullif event detail is not present or is null - See Also:
-
getEventDetail
public <T> T getEventDetail(tools.jackson.core.type.TypeReference<T> typeReference) Gets theevent.detailproperty from the event, deserialized as the type specified by theTypeReference. This method supports generic types such asList<MyBean>andMap<String, MyBean>through Jackson's TypeReference mechanism.The
event.detailproperty must have been included in the event data usingDomListenerRegistration.addEventDetail().Example usage:
element.addEventListener("list-change", e -> { List<String> items = e .getEventDetail(new TypeReference<List<String>>() { }); System.out.println("Items: " + items); }).addEventDetail();- Type Parameters:
T- the type to deserialize to- Parameters:
typeReference- the type reference describing the target type, notnull- Returns:
- the event detail deserialized as the given type, or
nullif event detail is not present or is null - See Also:
-