Skip to content

Commit ca5372b

Browse files
committed
Add EventListenerOptions trait and implement it in addEventListener and removeEventListener
Create removeEventListener function which accepts an options object Create companion object to EventListenerOptions with an initializer Formatting fixes for scalafmt Annotate companion instantiator with @inline Indent lines to satisfy scalafmt
1 parent 4cf1503 commit ca5372b

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

src/main/scala/org/scalajs/dom/raw/lib.scala

+87-2
Original file line numberDiff line numberDiff line change
@@ -2693,8 +2693,65 @@ class Window
26932693
}
26942694

26952695
/**
2696-
* EventTarget is a DOM interface implemented by objects that can receive DOM events
2697-
* and have listeners for them.
2696+
* An options object that specifies characteristics about the event listener.
2697+
*
2698+
* MDN
2699+
*/
2700+
trait EventListenerOptions extends js.Object {
2701+
2702+
/**
2703+
* A Boolean indicating that events of this type
2704+
* will be dispatched to the registered listener
2705+
* before being dispatched to any EventTarget
2706+
* beneath it in the DOM tree.
2707+
*
2708+
* MDN
2709+
*/
2710+
var capture: js.UndefOr[Boolean] = js.undefined
2711+
2712+
/**
2713+
* A Boolean indicating that the listener
2714+
* should be invoked at most once after being added.
2715+
* If true, the listener would be automatically removed when invoked.
2716+
*/
2717+
var once: js.UndefOr[Boolean] = js.undefined
2718+
2719+
/**
2720+
* A Boolean which, if true, indicates
2721+
* that the function specified by listener
2722+
* will never call preventDefault().
2723+
* If a passive listener does call preventDefault(),
2724+
* the user agent will do nothing other
2725+
* than generate a console warning.
2726+
* See Improving scrolling performance with passive listeners to learn more.
2727+
*
2728+
* MDN
2729+
*/
2730+
var passive: js.UndefOr[Boolean] = js.undefined
2731+
}
2732+
2733+
object EventListenerOptions {
2734+
2735+
/**
2736+
* Construct a new EventListenerOptions
2737+
*/
2738+
@inline
2739+
def apply(capture: js.UndefOr[Boolean] = js.undefined,
2740+
once: js.UndefOr[Boolean] = js.undefined,
2741+
passive: js.UndefOr[Boolean] = js.undefined): EventListenerOptions = {
2742+
val result = js.Dynamic.literal()
2743+
2744+
capture.foreach(result.capture = _)
2745+
once.foreach(result.once = _)
2746+
passive.foreach(result.passive = _)
2747+
2748+
result.asInstanceOf[EventListenerOptions]
2749+
}
2750+
}
2751+
2752+
/**
2753+
* EventTarget is a DOM interface implemented by objects
2754+
* that can receive DOM events and have listeners for them.
26982755
*
26992756
* Element, document, and window are the most common event targets, but other
27002757
* objects can be event targets too, for example XMLHttpRequest, AudioNode,
@@ -2731,6 +2788,34 @@ class EventTarget extends js.Object {
27312788
listener: js.Function1[T, _],
27322789
useCapture: Boolean = js.native): Unit = js.native
27332790

2791+
/**
2792+
* Removes the event listener previously registered with
2793+
* EventTarget.addEventListener.
2794+
*
2795+
* This implementation accepts a settings object of type EventListenerOptions.
2796+
*
2797+
* MDN
2798+
*/
2799+
def removeEventListener[T <: Event](`type`: String,
2800+
listener: js.Function1[T, _],
2801+
options: EventListenerOptions): Unit = js.native
2802+
2803+
/**
2804+
* The EventTarget.addEventListener() method
2805+
* registers the specified listener
2806+
* on the EventTarget it's called on.
2807+
* The event target may be an Element in a document,
2808+
* the Document itself, a Window, or any other object that supports events
2809+
* (such as XMLHttpRequest).
2810+
*
2811+
* This implementation accepts a settings object of type EventListenerOptions.
2812+
*
2813+
* MDN
2814+
*/
2815+
def addEventListener[T <: Event](`type`: String,
2816+
listener: js.Function1[T, _],
2817+
options: EventListenerOptions): Unit = js.native
2818+
27342819
/**
27352820
* Dispatches an Event at the specified EventTarget, invoking the affected
27362821
* EventListeners in the appropriate order. The normal event processing rules

0 commit comments

Comments
 (0)