blob: 4f95c90f4ffb2db12a08661109ee7817fe2e355a [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;
Philipp Schrader30b4a682022-04-16 14:36:17 -070017 setNumber: number;
Philipp Schrader817cce32022-03-26 15:00:00 -070018 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -070019};
Philipp Schrader72beced2022-03-07 05:29:52 -080020
Alex Perryb3168082022-01-22 13:36:13 -080021@Component({
22 selector: 'my-app',
23 templateUrl: './app.ng.html',
Philipp Schrader817cce32022-03-26 15:00:00 -070024 styleUrls: ['./common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080025})
26export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070027 selectedTeamInMatch: TeamInMatch = {
28 teamNumber: 1,
29 matchNumber: 1,
Philipp Schrader30b4a682022-04-16 14:36:17 -070030 setNumber: 1,
Philipp Schrader817cce32022-03-26 15:00:00 -070031 compLevel: 'qm',
32 };
Ravago Jones2813c032022-03-16 23:44:11 -070033 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080034
Ravago Jones2813c032022-03-16 23:44:11 -070035 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070036
Alex Perryd3ccac92022-03-12 13:48:04 -080037 constructor() {
38 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070039 if (!unguardedTabs.includes(this.tab)) {
40 if (!this.block_alerts.nativeElement.checked) {
41 // Based on
42 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
43 // This combination ensures a dialog will be shown on most browsers.
44 e.preventDefault();
45 e.returnValue = '';
46 }
Philipp Schradercf915462022-03-16 23:42:22 -070047 }
Alex Perryd3ccac92022-03-12 13:48:04 -080048 });
49 }
50
Philipp Schrader72beced2022-03-07 05:29:52 -080051 tabIs(tab: Tab) {
52 return this.tab == tab;
53 }
54
Ravago Jones2813c032022-03-16 23:44:11 -070055 selectTeamInMatch(teamInMatch: TeamInMatch) {
56 this.selectedTeamInMatch = teamInMatch;
57 this.switchTabTo('Entry');
58 }
59
60 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080061 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070062 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070063 if (!unguardedTabs.includes(this.tab)) {
64 if (!this.block_alerts.nativeElement.checked) {
65 shouldSwitch = window.confirm(
66 'Leave current page? You will lose all data.'
67 );
68 }
Philipp Schrader5f190012022-03-15 23:29:09 -070069 }
Alex Perry19a87962022-03-12 13:36:10 -080070 }
71 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070072 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080073 }
Philipp Schrader72beced2022-03-07 05:29:52 -080074 }
Ravago Jones2813c032022-03-16 23:44:11 -070075
76 private switchTabTo(tab: Tab) {
77 this.tab = tab;
78 }
Alex Perryb3168082022-01-22 13:36:13 -080079}