blob: ab9c229aab57d1983be630c6a89f9eed1a69466c [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'
8 | 'ShiftSchedule';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -07009
10// Ignore the guard for tabs that don't require the user to enter any data.
11const unguardedTabs: Tab[] = ['MatchList', 'ImportMatchList'];
12
Ravago Jones2813c032022-03-16 23:44:11 -070013type TeamInMatch = {
Philipp Schrader817cce32022-03-26 15:00:00 -070014 teamNumber: number;
15 matchNumber: number;
16 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -070017};
Philipp Schrader72beced2022-03-07 05:29:52 -080018
Alex Perryb3168082022-01-22 13:36:13 -080019@Component({
20 selector: 'my-app',
21 templateUrl: './app.ng.html',
Philipp Schrader817cce32022-03-26 15:00:00 -070022 styleUrls: ['./common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080023})
24export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070025 selectedTeamInMatch: TeamInMatch = {
26 teamNumber: 1,
27 matchNumber: 1,
28 compLevel: 'qm',
29 };
Ravago Jones2813c032022-03-16 23:44:11 -070030 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080031
Ravago Jones2813c032022-03-16 23:44:11 -070032 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070033
Alex Perryd3ccac92022-03-12 13:48:04 -080034 constructor() {
35 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070036 if (!unguardedTabs.includes(this.tab)) {
37 if (!this.block_alerts.nativeElement.checked) {
38 // Based on
39 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
40 // This combination ensures a dialog will be shown on most browsers.
41 e.preventDefault();
42 e.returnValue = '';
43 }
Philipp Schradercf915462022-03-16 23:42:22 -070044 }
Alex Perryd3ccac92022-03-12 13:48:04 -080045 });
46 }
47
Philipp Schrader72beced2022-03-07 05:29:52 -080048 tabIs(tab: Tab) {
49 return this.tab == tab;
50 }
51
Ravago Jones2813c032022-03-16 23:44:11 -070052 selectTeamInMatch(teamInMatch: TeamInMatch) {
53 this.selectedTeamInMatch = teamInMatch;
54 this.switchTabTo('Entry');
55 }
56
57 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080058 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070059 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070060 if (!unguardedTabs.includes(this.tab)) {
61 if (!this.block_alerts.nativeElement.checked) {
62 shouldSwitch = window.confirm(
63 'Leave current page? You will lose all data.'
64 );
65 }
Philipp Schrader5f190012022-03-15 23:29:09 -070066 }
Alex Perry19a87962022-03-12 13:36:10 -080067 }
68 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070069 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080070 }
Philipp Schrader72beced2022-03-07 05:29:52 -080071 }
Ravago Jones2813c032022-03-16 23:44:11 -070072
73 private switchTabTo(tab: Tab) {
74 this.tab = tab;
75 }
Alex Perryb3168082022-01-22 13:36:13 -080076}