Class DomEvent

java.lang.Object
java.util.EventObject
com.vaadin.flow.dom.DomEvent
All Implemented Interfaces:
Serializable

public class DomEvent extends EventObject
Server-side representation of a DOM event fired in the browser.
Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • DomEvent

      public DomEvent(Element source, String eventType, tools.jackson.databind.JsonNode eventData)
      Creates a new DOM event.
      Parameters:
      source - the element on which the listener has been attached, not null
      eventType - the type of the event, not null
      eventData - additional data related to the event, not null
      See Also:
  • Method Details

    • getSource

      public Element getSource()
      Returns the element on which the listener has been attached.
      Overrides:
      getSource in class EventObject
      Returns:
      The element on which the listener has been attached.
      See Also:
    • getType

      public String 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

      public <T> T getEventData(Class<T> type)
      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, not null
      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 the TypeReference. This method supports generic types such as List<MyBean> and Map<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, not null
      Returns:
      the event data deserialized as the given type
      See Also:
    • getPhase

      public DebouncePhase 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

      public Optional<Element> getEventTarget()
      Gets the closest Element that corresponds to the event.target for the DOM event. This is always inside the child hierarchy of the element returned by getSource().

      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.target or an empty optional
      Since:
      9.0
    • getEventDataElement

      public Optional<Element> getEventDataElement(String eventDataExpression)
      Gets the closest Element corresponding to the given event data expression. NOTE: this only works if you have added the expression using DomListenerRegistration.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.target element, use getEventTarget() instead.

      Parameters:
      eventDataExpression - the expression that was executed on the client to retrieve the element, not null
      Returns:
      the element that corresponds to the given expression or an empty optional
      Since:
      9.0
    • getEventDetail

      public <T> T getEventDetail(Class<T> type)
      Gets the event.detail property from the event, deserialized as the given type. This method supports arbitrary bean types and Java records through Jackson deserialization.

      The event.detail property must have been included in the event data using DomListenerRegistration.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, not null
      Returns:
      the event detail deserialized as the given type, or null if event detail is not present or is null
      See Also:
    • getEventDetail

      public <T> T getEventDetail(tools.jackson.core.type.TypeReference<T> typeReference)
      Gets the event.detail property from the event, deserialized as the type specified by the TypeReference. This method supports generic types such as List<MyBean> and Map<String, MyBean> through Jackson's TypeReference mechanism.

      The event.detail property must have been included in the event data using DomListenerRegistration.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, not null
      Returns:
      the event detail deserialized as the given type, or null if event detail is not present or is null
      See Also: