blob: 895336b71a9e35a4e18f999b8ea295b30cffc26a [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'
Filip Kujawa210a03b2022-11-24 14:41:11 -08007 | 'DriverRanking'
Ishan Katpallydad5f1a2022-03-23 21:06:36 -07008 | 'ShiftSchedule'
Emily Markovafaecfe12023-07-01 12:40:03 -07009 | 'View'
Philipp Schradere2e27ff2024-02-25 22:08:55 -080010 | 'Pit'
11 | 'Scan';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070012
13// Ignore the guard for tabs that don't require the user to enter any data.
Philipp Schradere2e27ff2024-02-25 22:08:55 -080014const unguardedTabs: Tab[] = ['MatchList', 'Scan', 'View'];
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070015
Ravago Jones2813c032022-03-16 23:44:11 -070016type TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080017 teamNumber: string;
Philipp Schrader817cce32022-03-26 15:00:00 -070018 matchNumber: number;
Philipp Schrader30b4a682022-04-16 14:36:17 -070019 setNumber: number;
Philipp Schrader817cce32022-03-26 15:00:00 -070020 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -070021};
Philipp Schrader72beced2022-03-07 05:29:52 -080022
Alex Perryb3168082022-01-22 13:36:13 -080023@Component({
24 selector: 'my-app',
25 templateUrl: './app.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -080026 styleUrls: ['../app/common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080027})
28export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070029 selectedTeamInMatch: TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080030 teamNumber: '1',
Philipp Schrader817cce32022-03-26 15:00:00 -070031 matchNumber: 1,
Philipp Schrader30b4a682022-04-16 14:36:17 -070032 setNumber: 1,
Philipp Schrader817cce32022-03-26 15:00:00 -070033 compLevel: 'qm',
34 };
Philipp Schrader75021f52023-04-09 21:14:13 -070035 // Keep track of the match list automatically navigating the user to the
36 // Entry tab.
37 navigatedFromMatchList: boolean = false;
Ravago Jones2813c032022-03-16 23:44:11 -070038 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080039
Ravago Jones2813c032022-03-16 23:44:11 -070040 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070041
Alex Perryd3ccac92022-03-12 13:48:04 -080042 constructor() {
43 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070044 if (!unguardedTabs.includes(this.tab)) {
45 if (!this.block_alerts.nativeElement.checked) {
46 // Based on
47 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
48 // This combination ensures a dialog will be shown on most browsers.
49 e.preventDefault();
50 e.returnValue = '';
51 }
Philipp Schradercf915462022-03-16 23:42:22 -070052 }
Alex Perryd3ccac92022-03-12 13:48:04 -080053 });
54 }
55
Philipp Schrader72beced2022-03-07 05:29:52 -080056 tabIs(tab: Tab) {
57 return this.tab == tab;
58 }
59
Ravago Jones2813c032022-03-16 23:44:11 -070060 selectTeamInMatch(teamInMatch: TeamInMatch) {
61 this.selectedTeamInMatch = teamInMatch;
Philipp Schrader75021f52023-04-09 21:14:13 -070062 this.navigatedFromMatchList = true;
63 this.switchTabTo('Entry', false);
Ravago Jones2813c032022-03-16 23:44:11 -070064 }
65
66 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080067 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070068 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070069 if (!unguardedTabs.includes(this.tab)) {
70 if (!this.block_alerts.nativeElement.checked) {
71 shouldSwitch = window.confirm(
72 'Leave current page? You will lose all data.'
73 );
74 }
Philipp Schrader5f190012022-03-15 23:29:09 -070075 }
Alex Perry19a87962022-03-12 13:36:10 -080076 }
77 if (shouldSwitch) {
Philipp Schrader75021f52023-04-09 21:14:13 -070078 this.switchTabTo(tab, true);
Alex Perry19a87962022-03-12 13:36:10 -080079 }
Philipp Schrader72beced2022-03-07 05:29:52 -080080 }
Ravago Jones2813c032022-03-16 23:44:11 -070081
Philipp Schrader75021f52023-04-09 21:14:13 -070082 private switchTabTo(tab: Tab, wasGuarded: boolean) {
83 if (wasGuarded) {
84 // When the user navigated between tabs manually, we want to reset some
85 // state.
86 this.navigatedFromMatchList = false;
87 }
Ravago Jones2813c032022-03-16 23:44:11 -070088 this.tab = tab;
89 }
Alex Perryb3168082022-01-22 13:36:13 -080090}