Philipp Schrader | cf91546 | 2022-03-16 23:42:22 -0700 | [diff] [blame] | 1 | import {Component, ElementRef, ViewChild} from '@angular/core'; |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 2 | |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 3 | type Tab = 'Entry'|'ImportMatchList'; |
| 4 | |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 5 | @Component({ |
| 6 | selector: 'my-app', |
| 7 | templateUrl: './app.ng.html', |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 8 | styleUrls: ['./common.css'] |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 9 | }) |
| 10 | export class App { |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 11 | tab: Tab = 'Entry'; |
| 12 | |
Philipp Schrader | cf91546 | 2022-03-16 23:42:22 -0700 | [diff] [blame] | 13 | @ViewChild("block_alerts") block_alerts: ElementRef; |
| 14 | |
Alex Perry | d3ccac9 | 2022-03-12 13:48:04 -0800 | [diff] [blame] | 15 | constructor() { |
| 16 | window.addEventListener('beforeunload', (e) => { |
Philipp Schrader | cf91546 | 2022-03-16 23:42:22 -0700 | [diff] [blame] | 17 | 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 Perry | d3ccac9 | 2022-03-12 13:48:04 -0800 | [diff] [blame] | 23 | }); |
| 24 | } |
| 25 | |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 26 | tabIs(tab: Tab) { |
| 27 | return this.tab == tab; |
| 28 | } |
| 29 | |
| 30 | switchTabTo(tab: Tab) { |
Alex Perry | 19a8796 | 2022-03-12 13:36:10 -0800 | [diff] [blame] | 31 | let shouldSwitch = true; |
Philipp Schrader | 5f19001 | 2022-03-15 23:29:09 -0700 | [diff] [blame] | 32 | if (this.tab !== tab) { |
| 33 | if (!this.block_alerts.nativeElement.checked) { |
| 34 | shouldSwitch = window.confirm('Leave current page?'); |
| 35 | } |
Alex Perry | 19a8796 | 2022-03-12 13:36:10 -0800 | [diff] [blame] | 36 | } |
| 37 | if (shouldSwitch) { |
| 38 | this.tab = tab; |
| 39 | } |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 40 | } |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 41 | } |