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
      Since:
      25.0
      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.

      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 with DomListenerRegistration.addEventData(Class), can be decoded. Maps, Object and JsonNode types 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, not null
      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 the TypeReference. This method supports generic types such as List<MyBean> and Map<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 with DomListenerRegistration.addEventData(TypeReference), can be decoded. Maps, Object and JsonNode types 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, not null
      Returns:
      the event data deserialized as the given type
      Since:
      25.0
      See Also:
    • getEventData

      public <T> T getEventData(DomEventData<T> eventData)
      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 returns DomEventData<String>, the value is a String. For DomEventData<Element>, the value is the element, like with getEventDataElement(DomEventData), or null.

      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 gets null.

      Type Parameters:
      T - the type of the value
      Parameters:
      eventData - event data added to the listener with DomListenerRegistration.addEventData(DomEventData), not null
      Returns:
      the value, or null if 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

      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.addEventDataElementPath(String) or 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.

      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, use getEventDataElement(DomEventData).

      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
    • getEventDataElement

      public Optional<Element> getEventDataElement(DomEventData<Element> eventData)
      Gets the closest Element corresponding to the DOM node that the given declared event data returned in the browser. This works for event data added with DomListenerRegistration.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 allow unsafe-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, not null
      Returns:
      the element that corresponds to the returned DOM node, or an empty optional if there is none
      Since:
      25.4
      See Also:
    • 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(), or its properties using DomListenerRegistration.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 with DomListenerRegistration.addEventDetail(Class), can be decoded. Maps, Object and JsonNode types 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, not null
      Returns:
      the event detail deserialized as the given type, or null if 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 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(), or its properties using DomListenerRegistration.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 with DomListenerRegistration.addEventDetail(Class), can be decoded. Maps, Object and JsonNode types 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, not null
      Returns:
      the event detail deserialized as the given type, or null if event detail is not present or is null
      Since:
      25.0
      See Also: