blob: fbde74fc732e1aa56b5d81de0f965458169ecc79 [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
Ravago Jones2813c032022-03-16 23:44:11 -07003type Tab = 'MatchList'|'Entry'|'ImportMatchList';
4type TeamInMatch = {
5 teamNumber: number,
6 matchNumber: number,
7 compLevel: string
8};
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 Schrader72beced2022-03-07 05:29:52 -080013 styleUrls: ['./common.css']
Alex Perryb3168082022-01-22 13:36:13 -080014})
15export class App {
Ravago Jones2813c032022-03-16 23:44:11 -070016 selectedTeamInMatch:
17 TeamInMatch = {teamNumber: 1, matchNumber: 1, compLevel: 'qm'};
18 tab: Tab = 'MatchList';
Philipp Schrader72beced2022-03-07 05:29:52 -080019
Ravago Jones2813c032022-03-16 23:44:11 -070020 @ViewChild('block_alerts') block_alerts: ElementRef;
Philipp Schradercf915462022-03-16 23:42:22 -070021
Alex Perryd3ccac92022-03-12 13:48:04 -080022 constructor() {
23 window.addEventListener('beforeunload', (e) => {
Philipp Schradercf915462022-03-16 23:42:22 -070024 if (!this.block_alerts.nativeElement.checked) {
Ravago Jones2813c032022-03-16 23:44:11 -070025 // Based on
26 // https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#example
Philipp Schradercf915462022-03-16 23:42:22 -070027 // This combination ensures a dialog will be shown on most browsers.
28 e.preventDefault();
29 e.returnValue = '';
30 }
Alex Perryd3ccac92022-03-12 13:48:04 -080031 });
32 }
33
Philipp Schrader72beced2022-03-07 05:29:52 -080034 tabIs(tab: Tab) {
35 return this.tab == tab;
36 }
37
Ravago Jones2813c032022-03-16 23:44:11 -070038 selectTeamInMatch(teamInMatch: TeamInMatch) {
39 this.selectedTeamInMatch = teamInMatch;
40 this.switchTabTo('Entry');
41 }
42
43 switchTabToGuarded(tab: Tab) {
Alex Perry19a87962022-03-12 13:36:10 -080044 let shouldSwitch = true;
Philipp Schrader5f190012022-03-15 23:29:09 -070045 if (this.tab !== tab) {
46 if (!this.block_alerts.nativeElement.checked) {
47 shouldSwitch = window.confirm('Leave current page?');
48 }
Alex Perry19a87962022-03-12 13:36:10 -080049 }
50 if (shouldSwitch) {
Ravago Jones2813c032022-03-16 23:44:11 -070051 this.switchTabTo(tab);
Alex Perry19a87962022-03-12 13:36:10 -080052 }
Philipp Schrader72beced2022-03-07 05:29:52 -080053 }
Ravago Jones2813c032022-03-16 23:44:11 -070054
55 private switchTabTo(tab: Tab) {
56 this.tab = tab;
57 }
Alex Perryb3168082022-01-22 13:36:13 -080058}