Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Implement adding event listeners for beforeunload #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions src/Webapi/Dom/Webapi__Dom__BeforeUnloadEvent.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ type t = Dom.beforeUnloadEvent;
include Webapi__Dom__Event.Impl({ type nonrec t = t; });

[@bs.new] external make : string => t = "BeforeUnloadEvent";
[@bs.new] external makeWithOptions : (string, Js.t({..})) => t = "BeforeUnloadEvent";

[@bs.get] external returnValue : t => string = "";
[@bs.new] external makeWithOptions : (string, Js.t({..})) => t = "BeforeUnloadEvent";
4 changes: 4 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__Event.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Impl = (T: {type t;}) => {
[@bs.get] external currentTarget : T.t => Dom.eventTarget = "";
[@bs.get] external defaultPrevented : T.t => bool = "";
[@bs.get] external eventPhase : T.t => int /* eventPhase enum */ = "";
[@bs.get] external returnValue : T.t => string = "";

let eventPhase: T.t => Webapi__Dom__Types.EventPhase.t =
(self) => Webapi__Dom__Types.EventPhase.decode(eventPhase(self));
Expand All @@ -17,6 +18,9 @@ module Impl = (T: {type t;}) => {
[@bs.send.pipe : T.t] external preventDefault : unit = "";
[@bs.send.pipe : T.t] external stopImmediatePropagation : unit = "";
[@bs.send.pipe : T.t] external stopPropagation : unit = "";

[@bs.set] external setReturnValue: (T.t, string) => unit = "returnValue";
let deleteReturnValue: T.t => unit = [%raw {| event => delete event['returnValue'] |}];
};

type t = Dom.event;
Expand Down
2 changes: 2 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__Window.re
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ module Impl = (T: {type t;}) => {

[@bs.send.pipe : t_window] external addPopStateEventListener : ([@bs.as "popstate"] _, Dom.popStateEvent => unit) => unit = "addEventListener";
[@bs.send.pipe : t_window] external removePopStateEventListener : ([@bs.as "popstate"] _, Dom.popStateEvent => unit) => unit= "removeEventListener";
[@bs.send.pipe : t_window] external addBeforeUnloadEventListener : ([@bs.as "beforeunload"] _, Dom.beforeUnloadEvent => unit) => unit = "addEventListener";
[@bs.send.pipe : t_window] external removeBeforeUnloadEventListener : ([@bs.as "beforeunload"] _, Dom.beforeUnloadEvent => unit) => unit= "removeEventListener";

[@bs.set] external setOnLoad : (t_window, unit => unit) => unit = "onload"; /* use addEventListener instead? */
};
Expand Down
5 changes: 2 additions & 3 deletions tests/Webapi/Dom/Webapi__Dom__BeforeUnloadEvent__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ let _ = target(event);
let _ = timeStamp(event);
let _ = type_(event);
let _ = isTrusted(event);
let _ = returnValue(event);

preventDefault(event);
stopImmediatePropagation(event);
stopPropagation(event);

/* BeforeUnloadEvent */
let _ = returnValue(event);
setReturnValue(event, "Test");
3 changes: 3 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__Event__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ let _ = target(event);
let _ = timeStamp(event);
let _ = type_(event);
let _ = isTrusted(event);
let _ = returnValue(event);

preventDefault(event);
stopImmediatePropagation(event);
stopPropagation(event);
setReturnValue(event, "Test");
deleteReturnValue(event);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you commit the JS artifacts as well so we can ensure this generates correct code?

2 changes: 2 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__Window__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ let _ = Window.resizeTo(120, 300, window);
let _ = Window.scroll(0.0, 0.0, window);
let _ = Window.scrollBy(10.0, (-10.0), window);
let _ = Window.scrollTo(120.5, 300.3, window);
Window.addBeforeUnloadEventListener((_: BeforeUnloadEvent.t) => (), window);
Window.removeBeforeUnloadEventListener((_: BeforeUnloadEvent.t) => (), window);
Window.stop(window);
Window.setOnLoad(window, () => print_endline("load"));