blob: 645c224aa0812fe02c96e8bbff2ae441d056bd72 [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
Philipp Schraderba315da2024-03-17 16:16:50 -07003import {CompLevel} from '@org_frc971/scouting/www/entry';
4
Milo Lin26b2cbb2022-03-26 17:35:20 -07005type Tab =
6 | 'MatchList'
7 | 'Notes'
8 | 'Entry'
Filip Kujawa210a03b2022-11-24 14:41:11 -08009 | 'DriverRanking'
Ishan Katpallydad5f1a2022-03-23 21:06:36 -070010 | 'ShiftSchedule'
Emily Markovafaecfe12023-07-01 12:40:03 -070011 | 'View'
Philipp Schradere2e27ff2024-02-25 22:08:55 -080012 | 'Pit'
13 | 'Scan';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070014
15// Ignore the guard for tabs that don't require the user to enter any data.
Philipp Schradere2e27ff2024-02-25 22:08:55 -080016const unguardedTabs: Tab[] = ['MatchList', 'Scan', 'View'];
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070017
Ravago Jones2813c032022-03-16 23:44:11 -070018type TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080019 teamNumber: string;
Philipp Schrader817cce32022-03-26 15:00:00 -070020 matchNumber: number;
Philipp Schrader30b4a682022-04-16 14:36:17 -070021 setNumber: number;
Philipp Schraderba315da2024-03-17 16:16:50 -070022 compLevel: CompLevel;
Ravago Jones2813c032022-03-16 23:44:11 -070023};
Philipp Schrader72beced2022-03-07 05:29:52 -080024
Alex Perryb3168082022-01-22 13:36:13 -080025@Component({
26 selector: 'my-app',
27 templateUrl: './app.ng.html',
Philipp Schrader175a93c2023-02-19 13:13:40 -080028 styleUrls: ['../app/common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080029})
30export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070031 selectedTeamInMatch: TeamInMatch = {
Emily Markovae68b7632023-12-30 14:17:55 -080032 teamNumber: '1',
Philipp Schrader817cce32022-03-26 15:00:00 -070033 matchNumber: 1,
Philipp Schrader30b4a682022-04-16 14:36:17 -070034 setNumber: 1,
Philipp Schrader817cce32022-03-26 15:00:00 -070035 compLevel: 'qm',
36 };
Philipp Schrader75021f52023-04-09 21:14:13 -070037 // Keep track of the match list automatically navigating the user to the
38 // Entry tab.
39 navigatedFromMatchList: boolean = false;
Ravago Jones2813c032022-03-16 23:44:11 -070040 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080041
Ravago Jones2813c032022-03-16 23:44:11 -070042 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070043
Alex Perryd3ccac92022-03-12 13:48:04 -080044 constructor() {
45 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}