Class ClientDomEvent

java.lang.Object
com.vaadin.browserless.internal.ClientDomEvent

public final class ClientDomEvent extends Object
A DOM event as the browser dispatches it to the Flow client.

When the browser dispatches an event, the Flow client evaluates every JavaScript expression the server registered for that event type ? event data expressions, listener filters and the event.preventDefault() calls added by DomListenerRegistration.preventDefault() ? against the native event object, and sends the results to the server as the event data. This class does the same for a simulated event, so that filtered listeners see the same data as in the browser, and whether a listener prevented the default action is known when the event is dispatched, as in the browser.

Only the subset of JavaScript that such expressions use is supported: property reads and the preventDefault(), stopPropagation() and getModifierState() calls on event, literals, !, &&, ||, comparisons and parentheses. An expression outside that subset is left out of the event data, as if it were undefined, unless it calls preventDefault(): then whether the default action was prevented cannot be known, and isDefaultPrevented() fails. The event can still be fired, for callers that don't need to know.

Filters and event data declared as JavaScript functions of the bundle, rather than as expressions, are run by a DeclaredFunctionSimulation of the declared function, registered by its @JsDefinition interface. Event data declared by a function without a simulation is left out, like an unsupported expression. A filter declared by a function without a simulation does not match, and as its body cannot be inspected, whether it prevented the default action cannot be known: isDefaultPrevented() fails.

For internal use only.

  • Method Summary

    Modifier and Type
    Method
    Description
    dispatch(Element element, String type, Map<String,?> properties)
    Dispatches an event of the given type on the given element, the way the browser does: the registered expressions are evaluated against an event object with the given properties, giving the event data and whether the default action was prevented.
    dispatchKeyDown(Element element, Key key, Collection<? extends Key> modifiers)
    Dispatches a keydown event of the given key on the given element, with the given modifiers pressed, the way the browser does when the user presses the keys.
    void
    Sends the event to the server, running the listeners whose filter matched.
    boolean
    Gets whether an expression called event.preventDefault() while the event was dispatched.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • dispatch

      public static ClientDomEvent dispatch(Element element, String type, Map<String,?> properties)
      Dispatches an event of the given type on the given element, the way the browser does: the registered expressions are evaluated against an event object with the given properties, giving the event data and whether the default action was prevented.

      The event is not yet sent to the server, see fire().

      Parameters:
      element - the element the event is dispatched on, not null
      type - the event type, e.g. click, not null
      properties - the properties of the native event object, e.g. button; values are Boolean, Number or String
      Returns:
      the dispatched event
    • dispatchKeyDown

      public static ClientDomEvent dispatchKeyDown(Element element, Key key, Collection<? extends Key> modifiers)
      Dispatches a keydown event of the given key on the given element, with the given modifiers pressed, the way the browser does when the user presses the keys. The event has both the key and the code of the key, see KeyboardKey. See dispatch(Element, String, Map).
      Parameters:
      element - the element the event is dispatched on, not null
      key - the pressed key, not null
      modifiers - the modifiers held down while the key is pressed, not null
      Returns:
      the dispatched event
    • isDefaultPrevented

      public boolean isDefaultPrevented()
      Gets whether an expression called event.preventDefault() while the event was dispatched.
      Returns:
      true if the default action of the event was prevented
      Throws:
      IllegalStateException - if a registered expression that calls preventDefault(), or a declared filter, cannot be run without a browser
    • fire

      public void fire()
      Sends the event to the server, running the listeners whose filter matched.