Class DomEventFilter

java.lang.Object
com.vaadin.flow.dom.DomEventFilter
All Implemented Interfaces:
JsSnippet, Serializable

@JsCalledWith({"event","element"}) public final class DomEventFilter extends Object implements JsSnippet
A declared JavaScript expression that decides if a DOM event is sent to a listener. Use it with DomListenerRegistration.setFilterExpression(DomEventFilter).

To declare a filter, add a method that returns this type to a JsDefinition interface. To create a filter, call that method on the object that JsSnippets.of(Class) returns.

The expression is the body of a function. The function gets two parameters: the DOM event, and the element that the listener is added to. It returns true if the event should be sent. The arguments of the method call are captured, and the expression uses them as $0, $1, and so on:

 @JsDefinition
 public interface KeyFilters extends Serializable {

     @JsExpression("return event.key === $0 && !event.repeat;")
     DomEventFilter isKey(String key);
 }

 static final KeyFilters FILTERS = JsSnippets.of(KeyFilters.class);

 field.getElement().addEventListener("keydown", event -> submit())
         .setFilterExpression(FILTERS.isKey("Enter"));
 

The build adds the expression to the bundle, so the browser does not compile any JavaScript from a string. Filters from the same method share one function in the browser, also when the arguments are different. Each one still checks its own arguments: one listener can have isKey("Enter") and another listener of the same element isKey("Escape"). The first listener then gets only Enter, and the second only Escape.

When all arguments are plain values (strings, numbers, booleans, characters, enums or null), the settings of the listener are stored in a cache that the server and the browser keep for the lifetime of the UI. Each different value adds one entry to this cache, and entries are never removed. So avoid a captured value that is different for each element, such as a row id on every row of a long list. Read such a value from the element instead, for example from an attribute.

The captured values are checked when the filter is created. If a value cannot be sent to the browser, creating the filter fails.

The captured values are encoded each time the settings of the listener are sent to the browser. This happens when the listeners of the element change, and when the whole UI is sent again, for example after a page refresh with @PreserveOnRefresh. A captured element or component is sent with the id that it has at that time.

So a captured element or component must be attached before the settings are sent: in the same round trip in which the filter is set on a listener, or earlier. If it is not attached at that time, the browser gets null for it until the settings are sent again. The browser also gets null for a captured element or component while it is detached. A declared expression that uses such a captured value must handle null.

See Also:
  • Method Details

    • ofDeclaredCall

      public static DomEventFilter ofDeclaredCall(JsCall call)
      Creates a filter for the given call of a JsDefinition method. The method must return a filter.

      For internal use only. May be renamed or removed in a future release. Create a filter by calling the relevant method on the implementation returned by JsSnippets.of(Class).

      Parameters:
      call - the call of a method that returns a filter, not null
      Returns:
      a new filter, not null
      Throws:
      IllegalArgumentException - if the called method does not declare a JsSnippet
      RuntimeException - if any argument of the call cannot be encoded to be sent to the client
    • getDeclaredCall

      public JsCall getDeclaredCall()
      Gets the call of the JsDefinition method that declared this filter. The call tells which function of the bundle to run.

      For internal use only. May be renamed or removed in a future release.

      Returns:
      the declaring call, not null
    • getSnippetKey

      public String getSnippetKey()
      Gets the key that identifies this snippet: its function and its captured values. It does not change after the snippet is created. See JsCall.createSnippetKey().

      For internal use only. May be renamed or removed in a future release.

      Returns:
      the key of the snippet, not null