menu.component.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Injectable, Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
  2. import { Router, RouterLink, RouterLinkActive } from '@angular/router';
  3. import { DEFAULT_INTERRUPTSOURCES, Idle } from '@ng-idle/core';
  4. import { TooltipModule } from 'ngx-bootstrap/tooltip';
  5. import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
  6. import { faHome, faRightToBracket, faClock, faEye, faUser, faRightFromBracket, faComments, faSave, faVoteYea, faTrophy, faUsers, faLayerGroup, faDisplay, faSlidersH } from '@fortawesome/free-solid-svg-icons';
  7. import { AccountService } from '../../services/account.service';
  8. @Component({ selector: 'app-menu', imports: [FontAwesomeModule, TooltipModule, RouterLink, RouterLinkActive], templateUrl: './menu.component.html', styleUrl: './menu.component.css' })
  9. @Injectable({ providedIn: 'root' })
  10. export class MenuComponent implements OnInit
  11. {
  12. faHome = faHome; faRightToBracket = faRightToBracket; faClock = faClock; faEye = faEye; faUser = faUser; faRightFromBracket = faRightFromBracket; faComments = faComments;
  13. faSave = faSave; faTrophy = faTrophy; faVoteYea = faVoteYea; faUsers = faUsers; faLayerGroup = faLayerGroup; faDisplay = faDisplay; faSlidersH = faSlidersH;
  14. logged: boolean = false;
  15. role: string = "";
  16. private idleState: string = 'NOT_STARTED';
  17. private countdown: number = 0;
  18. private timedOut: boolean = false;
  19. @ViewChild('signouticon', {static: false}) signOutIcon!: ElementRef;
  20. constructor(private idle: Idle, private router: Router, private accountService: AccountService, private el: ElementRef, private renderer: Renderer2)
  21. {
  22. this.idle.setIdle(900);
  23. this.idle.setTimeout(15);
  24. this.idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
  25. this.idle.onIdleStart.subscribe(() => { this.idleState = 'IDLE'; this.showPendingLogout(); });
  26. this.idle.onIdleEnd.subscribe(() => { this.resetIdle(); });
  27. this.idle.onTimeout.subscribe(() => { if (this.logged) { this.idleState = 'TIMED_OUT'; this.timedOut = true; this.deconnexion(); } else { this.resetIdle(); } });
  28. this.idle.onTimeoutWarning.subscribe((countdown) => { this.countdown = countdown; });
  29. }
  30. ngOnInit()
  31. {
  32. this.logged = this.accountService.isLogged();
  33. this.role = this.accountService.getRole();
  34. this.idle.setIdle(Math.max(15, this.accountService.getDelaiAvantDeconnexion()) * 60);
  35. this.resetIdle();
  36. this.idle.watch();
  37. }
  38. deconnexion() { this.accountService.signOut(); this.logged = false; if ((this.router.url === '/') || (this.router.url === '/home')) { window.location.reload(); } else { this.router.navigate(['/']); } }
  39. getRandomInteger(min: number, max: number) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; }
  40. resetIdle() { this.hidePendingLogout(); this.idleState = "NOT_IDLE"; this.timedOut = false; this.countdown = 0; }
  41. showPendingLogout() { if (this.logged) { if (this.signOutIcon) { this.renderer.addClass(this.signOutIcon.nativeElement, 'fa-beat-fade'); this.renderer.addClass(this.signOutIcon.nativeElement, 'text-danger'); } } }
  42. hidePendingLogout() { if (this.logged) { if (this.signOutIcon) { this.renderer.removeClass(this.signOutIcon.nativeElement, 'fa-beat-fade'); this.renderer.removeClass(this.signOutIcon.nativeElement, 'text-danger'); } } }
  43. }