blob: 02af125792c7054f986efc13d4f68595e8e656ee [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
Philipp Schrader817cce32022-03-26 15:00:00 -07003type Tab = 'MatchList' | 'Notes' | 'Entry' | 'ImportMatchList';
Ravago Jones2813c032022-03-16 23:44:11 -07004type TeamInMatch = {
Philipp Schrader817cce32022-03-26 15:00:00 -07005 teamNumber: number;
6 matchNumber: number;
7 compLevel: string;
Ravago Jones2813c032022-03-16 23:44:11 -07008};
Philipp Schrader72beced2022-03-07 05:29:52 -08009
Alex Perryb3168082022-01-22 13:36:13 -080010@Component({
11 selector: 'my-app',
12 templateUrl: './app.ng.html',
Philipp Schrader817cce32022-03-26 15:00:00 -070013 styleUrls: ['./common.css'],
Alex Perryb3168082022-01-22 13:36:13 -080014})
15export class App {
Philipp Schrader817cce32022-03-26 15:00:00 -070016 selectedTeamInMatch: TeamInMatch = {
17 teamNumber: 1,
18 matchNumber: 1,
19 compLevel: 'qm',
20 };
Ravago Jones2813c032022-03-16 23:44:11 -070021 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080022
Ravago Jones2813c032022-03-16 23:44:11 -070023 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070024
Alex Perryd3ccac92022-03-12 13:48:04 -080025 constructor() {
26 window.addEventListener('beforeunload', (e) => {
Philipp Schradercf915462022-03-16 23:42:22 -070027 if (!this.block_alerts.nativeElement.checked) {
Ravago Jones2813c032022-03-16 23:44:11 -070028 // Based on
29 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
Philipp Schradercf915462022-03-16 23:42:22 -070030 // This combination ensures a dialog will be shown on most browsers.
31 e.preventDefault();
32 e.returnValue = '';
33 }
Alex Perryd3ccac92022-03-12 13:48:04 -080034 });
35 }
36
Philipp Schrader72beced2022-03-07 05:29:52 -080037 tabIs(tab: Tab) {
38 return this.tab == tab;
39 }
40
Ravago Jones2813c032022-03-16 23:44:11 -070041 selectTeamInMatch(teamInMatch: TeamInMatch) {
42 this.selectedTeamInMatch = teamInMatch;
43 this.switchTabTo('Entry');
44 }
45
46 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080047 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070048 if (this.tab !== tab) {
49 if (!this.block_alerts.nativeElement.checked) {
50 shouldSwitch = window.confirm('Leave current page?');
51 }
Alex Perry19a87962022-03-12 13:36:10 -080052 }
53 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070054 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080055 }
Philipp Schrader72beced2022-03-07 05:29:52 -080056 }
Ravago Jones2813c032022-03-16 23:44:11 -070057
58 private switchTabTo(tab: Tab) {
59 this.tab = tab;
60 }
Alex Perryb3168082022-01-22 13:36:13 -080061}