From 62c2f599723a127b5574be88ba57f571c27f2ad2 Mon Sep 17 00:00:00 2001 From: monstasat Date: Thu, 26 Sep 2019 09:52:36 +0300 Subject: [PATCH] Added support for custom events --- lib/js_of_ocaml/dom.ml | 25 +++++++++++++++++++++++++ lib/js_of_ocaml/dom.mli | 15 +++++++++++++++ lib/js_of_ocaml/dom_html.ml | 9 +++++++++ lib/js_of_ocaml/dom_html.mli | 15 +++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/lib/js_of_ocaml/dom.ml b/lib/js_of_ocaml/dom.ml index d6290d3582..c809e887d4 100644 --- a/lib/js_of_ocaml/dom.ml +++ b/lib/js_of_ocaml/dom.ml @@ -292,6 +292,13 @@ class type ['a] event = method srcElement : 'a t opt readonly_prop end +class type ['a, 'b] customEvent = + object + inherit ['a] event + + method detail : 'b Js.opt Js.readonly_prop + end + let no_handler : ('a, 'b) event_listener = Js.null let window_event () : 'a #event t = Js.Unsafe.pure_js_expr "event" @@ -391,6 +398,24 @@ let preventDefault ev = then (Js.Unsafe.coerce ev)##preventDefault else (Js.Unsafe.coerce ev)##.returnValue := Js.bool false +let createCustomEvent ?bubbles ?cancelable ?detail typ = + let opt_iter f = function + | None -> () + | Some x -> f x + in + let opts = Unsafe.obj [||] in + opt_iter (fun x -> opts##.bubbles := bool x) bubbles; + opt_iter (fun x -> opts##.cancelable := bool x) cancelable; + opt_iter (fun x -> opts##.detail := some x) detail; + let constr : + ( ('a, 'b) #customEvent Js.t Event.typ + -> < detail : 'b opt prop > t + -> ('a, 'b) customEvent t) + constr = + Unsafe.global##._CustomEvent + in + new%js constr typ opts + (* IE < 9 *) class type stringList = diff --git a/lib/js_of_ocaml/dom.mli b/lib/js_of_ocaml/dom.mli index 0de17e2eba..9e066e64a2 100644 --- a/lib/js_of_ocaml/dom.mli +++ b/lib/js_of_ocaml/dom.mli @@ -304,6 +304,13 @@ class type ['a] event = method srcElement : 'a t opt readonly_prop end +class type ['a, 'b] customEvent = + object + inherit ['a] event + + method detail : 'b Js.opt Js.readonly_prop + end + (** {2 Event handlers} *) val no_handler : ('a, 'b) event_listener @@ -363,6 +370,14 @@ val preventDefault : 'a #event Js.t -> unit (** Call this to prevent the default handler for the event. To stop propagation of the event, call {!Dom_html.stopPropagation}. *) +val createCustomEvent : + ?bubbles:bool + -> ?cancelable:bool + -> ?detail:'b + -> ('a, 'b) #customEvent Js.t Event.typ + -> ('a, 'b) customEvent Js.t +(** Create a custom event of given type. *) + (** {2 Other DOM objects} *) class type stringList = diff --git a/lib/js_of_ocaml/dom_html.ml b/lib/js_of_ocaml/dom_html.ml index cc64cb99ba..4fa15ee873 100644 --- a/lib/js_of_ocaml/dom_html.ml +++ b/lib/js_of_ocaml/dom_html.ml @@ -266,6 +266,11 @@ class type event = inherit [element] Dom.event end +and ['a] customEvent = + object + inherit [element, 'a] Dom.customEvent + end + and mouseEvent = object inherit event @@ -477,6 +482,8 @@ and eventTarget = ('self t, animationEvent t) event_listener writeonly_prop method onanimationcancel : ('self t, animationEvent t) event_listener writeonly_prop + + method dispatchEvent : event t -> bool t meth end and popStateEvent = @@ -820,6 +827,8 @@ let addEventListenerWithOptions = Dom.addEventListenerWithOptions let removeEventListener = Dom.removeEventListener +let createCustomEvent = Dom.createCustomEvent + class type ['node] collection = object method length : int readonly_prop diff --git a/lib/js_of_ocaml/dom_html.mli b/lib/js_of_ocaml/dom_html.mli index 68e31fda49..8918f6b0bb 100644 --- a/lib/js_of_ocaml/dom_html.mli +++ b/lib/js_of_ocaml/dom_html.mli @@ -271,6 +271,11 @@ class type event = inherit [element] Dom.event end +and ['a] customEvent = + object + inherit [element, 'a] Dom.customEvent + end + and mouseEvent = object inherit event @@ -489,6 +494,8 @@ and eventTarget = ('self t, animationEvent t) event_listener writeonly_prop method onanimationcancel : ('self t, animationEvent t) event_listener writeonly_prop + + method dispatchEvent : event t -> bool t meth end and popStateEvent = @@ -2399,6 +2406,14 @@ val addMousewheelEventListener : event and the numbers of ticks the mouse wheel moved. Positive means down / right. *) +val createCustomEvent : + ?bubbles:bool + -> ?cancelable:bool + -> ?detail:'a + -> 'a #customEvent Js.t Event.typ + -> 'a customEvent Js.t +(** See [Dom.createCustomEvent] *) + (****) (** {2 Mouse event helper functions} *)