blob: 9d6c539aeed83b4cc5019e57d73787135db98f2d [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
Milo Lin26b2cbb2022-03-26 17:35:20 -07003type Tab =
4 | 'MatchList'
5 | 'Notes'
6 | 'Entry'
7 | 'ImportMatchList'
Ishan Katpallydad5f1a2022-03-23 21:06:36 -07008 | 'ShiftSchedule'
9 | 'View';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070010
11// Ignore the guard for tabs that don't require the user to enter any data.
12const unguardedTabs: Tab[] = ['MatchList', 'ImportMatchList'];
13
Ravago Jones2813c032022-03-16 23:44:11 -070014type TeamInMatch = {
Philipp Schrader817cce32022-03-26 15:00:00 -070015 teamNumber: number;
16 matchNumber: number;
17 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -070018};
Philipp Schrader72beced2022-03-07 05:29:52 -080019
Alex Perryb3168082022-01-22 13:36:13 -080020@Component({
21 selector: 'my-app',
22 templateUrl: './app.ng.html',
Philipp Schrader817cce32022-03-26 15:00:00 -070023 styleUrls: ['./common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080024})
25export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070026 selectedTeamInMatch: TeamInMatch = {
27 teamNumber: 1,
28 matchNumber: 1,
29 compLevel: 'qm',
30 };
Ravago Jones2813c032022-03-16 23:44:11 -070031 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080032
Ravago Jones2813c032022-03-16 23:44:11 -070033 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070034
Alex Perryd3ccac92022-03-12 13:48:04 -080035 constructor() {
36 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070037 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 Schradercf915462022-03-16 23:42:22 -070045 }
Alex Perryd3ccac92022-03-12 13:48:04 -080046 });
47 }
48
Philipp Schrader72beced2022-03-07 05:29:52 -080049 tabIs(tab: Tab) {
50 return this.tab == tab;
51 }
52
Ravago Jones2813c032022-03-16 23:44:11 -070053 selectTeamInMatch(teamInMatch: TeamInMatch) {
54 this.selectedTeamInMatch = teamInMatch;
55 this.switchTabTo('Entry');
56 }
57
58 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080059 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070060 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070061 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 Schrader5f190012022-03-15 23:29:09 -070067 }
Alex Perry19a87962022-03-12 13:36:10 -080068 }
69 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070070 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080071 }
Philipp Schrader72beced2022-03-07 05:29:52 -080072 }
Ravago Jones2813c032022-03-16 23:44:11 -070073
74 private switchTabTo(tab: Tab) {
75 this.tab = tab;
76 }
Alex Perryb3168082022-01-22 13:36:13 -080077}