app.component.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Component, ElementRef, HostBinding, HostListener, OnDestroy, ViewChild } from '@angular/core';
  2. import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router } from '@angular/router';
  3. import { NgxSpinnerService } from 'ngx-spinner';
  4. import { BehaviorSubject, Observable, Subscription } from 'rxjs';
  5. import { map } from 'rxjs/operators';
  6. import { AppConfig } from './app.config';
  7. import { ThemesService } from './services/themes.service';
  8. import { ShortcutsService } from './shortcuts/shortcuts.service';
  9. import { EvtIconInfo } from './ui-components/icon/icon.component';
  10. @Component({
  11. selector: 'evt-root',
  12. templateUrl: './app.component.html',
  13. styleUrls: ['./app.component.scss'],
  14. })
  15. export class AppComponent implements OnDestroy {
  16. @ViewChild('mainSpinner') mainSpinner: ElementRef;
  17. private subscriptions: Subscription[] = [];
  18. public hasNavBar = AppConfig.evtSettings.ui.enableNavBar;
  19. public navbarOpened$ = new BehaviorSubject(this.hasNavBar && AppConfig.evtSettings.ui.initNavBarOpened);
  20. public navbarTogglerIcon$: Observable<EvtIconInfo> = this.navbarOpened$.pipe(
  21. map((opened: boolean) => opened ? { icon: 'caret-down', iconSet: 'fas' } : { icon: 'caret-up', iconSet: 'fas' }),
  22. );
  23. constructor(
  24. private router: Router,
  25. private spinner: NgxSpinnerService,
  26. private shortcutsService: ShortcutsService,
  27. private themes: ThemesService,
  28. ) {
  29. this.router.events.subscribe((event) => {
  30. switch (true) {
  31. case event instanceof NavigationStart:
  32. this.spinner.show();
  33. break;
  34. case event instanceof NavigationEnd:
  35. case event instanceof NavigationCancel:
  36. case event instanceof NavigationError:
  37. this.spinner.hide();
  38. break;
  39. default:
  40. break;
  41. }
  42. });
  43. }
  44. @HostBinding('attr.data-theme') get dataTheme() { return this.themes.getCurrentTheme().value; }
  45. toggleToolbar() {
  46. this.navbarOpened$.next(!this.navbarOpened$.getValue());
  47. window.dispatchEvent(new Event('resize')); // Needed to tell Gridster to resize
  48. }
  49. ngOnDestroy() {
  50. this.subscriptions.forEach(subscription => subscription.unsubscribe());
  51. }
  52. @HostListener('window:keyup', ['$event'])
  53. keyEvent(e: KeyboardEvent) {
  54. this.shortcutsService.handleKeyboardEvent(e);
  55. }
  56. }