blob: 011c94fe1d11606a0d4ddc2fa8366429a4345bc2 [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'
10 | 'Pit';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070011
12// Ignore the guard for tabs that don't require the user to enter any data.
Philipp Schrader43c730b2023-02-26 20:27:44 -080013const unguardedTabs: Tab[] = ['MatchList'];
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070014
Ravago Jones2813c032022-03-16 23:44:11 -070015type TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080016 teamNumber: string;
Philipp Schrader817cce32022-03-26 15:00:00 -070017 matchNumber: number;
Philipp Schrader30b4a682022-04-16 14:36:17 -070018 setNumber: number;
Philipp Schrader817cce32022-03-26 15:00:00 -070019 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -070020};
Philipp Schrader72beced2022-03-07 05:29:52 -080021
Alex Perryb3168082022-01-22 13:36:13 -080022@Component({
23 selector: 'my-app',
24 templateUrl: './app.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -080025 styleUrls: ['../app/common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080026})
27export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070028 selectedTeamInMatch: TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080029 teamNumber: '1',
Philipp Schrader817cce32022-03-26 15:00:00 -070030 matchNumber: 1,
Philipp Schrader30b4a682022-04-16 14:36:17 -070031 setNumber: 1,
Philipp Schrader817cce32022-03-26 15:00:00 -070032 compLevel: 'qm',
33 };
Philipp Schrader75021f52023-04-09 21:14:13 -070034 // Keep track of the match list automatically navigating the user to the
35 // Entry tab.
36 navigatedFromMatchList: boolean = false;
Ravago Jones2813c032022-03-16 23:44:11 -070037 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080038
Ravago Jones2813c032022-03-16 23:44:11 -070039 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070040
Alex Perryd3ccac92022-03-12 13:48:04 -080041 constructor() {
42 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070043 if (!unguardedTabs.includes(this.tab)) {
44 if (!this.block_alerts.nativeElement.checked) {
45 // Based on
46 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
47 // This combination ensures a dialog will be shown on most browsers.
48 e.preventDefault();
49 e.returnValue = '';
50 }
Philipp Schradercf915462022-03-16 23:42:22 -070051 }
Alex Perryd3ccac92022-03-12 13:48:04 -080052 });
53 }
54
Philipp Schrader72beced2022-03-07 05:29:52 -080055 tabIs(tab: Tab) {
56 return this.tab == tab;
57 }
58
Ravago Jones2813c032022-03-16 23:44:11 -070059 selectTeamInMatch(teamInMatch: TeamInMatch) {
60 this.selectedTeamInMatch = teamInMatch;
Philipp Schrader75021f52023-04-09 21:14:13 -070061 this.navigatedFromMatchList = true;
62 this.switchTabTo('Entry', false);
Ravago Jones2813c032022-03-16 23:44:11 -070063 }
64
65 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080066 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070067 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070068 if (!unguardedTabs.includes(this.tab)) {
69 if (!this.block_alerts.nativeElement.checked) {
70 shouldSwitch = window.confirm(
71 'Leave current page? You will lose all data.'
72 );
73 }
Philipp Schrader5f190012022-03-15 23:29:09 -070074 }
Alex Perry19a87962022-03-12 13:36:10 -080075 }
76 if (shouldSwitch) {
Philipp Schrader75021f52023-04-09 21:14:13 -070077 this.switchTabTo(tab, true);
Alex Perry19a87962022-03-12 13:36:10 -080078 }
Philipp Schrader72beced2022-03-07 05:29:52 -080079 }
Ravago Jones2813c032022-03-16 23:44:11 -070080
Philipp Schrader75021f52023-04-09 21:14:13 -070081 private switchTabTo(tab: Tab, wasGuarded: boolean) {
82 if (wasGuarded) {
83 // When the user navigated between tabs manually, we want to reset some
84 // state.
85 this.navigatedFromMatchList = false;
86 }
Ravago Jones2813c032022-03-16 23:44:11 -070087 this.tab = tab;
88 }
Alex Perryb3168082022-01-22 13:36:13 -080089}