Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/js_of_ocaml/dom.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Comment thread
TyOverby marked this conversation as resolved.
in
new%js constr typ opts

(* IE < 9 *)

class type stringList =
Expand Down
15 changes: 15 additions & 0 deletions lib/js_of_ocaml/dom.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
9 changes: 9 additions & 0 deletions lib/js_of_ocaml/dom_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/js_of_ocaml/dom_html.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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} *)
Expand Down