blob: ab15ae550598efb4416f54fce0fd12bf0d759213 [file] [log] [blame]
Emily Markova7b786402024-01-24 20:05:24 -08001import {Component, ElementRef, ViewChild, isDevMode} 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() {
Emily Markova7b786402024-01-24 20:05:24 -080043 console.log(`Using development mode: ${isDevMode()}`);
44
Alex Perryd3ccac92022-03-12 13:48:04 -080045 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070046 if (!unguardedTabs.includes(this.tab)) {
47 if (!this.block_alerts.nativeElement.checked) {
48 // Based on
49 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
50 // This combination ensures a dialog will be shown on most browsers.
51 e.preventDefault();
52 e.returnValue = '';
53 }
Philipp Schradercf915462022-03-16 23:42:22 -070054 }
Alex Perryd3ccac92022-03-12 13:48:04 -080055 });
56 }
57
Philipp Schrader72beced2022-03-07 05:29:52 -080058 tabIs(tab: Tab) {
59 return this.tab == tab;
60 }
61
Ravago Jones2813c032022-03-16 23:44:11 -070062 selectTeamInMatch(teamInMatch: TeamInMatch) {
63 this.selectedTeamInMatch = teamInMatch;
Philipp Schrader75021f52023-04-09 21:14:13 -070064 this.navigatedFromMatchList = true;
65 this.switchTabTo('Entry', false);
Ravago Jones2813c032022-03-16 23:44:11 -070066 }
67
68 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080069 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070070 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070071 if (!unguardedTabs.includes(this.tab)) {
72 if (!this.block_alerts.nativeElement.checked) {
73 shouldSwitch = window.confirm(
74 'Leave current page? You will lose all data.'
75 );
76 }
Philipp Schrader5f190012022-03-15 23:29:09 -070077 }
Alex Perry19a87962022-03-12 13:36:10 -080078 }
79 if (shouldSwitch) {
Philipp Schrader75021f52023-04-09 21:14:13 -070080 this.switchTabTo(tab, true);
Alex Perry19a87962022-03-12 13:36:10 -080081 }
Philipp Schrader72beced2022-03-07 05:29:52 -080082 }
Ravago Jones2813c032022-03-16 23:44:11 -070083
Philipp Schrader75021f52023-04-09 21:14:13 -070084 private switchTabTo(tab: Tab, wasGuarded: boolean) {
85 if (wasGuarded) {
86 // When the user navigated between tabs manually, we want to reset some
87 // state.
88 this.navigatedFromMatchList = false;
89 }
Ravago Jones2813c032022-03-16 23:44:11 -070090 this.tab = tab;
91 }
Alex Perryb3168082022-01-22 13:36:13 -080092}