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(DomEventData<T> eventData) Gets the value of the given event data.<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(DomEventData<Element> eventData) Gets the closestElementcorresponding to the DOM node that the given declared event data returned in the browser.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- Since:
- 25.0
- 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.Event data expressions that are property paths, such as
event.clientX, are also made available as nested objects, so that a type mirroring the structure of the event, as used withDomListenerRegistration.addEventData(Class), can be decoded. Maps,ObjectandJsonNodetypes get the event data as is, with one key per expression.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
- Since:
- 25.0
- 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.Event data expressions that are property paths, such as
event.clientX, are also made available as nested objects, so that a type mirroring the structure of the event, as used withDomListenerRegistration.addEventData(TypeReference), can be decoded. Maps,ObjectandJsonNodetypes get the event data as is, with one key per expression.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
- Since:
- 25.0
- See Also:
-
getEventData
Gets the value of the given event data. The browser sends the value together with this event. The value has the type that the declaring method defines. For example, for a method that returnsDomEventData<String>, the value is aString. ForDomEventData<Element>, the value is the element, like withgetEventDataElement(DomEventData), ornull.Pass the instance that you added to the listener. If all arguments of the event data are plain values (strings, numbers, booleans, characters, enums or
null), another instance created with the same method and equal arguments also works. With any other argument, such as an element, a record or a list, another instance getsnull.- Type Parameters:
T- the type of the value- Parameters:
eventData- event data added to the listener withDomListenerRegistration.addEventData(DomEventData), notnull- Returns:
- the value, or
nullif the browser sent no value for the event data, or if the event data was not added to the listener - Throws:
IllegalArgumentException- if the value cannot be read as the declared type- Since:
- 25.4
-
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.addEventDataElementPath(String)orDomListenerRegistration.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.The browser compiles the expression from a string, so it does not work with a Content Security Policy that does not allow
unsafe-eval, unless it is a property path. For an element from declared event data, which works without that, usegetEventDataElement(DomEventData).- 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
-
getEventDataElement
Gets the closestElementcorresponding to the DOM node that the given declared event data returned in the browser. This works for event data added withDomListenerRegistration.addEventData(DomEventData).If the DOM node is not created or controlled by the server side, the closest parent element that is controlled is returned instead. Invisible elements are not reported.
Unlike
getEventDataElement(String), the browser runs a function from the bundle instead of compiling an expression from a string, so this also works with a Content Security Policy that does not allowunsafe-eval.@JsDefinition public interface PathJs extends Serializable { @JsExpression("return event.composedPath()[0];") DomEventData<Element> originalTarget(); } DomEventData<Element> target = JsSnippets.of(PathJs.class) .originalTarget(); element.addEventListener("click", event -> event.getEventDataElement(target).ifPresent(this::show)) .addEventData(target);- Parameters:
eventData- event data that returns an element, added to the listener, notnull- Returns:
- the element that corresponds to the returned DOM node, or an empty optional if there is none
- Since:
- 25.4
- See Also:
-
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(), or its properties usingDomListenerRegistration.addEventDetail(Class).Detail properties that are property paths, such as
color.r, are also made available as nested objects, so that a type with nested properties, as used withDomListenerRegistration.addEventDetail(Class), can be decoded. Maps,ObjectandJsonNodetypes get the detail as is, with one key per property.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 - Since:
- 25.0
- 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(), or its properties usingDomListenerRegistration.addEventDetail(Class).Detail properties that are property paths, such as
color.r, are also made available as nested objects, so that a type with nested properties, as used withDomListenerRegistration.addEventDetail(Class), can be decoded. Maps,ObjectandJsonNodetypes get the detail as is, with one key per property.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 - Since:
- 25.0
- See Also:
-