Class DomEventTrigger
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
KeyboardEventTrigger,MouseEventTrigger,SequenceTrigger
Examples:
new DomEventTrigger(button, "click").triggers(action);
new DomEventTrigger(input, "input").triggers(action);
new DomEventTrigger(panel, "pointerdown").triggers(action);
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDomEventTrigger(Component host, String eventName) Creates a trigger that fires when the host receives a DOM event with the given name. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendHandlerBody(StringBuilder body, List<Object> extraCaptures) Hook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements.protected voidappendInstallPrelude(StringBuilder prelude) Hook for subclasses to inject JavaScript that runs once per install, before theconst h = e => {...}wrapper is created.protected final Registrationinstall(JsFunction action) Installs the given rendered action as a client-side listener and returns theRegistrationthat detaches it.Configures the trigger to callevent.preventDefault()when it fires, suppressing the browser's default action (e.g. submitting a form on Enter, scrolling on Space).Configures the trigger to callevent.stopPropagation()when it fires, preventing the event from bubbling to ancestor listeners.
-
Constructor Details
-
DomEventTrigger
Creates a trigger that fires when the host receives a DOM event with the given name.- Parameters:
host- the component whose root element listens for the event, notnulleventName- the DOM event name (e.g."click","input"), notnull
-
-
Method Details
-
preventDefault
Configures the trigger to callevent.preventDefault()when it fires, suppressing the browser's default action (e.g. submitting a form on Enter, scrolling on Space). AffectsTrigger.triggers(Action...)calls made after this method; existing wirings are not retroactively changed.- Returns:
- this trigger, for chaining
-
stopPropagation
Configures the trigger to callevent.stopPropagation()when it fires, preventing the event from bubbling to ancestor listeners. AffectsTrigger.triggers(Action...)calls made after this method; existing wirings are not retroactively changed.- Returns:
- this trigger, for chaining
-
install
Description copied from class:TriggerInstalls the given rendered action as a client-side listener and returns theRegistrationthat detaches it. Called once per action passed toTrigger.triggers(Action...).Implementations typically call
getHost().addJsInitializerwith an install expression that hands the actionJsFunctionto whatever client API the trigger wraps:
The expression runs withreturn getHost().addJsInitializer( "this.addEventListener($1, $0);" + "return () => this.removeEventListener($1, $0);", action, eventName);thisbound to the host element. The captures are made available as$0,$1, … in the order passed.- Specified by:
installin classTrigger- Parameters:
action- the rendered actionJsFunction; takes one runtime argument namedevent(the trigger's event payload), notnull- Returns:
- the registration whose
Registration.remove()detaches the listener, notnull
-
appendHandlerBody
Hook for subclasses to contribute statements to the wrapper handler body and extra captures referenced by those statements. The base implementation appendse.preventDefault();ande.stopPropagation();when the corresponding flags are set.Subclasses overriding this method typically prepend their own guard statements (and call
super.appendHandlerBodyafterwards) so the guard runs beforepreventDefault— preventing the default action on events the guard would have rejected would be wrong. Each entry added toextraCapturesis appended after the event name capture, so the first extra is referenced as$2in the body, the next as$3, and so on.- Parameters:
body- the wrapper body so far; statements appended here run inside theconst h = e => { ... $0(e); }wrapper, in order, with the event available aseextraCaptures- mutable list of captures appended after the event-name capture ($1); the first capture added is$2, etc.
-
appendInstallPrelude
Hook for subclasses to inject JavaScript that runs once per install, before theconst h = e => {...}wrapper is created. Use this to declare closure variables (e.g.let i = 0;for sequence tracking) that the wrapper body then captures by lexical scope.The prelude has access to the same captures as the rest of the install JS —
$0is the action,$1is the event name, and any extras added viaappendHandlerBody(java.lang.StringBuilder, java.util.List<java.lang.Object>)appear at$2+. The base implementation is empty.- Parameters:
prelude- the prelude buffer; statements appended here run before the wrapper is created and remain in scope inside it
-