blob: e34283a1073175326aa5f76f249450df4ac0244a [file] [log] [blame]
Philipp Schradercf915462022-03-16 23:42:22 -07001import {Component, ElementRef, ViewChild} from '@angular/core';
Alex Perryb3168082022-01-22 13:36:13 -08002
Philipp Schrader72beced2022-03-07 05:29:52 -08003type Tab = 'Entry'|'ImportMatchList';
4
Alex Perryb3168082022-01-22 13:36:13 -08005@Component({
6 selector: 'my-app',
7 templateUrl: './app.ng.html',
Philipp Schrader72beced2022-03-07 05:29:52 -08008 styleUrls: ['./common.css']
Alex Perryb3168082022-01-22 13:36:13 -08009})
10export class App {
Philipp Schrader72beced2022-03-07 05:29:52 -080011 tab: Tab = 'Entry';
12
Philipp Schradercf915462022-03-16 23:42:22 -070013 @ViewChild("block_alerts") block_alerts: ElementRef;
14
Alex Perryd3ccac92022-03-12 13:48:04 -080015 constructor() {
16 window.addEventListener('beforeunload', (e) => {
Philipp Schradercf915462022-03-16 23:42:22 -070017 if (!this.block_alerts.nativeElement.checked) {
18 // Based on https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
19 // This combination ensures a dialog will be shown on most browsers.
20 e.preventDefault();
21 e.returnValue = '';
22 }
Alex Perryd3ccac92022-03-12 13:48:04 -080023 });
24 }
25
Philipp Schrader72beced2022-03-07 05:29:52 -080026 tabIs(tab: Tab) {
27 return this.tab == tab;
28 }
29
30 switchTabTo(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080031 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070032 if (this.tab !== tab) {
33 if (!this.block_alerts.nativeElement.checked) {
34 shouldSwitch = window.confirm('Leave current page?');
35 }
Alex Perry19a87962022-03-12 13:36:10 -080036 }
37 if (shouldSwitch) {
38 this.tab = tab;
39 }
Philipp Schrader72beced2022-03-07 05:29:52 -080040 }
Alex Perryb3168082022-01-22 13:36:13 -080041}