From 342c305d9925018dd4342dda1f41d5cb48497e6e Mon Sep 17 00:00:00 2001 From: Clement Busschaert Date: Wed, 5 Jun 2019 10:44:55 +0200 Subject: [PATCH 1/3] Lib: Add support for 'addEventListener' with options object --- CHANGES | 1 + lib/js_of_ocaml/dom.ml | 22 +++++++++++++++++++--- lib/js_of_ocaml/dom.mli | 21 +++++++++++++++++++-- lib/js_of_ocaml/dom_html.ml | 7 +++++++ lib/js_of_ocaml/dom_html.mli | 19 +++++++++++++++++-- 5 files changed, 63 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 985fd210ad..8bc310a054 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,7 @@ ** Misc: remove cppo dependency ** Misc: remove ppx_tools_versioned dependency in ppx_deriving_json ** Misc: support for ocaml 4.09 + ** Lib: Add support for 'addEventListener' with options object * Bug fixes: ** Compiler: don't generate source if no-source-map passed (#780) diff --git a/lib/js_of_ocaml/dom.ml b/lib/js_of_ocaml/dom.ml index 05a21ecd53..32cc71edce 100644 --- a/lib/js_of_ocaml/dom.ml +++ b/lib/js_of_ocaml/dom.ml @@ -352,7 +352,14 @@ end type event_listener_id = unit -> unit -let addEventListener (e : (< .. > as 'a) t) typ h capt = +class type event_listener_options = + object + method capture : bool t readonly_prop + method once : bool t readonly_prop + method passive : bool t readonly_prop + end + +let addEventListenerWithOptions (e : (< .. > as 'a) t) typ h opts = if (Js.Unsafe.coerce e)##.addEventListener == Js.undefined then let ev = (Js.string "on")##concat typ in @@ -360,8 +367,17 @@ let addEventListener (e : (< .. > as 'a) t) typ h capt = let () = (Js.Unsafe.coerce e)##attachEvent ev callback in fun () -> (Js.Unsafe.coerce e)##detachEvent ev callback else - let () = (Js.Unsafe.coerce e)##addEventListener typ h capt in - fun () -> (Js.Unsafe.coerce e)##removeEventListener typ h capt + let () = (Js.Unsafe.coerce e)##addEventListener typ h opts in + fun () -> (Js.Unsafe.coerce e)##removeEventListener typ h opts + +let addEventListener (e : (< .. > as 'a) t) typ h capt = + let opts = object%js + val capture = capt + val once = Js.bool false + val passive = Js.bool false + end + in + addEventListenerWithOptions e typ h opts let removeEventListener id = id () diff --git a/lib/js_of_ocaml/dom.mli b/lib/js_of_ocaml/dom.mli index 475cb7da43..e9bcd03632 100644 --- a/lib/js_of_ocaml/dom.mli +++ b/lib/js_of_ocaml/dom.mli @@ -334,6 +334,23 @@ module Event : sig val make : string -> 'a typ end +class type event_listener_options = + object + method capture : bool t readonly_prop + method once : bool t readonly_prop + method passive : bool t readonly_prop + end + +val addEventListenerWithOptions : + (< .. > t as 'a) + -> 'b Event.typ + -> ('a, 'b) event_listener + -> #event_listener_options t + -> event_listener_id +(** Add an event listener. This function matches the + option-object variant of the [addEventListener] DOM method, + except that it returns an id for removing the listener. *) + val addEventListener : (< .. > t as 'a) -> 'b Event.typ @@ -341,8 +358,8 @@ val addEventListener : -> bool t -> event_listener_id (** Add an event listener. This function matches the - [addEventListener] DOM method, except that it returns - an id for removing the listener. *) + useCapture boolean variant of the [addEventListener] DOM method, + except that it returns an id for removing the listener. *) val removeEventListener : event_listener_id -> unit (** Remove the given event listener. *) diff --git a/lib/js_of_ocaml/dom_html.ml b/lib/js_of_ocaml/dom_html.ml index ab7fe0751a..4c5be273e3 100644 --- a/lib/js_of_ocaml/dom_html.ml +++ b/lib/js_of_ocaml/dom_html.ml @@ -814,8 +814,15 @@ end type event_listener_id = Dom.event_listener_id +class type event_listener_options = + object + inherit Dom.event_listener_options + end + let addEventListener = Dom.addEventListener +let addEventListenerWithOptions = Dom.addEventListenerWithOptions + let removeEventListener = Dom.removeEventListener class type ['node] collection = diff --git a/lib/js_of_ocaml/dom_html.mli b/lib/js_of_ocaml/dom_html.mli index 60a5fef009..db48c56876 100644 --- a/lib/js_of_ocaml/dom_html.mli +++ b/lib/js_of_ocaml/dom_html.mli @@ -2365,6 +2365,21 @@ end type event_listener_id = Dom.event_listener_id +class type event_listener_options = + object + inherit Dom.event_listener_options + end + +val addEventListenerWithOptions : + (#eventTarget t as 'a) + -> 'b Event.typ + -> ('a, 'b) event_listener + -> #event_listener_options t + -> event_listener_id +(** Add an event listener. This function matches the + option-object variant of the [addEventListener] DOM method, + except that it returns an id for removing the listener. *) + val addEventListener : (#eventTarget t as 'a) -> 'b Event.typ @@ -2372,8 +2387,8 @@ val addEventListener : -> bool t -> event_listener_id (** Add an event listener. This function matches the - [addEventListener] DOM method, except that it returns - an id for removing the listener. *) + useCapture boolean variant of the [addEventListener] DOM method, + except that it returns an id for removing the listener. *) val removeEventListener : event_listener_id -> unit (** Remove the given event listener. *) From 147c075f7268ab9968e6d3e6e0f660e80f957096 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sun, 7 Jul 2019 20:37:32 +0800 Subject: [PATCH 2/3] small tuning --- lib/js_of_ocaml/dom.ml | 27 ++++++++++++++++----------- lib/js_of_ocaml/dom.mli | 11 +++-------- lib/js_of_ocaml/dom_html.ml | 5 ----- lib/js_of_ocaml/dom_html.mli | 9 +++------ 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/lib/js_of_ocaml/dom.ml b/lib/js_of_ocaml/dom.ml index 32cc71edce..22cb31f441 100644 --- a/lib/js_of_ocaml/dom.ml +++ b/lib/js_of_ocaml/dom.ml @@ -354,12 +354,14 @@ type event_listener_id = unit -> unit class type event_listener_options = object - method capture : bool t readonly_prop - method once : bool t readonly_prop - method passive : bool t readonly_prop + method capture : bool t writeonly_prop + + method once : bool t writeonly_prop + + method passive : bool t writeonly_prop end -let addEventListenerWithOptions (e : (< .. > as 'a) t) typ h opts = +let addEventListenerWithOptions (e : (< .. > as 'a) t) typ ?capture ?once ?passive h = if (Js.Unsafe.coerce e)##.addEventListener == Js.undefined then let ev = (Js.string "on")##concat typ in @@ -367,17 +369,20 @@ let addEventListenerWithOptions (e : (< .. > as 'a) t) typ h opts = let () = (Js.Unsafe.coerce e)##attachEvent ev callback in fun () -> (Js.Unsafe.coerce e)##detachEvent ev callback else + let opts : event_listener_options t = Js.Unsafe.obj [||] in + let set t f = + match t with + | None -> () + | Some b -> f b + in + set capture (fun b -> opts##.capture := b); + set once (fun b -> opts##.once := b); + set passive (fun b -> opts##.passive := b); let () = (Js.Unsafe.coerce e)##addEventListener typ h opts in fun () -> (Js.Unsafe.coerce e)##removeEventListener typ h opts let addEventListener (e : (< .. > as 'a) t) typ h capt = - let opts = object%js - val capture = capt - val once = Js.bool false - val passive = Js.bool false - end - in - addEventListenerWithOptions e typ h opts + addEventListenerWithOptions e typ ~capture:capt h let removeEventListener id = id () diff --git a/lib/js_of_ocaml/dom.mli b/lib/js_of_ocaml/dom.mli index e9bcd03632..0de17e2eba 100644 --- a/lib/js_of_ocaml/dom.mli +++ b/lib/js_of_ocaml/dom.mli @@ -334,18 +334,13 @@ module Event : sig val make : string -> 'a typ end -class type event_listener_options = - object - method capture : bool t readonly_prop - method once : bool t readonly_prop - method passive : bool t readonly_prop - end - val addEventListenerWithOptions : (< .. > t as 'a) -> 'b Event.typ + -> ?capture:bool t + -> ?once:bool t + -> ?passive:bool t -> ('a, 'b) event_listener - -> #event_listener_options t -> event_listener_id (** Add an event listener. This function matches the option-object variant of the [addEventListener] DOM method, diff --git a/lib/js_of_ocaml/dom_html.ml b/lib/js_of_ocaml/dom_html.ml index 4c5be273e3..cc64cb99ba 100644 --- a/lib/js_of_ocaml/dom_html.ml +++ b/lib/js_of_ocaml/dom_html.ml @@ -814,11 +814,6 @@ end type event_listener_id = Dom.event_listener_id -class type event_listener_options = - object - inherit Dom.event_listener_options - end - let addEventListener = Dom.addEventListener let addEventListenerWithOptions = Dom.addEventListenerWithOptions diff --git a/lib/js_of_ocaml/dom_html.mli b/lib/js_of_ocaml/dom_html.mli index db48c56876..68e31fda49 100644 --- a/lib/js_of_ocaml/dom_html.mli +++ b/lib/js_of_ocaml/dom_html.mli @@ -2365,16 +2365,13 @@ end type event_listener_id = Dom.event_listener_id -class type event_listener_options = - object - inherit Dom.event_listener_options - end - val addEventListenerWithOptions : (#eventTarget t as 'a) -> 'b Event.typ + -> ?capture:bool t + -> ?once:bool t + -> ?passive:bool t -> ('a, 'b) event_listener - -> #event_listener_options t -> event_listener_id (** Add an event listener. This function matches the option-object variant of the [addEventListener] DOM method, From fda14cda10d3fbc413276f5ffe3c1eb885700c56 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sun, 7 Jul 2019 20:43:26 +0800 Subject: [PATCH 3/3] small tuning --- CHANGES | 2 +- lib/js_of_ocaml/dom.ml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 8bc310a054..c13c1281c6 100644 --- a/CHANGES +++ b/CHANGES @@ -11,7 +11,7 @@ ** Misc: remove cppo dependency ** Misc: remove ppx_tools_versioned dependency in ppx_deriving_json ** Misc: support for ocaml 4.09 - ** Lib: Add support for 'addEventListener' with options object + ** Lib: Add support for 'addEventListener' with options * Bug fixes: ** Compiler: don't generate source if no-source-map passed (#780) diff --git a/lib/js_of_ocaml/dom.ml b/lib/js_of_ocaml/dom.ml index 22cb31f441..d6290d3582 100644 --- a/lib/js_of_ocaml/dom.ml +++ b/lib/js_of_ocaml/dom.ml @@ -370,14 +370,14 @@ let addEventListenerWithOptions (e : (< .. > as 'a) t) typ ?capture ?once ?passi fun () -> (Js.Unsafe.coerce e)##detachEvent ev callback else let opts : event_listener_options t = Js.Unsafe.obj [||] in - let set t f = + let iter t f = match t with | None -> () | Some b -> f b in - set capture (fun b -> opts##.capture := b); - set once (fun b -> opts##.once := b); - set passive (fun b -> opts##.passive := b); + iter capture (fun b -> opts##.capture := b); + iter once (fun b -> opts##.once := b); + iter passive (fun b -> opts##.passive := b); let () = (Js.Unsafe.coerce e)##addEventListener typ h opts in fun () -> (Js.Unsafe.coerce e)##removeEventListener typ h opts