From 4ea3163c314c78a9bf46f00451f28f959fc498c6 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio@bekerle.com> Date: Fri, 29 May 2020 10:56:35 +0200 Subject: [PATCH] Add beforeunload support (VPU/Apps/Signature#7) --- packages/app-shell/src/app-shell.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/app-shell/src/app-shell.js b/packages/app-shell/src/app-shell.js index e6f191bc..acb3446e 100644 --- a/packages/app-shell/src/app-shell.js +++ b/packages/app-shell/src/app-shell.js @@ -286,6 +286,23 @@ export class AppShell extends ScopedElementsMixin(LitElement) { onMenuItemClick(e) { e.preventDefault(); + + // if not the current page was clicked we need to check if the page can be left + if (!e.currentTarget.className.includes("selected")) { + // simulate a "beforeunload" event + const event = new CustomEvent("beforeunload", { + bubbles: true, + cancelable: true, + }); + + const eventResult = window.dispatchEvent(event); + + // if someone canceled the "beforeunload" event we don't want to leave the page + if (!eventResult) { + return; + } + } + const link = e.composedPath()[0]; const location = link.getAttribute('href'); this.router.updateFromPathname(location); -- GitLab