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 | |
Milo Lin | 26b2cbb | 2022-03-26 17:35:20 -0700 | [diff] [blame] | 3 | type Tab = |
| 4 | | 'MatchList' |
| 5 | | 'Notes' |
| 6 | | 'Entry' |
| 7 | | 'ImportMatchList' |
Ishan Katpally | dad5f1a | 2022-03-23 21:06:36 -0700 | [diff] [blame^] | 8 | | 'ShiftSchedule' |
| 9 | | 'View'; |
Philipp Schrader | 9e3ab5a | 2022-04-03 15:18:29 -0700 | [diff] [blame] | 10 | |
| 11 | // Ignore the guard for tabs that don't require the user to enter any data. |
| 12 | const unguardedTabs: Tab[] = ['MatchList', 'ImportMatchList']; |
| 13 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 14 | type TeamInMatch = { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 15 | teamNumber: number; |
| 16 | matchNumber: number; |
| 17 | compLevel: string; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 18 | }; |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 19 | |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 20 | @Component({ |
| 21 | selector: 'my-app', |
| 22 | templateUrl: './app.ng.html', |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 23 | styleUrls: ['./common.css'], |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 24 | }) |
| 25 | export class App { |
Philipp Schrader | 817cce3 | 2022-03-26 15:00:00 -0700 | [diff] [blame] | 26 | selectedTeamInMatch: TeamInMatch = { |
| 27 | teamNumber: 1, |
| 28 | matchNumber: 1, |
| 29 | compLevel: 'qm', |
| 30 | }; |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 31 | tab: Tab = 'MatchList'; |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 32 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 33 | @ViewChild('block_alerts') block_alerts: ElementRef; |
Philipp Schrader | cf91546 | 2022-03-16 23:42:22 -0700 | [diff] [blame] | 34 | |
Alex Perry | d3ccac9 | 2022-03-12 13:48:04 -0800 | [diff] [blame] | 35 | constructor() { |
| 36 | window.addEventListener('beforeunload', (e) => { |
Philipp Schrader | 9e3ab5a | 2022-04-03 15:18:29 -0700 | [diff] [blame] | 37 | if (!unguardedTabs.includes(this.tab)) { |
| 38 | if (!this.block_alerts.nativeElement.checked) { |
| 39 | // Based on |
| 40 | // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example |
| 41 | // This combination ensures a dialog will be shown on most browsers. |
| 42 | e.preventDefault(); |
| 43 | e.returnValue = ''; |
| 44 | } |
Philipp Schrader | cf91546 | 2022-03-16 23:42:22 -0700 | [diff] [blame] | 45 | } |
Alex Perry | d3ccac9 | 2022-03-12 13:48:04 -0800 | [diff] [blame] | 46 | }); |
| 47 | } |
| 48 | |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 49 | tabIs(tab: Tab) { |
| 50 | return this.tab == tab; |
| 51 | } |
| 52 | |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 53 | selectTeamInMatch(teamInMatch: TeamInMatch) { |
| 54 | this.selectedTeamInMatch = teamInMatch; |
| 55 | this.switchTabTo('Entry'); |
| 56 | } |
| 57 | |
| 58 | switchTabToGuarded(tab: Tab) { |
Alex Perry | 19a8796 | 2022-03-12 13:36:10 -0800 | [diff] [blame] | 59 | let shouldSwitch = true; |
Philipp Schrader | 5f19001 | 2022-03-15 23:29:09 -0700 | [diff] [blame] | 60 | if (this.tab !== tab) { |
Philipp Schrader | 9e3ab5a | 2022-04-03 15:18:29 -0700 | [diff] [blame] | 61 | if (!unguardedTabs.includes(this.tab)) { |
| 62 | if (!this.block_alerts.nativeElement.checked) { |
| 63 | shouldSwitch = window.confirm( |
| 64 | 'Leave current page? You will lose all data.' |
| 65 | ); |
| 66 | } |
Philipp Schrader | 5f19001 | 2022-03-15 23:29:09 -0700 | [diff] [blame] | 67 | } |
Alex Perry | 19a8796 | 2022-03-12 13:36:10 -0800 | [diff] [blame] | 68 | } |
| 69 | if (shouldSwitch) { |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 70 | this.switchTabTo(tab); |
Alex Perry | 19a8796 | 2022-03-12 13:36:10 -0800 | [diff] [blame] | 71 | } |
Philipp Schrader | 72beced | 2022-03-07 05:29:52 -0800 | [diff] [blame] | 72 | } |
Ravago Jones | 2813c03 | 2022-03-16 23:44:11 -0700 | [diff] [blame] | 73 | |
| 74 | private switchTabTo(tab: Tab) { |
| 75 | this.tab = tab; |
| 76 | } |
Alex Perry | b316808 | 2022-01-22 13:36:13 -0800 | [diff] [blame] | 77 | } |