blob: 7e81d84c2be7c4d00af2c339df1f26d4a5e0b490 [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'
Milo Lin26b2cbb2022-03-26 17:35:20 -07008 | 'ImportMatchList'
Ishan Katpallydad5f1a2022-03-23 21:06:36 -07009 | 'ShiftSchedule'
10 | 'View';
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070011
12// Ignore the guard for tabs that don't require the user to enter any data.
13const unguardedTabs: Tab[] = ['MatchList', 'ImportMatchList'];
14
Ravago Jones2813c032022-03-16 23:44:11 -070015type TeamInMatch = {
Philipp Schrader817cce32022-03-26 15:00:00 -070016 teamNumber: number;
17 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 = {
29 teamNumber: 1,
30 matchNumber: 1,
Philipp Schrader30b4a682022-04-16 14:36:17 -070031 setNumber: 1,
Philipp Schrader817cce32022-03-26 15:00:00 -070032 compLevel: 'qm',
33 };
Ravago Jones2813c032022-03-16 23:44:11 -070034 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080035
Ravago Jones2813c032022-03-16 23:44:11 -070036 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070037
Alex Perryd3ccac92022-03-12 13:48:04 -080038 constructor() {
39 window.addEventListener('beforeunload', (e) => {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070040 if (!unguardedTabs.includes(this.tab)) {
41 if (!this.block_alerts.nativeElement.checked) {
42 // Based on
43 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
44 // This combination ensures a dialog will be shown on most browsers.
45 e.preventDefault();
46 e.returnValue = '';
47 }
Philipp Schradercf915462022-03-16 23:42:22 -070048 }
Alex Perryd3ccac92022-03-12 13:48:04 -080049 });
50 }
51
Philipp Schrader72beced2022-03-07 05:29:52 -080052 tabIs(tab: Tab) {
53 return this.tab == tab;
54 }
55
Ravago Jones2813c032022-03-16 23:44:11 -070056 selectTeamInMatch(teamInMatch: TeamInMatch) {
57 this.selectedTeamInMatch = teamInMatch;
58 this.switchTabTo('Entry');
59 }
60
61 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080062 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070063 if (this.tab !== tab) {
Philipp Schrader9e3ab5a2022-04-03 15:18:29 -070064 if (!unguardedTabs.includes(this.tab)) {
65 if (!this.block_alerts.nativeElement.checked) {
66 shouldSwitch = window.confirm(
67 'Leave current page? You will lose all data.'
68 );
69 }
Philipp Schrader5f190012022-03-15 23:29:09 -070070 }
Alex Perry19a87962022-03-12 13:36:10 -080071 }
72 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070073 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080074 }
Philipp Schrader72beced2022-03-07 05:29:52 -080075 }
Ravago Jones2813c032022-03-16 23:44:11 -070076
77 private switchTabTo(tab: Tab) {
78 this.tab = tab;
79 }
Alex Perryb3168082022-01-22 13:36:13 -080080}