blob: c19895afa9472db6d562f5e6758ceb8d8579cea8 [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'
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.
Philipp Schrader43c730b2023-02-26 20:27:44 -080012const unguardedTabs: Tab[] = ['MatchList'];
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070013
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 Schrader175a93c2023-02-19 13:13:40 -080024 styleUrls: ['../app/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 };
Philipp Schrader75021f52023-04-09 21:14:13 -070033 // Keep track of the match list automatically navigating the user to the
34 // Entry tab.
35 navigatedFromMatchList: boolean = false;
Ravago Jones2813c032022-03-16 23:44:11 -070036 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080037
Ravago Jones2813c032022-03-16 23:44:11 -070038 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070039
Alex Perryd3ccac92022-03-12 13:48:04 -080040 constructor() {
41 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070042 if (!unguardedTabs.includes(this.tab)) {
43 if (!this.block_alerts.nativeElement.checked) {
44 // Based on
45 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
46 // This combination ensures a dialog will be shown on most browsers.
47 e.preventDefault();
48 e.returnValue = '';
49 }
Philipp Schradercf915462022-03-16 23:42:22 -070050 }
Alex Perryd3ccac92022-03-12 13:48:04 -080051 });
52 }
53
Philipp Schrader72beced2022-03-07 05:29:52 -080054 tabIs(tab: Tab) {
55 return this.tab == tab;
56 }
57
Ravago Jones2813c032022-03-16 23:44:11 -070058 selectTeamInMatch(teamInMatch: TeamInMatch) {
59 this.selectedTeamInMatch = teamInMatch;
Philipp Schrader75021f52023-04-09 21:14:13 -070060 this.navigatedFromMatchList = true;
61 this.switchTabTo('Entry', false);
Ravago Jones2813c032022-03-16 23:44:11 -070062 }
63
64 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080065 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070066 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070067 if (!unguardedTabs.includes(this.tab)) {
68 if (!this.block_alerts.nativeElement.checked) {
69 shouldSwitch = window.confirm(
70 'Leave current page? You will lose all data.'
71 );
72 }
Philipp Schrader5f190012022-03-15 23:29:09 -070073 }
Alex Perry19a87962022-03-12 13:36:10 -080074 }
75 if (shouldSwitch) {
Philipp Schrader75021f52023-04-09 21:14:13 -070076 this.switchTabTo(tab, true);
Alex Perry19a87962022-03-12 13:36:10 -080077 }
Philipp Schrader72beced2022-03-07 05:29:52 -080078 }
Ravago Jones2813c032022-03-16 23:44:11 -070079
Philipp Schrader75021f52023-04-09 21:14:13 -070080 private switchTabTo(tab: Tab, wasGuarded: boolean) {
81 if (wasGuarded) {
82 // When the user navigated between tabs manually, we want to reset some
83 // state.
84 this.navigatedFromMatchList = false;
85 }
Ravago Jones2813c032022-03-16 23:44:11 -070086 this.tab = tab;
87 }
Alex Perryb3168082022-01-22 13:36:13 -080088}