All files / src/app app.component.ts

2% Statements 1/50
0% Branches 0/13
0% Functions 0/18
2% Lines 1/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                              1x                                                                                                                                                                                                                          
import {
  Component, OnDestroy, OnInit
} from '@angular/core';
import { Subscription, combineLatest } from 'rxjs';
import { DomSanitizer, Title } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { CustomtextService, MainDataService } from './shared/shared.module';
import { BackendService } from './backend.service';
import { AppConfig } from './shared/classes/app.config';
 
@Component({
  selector: 'tc-root',
  templateUrl: './app.component.html'
})
 
export class AppComponent implements OnInit, OnDestroy {
  private appErrorSubscription: Subscription | null = null;
  private appTitleSubscription: Subscription | null = null;
 
  showError = false;
 
  constructor(
    public mainDataService: MainDataService,
    private backendService: BackendService,
    private customtextService: CustomtextService,
    private titleService: Title,
    private sanitizer: DomSanitizer,
    private route: ActivatedRoute
  ) { }
 
  ngOnInit(): void {
    setTimeout(() => {
      this.appErrorSubscription = this.mainDataService.appError$
        .subscribe(err => {
          Iif (err.type === 'fatal') {
            this.mainDataService.quit();
          }
          Iif (!this.disableGlobalErrorDisplay()) {
            this.showError = true;
          }
        });
 
      this.appTitleSubscription = combineLatest([this.mainDataService.appTitle$, this.mainDataService.appSubTitle$])
        .subscribe(titles => {
          if (titles[1]) {
            this.titleService.setTitle(`${titles[0]} | ${titles[1]}`);
          } else {
            this.titleService.setTitle(titles[0]);
          }
        });
 
      window.addEventListener('message', (event: MessageEvent) => {
        const msgData = event.data;
        const msgType = msgData.type;
        Iif ((msgType !== undefined) && (msgType !== null)) {
          Iif ((msgType.substr(0, 2) === 'vo')) {
            this.mainDataService.postMessage$.next(event);
          }
        }
      });
 
      this.setupFocusListeners();
      this.setupFullScreenListener();
 
      this.backendService.getSysConfig()
        .subscribe(sysConfig => {
          this.mainDataService.appConfig$ = new AppConfig(sysConfig, this.customtextService, this.sanitizer);
        });
 
      // TODO don't ask for Syschecks on start, do it on SysCheck starter. Save calls.
      this.backendService.checkIfSysCheckModeExists()
        .subscribe(doesSysCheckModeExist => {
          this.mainDataService.sysCheckAvailableForAll = !doesSysCheckModeExist;
        });
    });
  }
 
  // some modules have their own error handling
  private disableGlobalErrorDisplay(): boolean {
    const routeData = this.route.firstChild?.routeConfig?.data ?? {};
    // eslint-disable-next-line @typescript-eslint/dot-notation
    return 'disableGlobalErrorDisplay' in routeData;
  }
 
  private setupFocusListeners(): void {
    Iif (typeof document.hidden !== 'undefined') {
      document.addEventListener('visibilitychange', () => {
        this.mainDataService.appWindowHasFocus$.next(!document.hidden);
      }, false);
    }
    window.addEventListener('blur', () => {
      this.mainDataService.appWindowHasFocus$.next(document.hasFocus());
    });
    window.addEventListener('focus', () => {
      this.mainDataService.appWindowHasFocus$.next(document.hasFocus());
    });
    window.addEventListener('unload', () => {
      this.mainDataService.appWindowHasFocus$.next(!document.hidden);
    });
  }
 
  private setupFullScreenListener(): void {
    document.addEventListener(
      'fullscreenchange',
      () => {
        this.mainDataService.isFullScreen = !!document.fullscreenElement;
      },
      false
    );
  }
 
  closeErrorBox(): void {
    this.showError = false;
  }
 
  ngOnDestroy(): void {
    Iif (this.appErrorSubscription !== null) {
      this.appErrorSubscription.unsubscribe();
    }
    Iif (this.appTitleSubscription !== null) {
      this.appTitleSubscription.unsubscribe();
    }
  }
}