app.component.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Component, HostListener, OnInit } from '@angular/core';
  2. import { Router, RouterModule, RouterOutlet, RoutesRecognized } from '@angular/router';
  3. import { MatModule } from '../dependencies/angularlib/mat.module';
  4. import { Store } from '@ngxs/store'
  5. import { Angularlib } from 'angularlib/angularlib.module';
  6. import { BaseComponent, untilDestroy } from 'angularlib/base.component';
  7. import { Subject, filter, map, repeat, takeUntil, timer } from 'rxjs';
  8. import { Title } from '@angular/platform-browser';
  9. import { LoginService } from 'angularlib/login/login.service';
  10. import { CommonModule } from '@angular/common';
  11. import { ChangeTheme } from 'angularlib/ui.state/ui.state.actions';
  12. import { ChangeLanguage } from 'angularlib/labels/label.actions';
  13. import { Label } from 'angularlib/labels/label.interface';
  14. import { ComponentService } from 'angularlib/component.service';
  15. @Component({
  16. selector: 'app-root',
  17. standalone: true,
  18. imports: [
  19. CommonModule,
  20. RouterOutlet,
  21. MatModule,
  22. RouterModule,
  23. Angularlib
  24. ],
  25. templateUrl: './app.component.html',
  26. styleUrls: [
  27. './app.component.scss'
  28. ]
  29. })
  30. export class AppComponent extends BaseComponent implements OnInit {
  31. title = 'FIS App';
  32. /**login timeout duration in milliseconds
  33. * @default 5 minutes
  34. */
  35. private duration: number = 300000;
  36. private startTimeout$ = new Subject();
  37. private stopTimeout$ = new Subject();
  38. private timeout = timer(this.duration).pipe(
  39. map(() => {if(this.loginService.user)this.loginService.logout();console.warn('session inactive timeout, logging out...');}),
  40. takeUntil(this.stopTimeout$),
  41. repeat({delay:() => this.startTimeout$})
  42. );
  43. /**current theme of application */
  44. protected theme;
  45. constructor(
  46. private router: Router,
  47. private store: Store,
  48. protected loginService: LoginService,
  49. /**Platform Browser title */
  50. protected pbTitle: Title,
  51. protected cs: ComponentService
  52. ) {
  53. super(store,cs);
  54. }
  55. @HostListener('window:mousedown')
  56. private refreshTimeout() {
  57. this.stopTimeout$.next(null);
  58. this.startTimeout$.next(null);
  59. }
  60. ngOnInit(): void {
  61. this.setDefaultTheme();
  62. this.router.events.pipe(untilDestroy(this),
  63. filter((event) => event instanceof RoutesRecognized),
  64. map((res:any) => {
  65. const data = res.state.root.firstChild.firstChild || res.state.root.firstChild;
  66. return data.data?.title;
  67. })
  68. ).subscribe(title => {
  69. if (typeof title === 'string')
  70. this.title = title;
  71. else this.title = this.getLabel(title.key,title.default);
  72. this.pbTitle.setTitle(this.title);
  73. });
  74. /**subsribe user changes and initiate timeout timer */
  75. this.loginService.user$.pipe(untilDestroy(this)).subscribe(user => {
  76. if (user) {
  77. this.timeout.pipe(untilDestroy(this),takeUntil(this.loginService.loggedOut)).subscribe();
  78. }
  79. });
  80. }
  81. /**
  82. * change application language
  83. * @param {string} language
  84. */
  85. protected changeLanguage(language: string) {
  86. this.store.dispatch(new ChangeLanguage(language));
  87. }
  88. protected setDefaultTheme() {
  89. if (localStorage.getItem('theme')) {
  90. this.theme = localStorage.getItem('theme') as string;
  91. const body = document.getElementsByTagName('body')[0];
  92. body.classList.add(this.theme);
  93. }
  94. }
  95. /**
  96. * change application theme
  97. * @param {string} theme theme name
  98. */
  99. protected changeTheme(theme: string) {
  100. const body = document.getElementsByTagName('body')[0];
  101. body.classList.remove(this.theme);
  102. this.theme = theme;
  103. body.classList.add(this.theme);
  104. localStorage.setItem('theme',this.theme);
  105. this.store.dispatch(new ChangeTheme(this.theme));
  106. }
  107. }