nav-bar.component.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {Component, ElementRef, EventEmitter, HostListener, Input, OnDestroy, OnInit, Output, Renderer2} from '@angular/core';
  2. import {AuthService} from '../../../auth/services/auth.service';
  3. import {User} from '../../../auth/models/user';
  4. import {Subscription} from 'rxjs';
  5. import {Right} from '../../api/endpoints/models/right';
  6. import {Phenomenon} from '../../api/endpoints/models/phenomenon';
  7. import {SensorsService} from '../../api/endpoints/services/sensors.service';
  8. import {InsertUnit} from '../../api/endpoints/models/insert-unit';
  9. import { InsertSensor } from '../../api/endpoints/models/insert-sensor';
  10. @Component({
  11. selector: 'app-nav-bar',
  12. templateUrl: './nav-bar.component.html',
  13. styleUrls: ['./nav-bar.component.scss']
  14. })
  15. export class NavBarComponent implements OnInit, OnDestroy {
  16. isMenuOpen = false;
  17. loggedUser: User;
  18. subscription: Subscription[] = [];
  19. showAddUserPopup = false;
  20. rights: Right[];
  21. showInsertUnitPopup = false;
  22. showDataDownloadPopup = false;
  23. phenomenons: Phenomenon[];
  24. @Output() emitNewUnit: EventEmitter<{unit: InsertUnit, sensors: InsertSensor[]}> =
  25. new EventEmitter<{unit: InsertUnit, sensors: InsertSensor[]}>();
  26. @Input() sensorTypes;
  27. constructor(
  28. private authService: AuthService,
  29. private sensorService: SensorsService,
  30. ) {
  31. }
  32. ngOnInit(): void {
  33. this.setUser();
  34. }
  35. /**
  36. * Toggle menu visibility on mobile
  37. */
  38. toggleMenu() {
  39. this.isMenuOpen = !this.isMenuOpen;
  40. const el = document.getElementById('navbarNav');
  41. const rect = el.getBoundingClientRect();
  42. console.log(rect.width); // Width including padding and borders
  43. console.log(rect.height); // Height including padding and borders
  44. console.log(rect.top); // Distance from top of viewport
  45. console.log(rect.left); // Distance from left of viewport
  46. }
  47. /**
  48. * Get user from state after logged
  49. */
  50. setUser(){
  51. this.authService.getUserState().subscribe(res => {
  52. if(res){
  53. this.loggedUser = res;
  54. }
  55. });
  56. }
  57. /**
  58. * Show insert unit popup
  59. */
  60. insertUnitPopup() {
  61. this.sensorService.getPhenomenons().subscribe(
  62. response => this.phenomenons = response
  63. );
  64. this.showInsertUnitPopup = true;
  65. }
  66. /**
  67. * Show data download popup
  68. */
  69. downloadData() {
  70. this.sensorService.getPhenomenons().subscribe(
  71. response => this.phenomenons = response
  72. );
  73. this.showDataDownloadPopup = true;
  74. }
  75. logOut(): void {
  76. this.authService.doLogout();
  77. }
  78. /**
  79. * Unsubscribe after leaving
  80. */
  81. ngOnDestroy(): void {
  82. this.subscription.forEach(subs => subs.unsubscribe());
  83. }
  84. /**
  85. * Show add user popup
  86. */
  87. addUser() {
  88. this.showAddUserPopup = true;
  89. }
  90. /**
  91. * Emit inserted unit to add it to units
  92. * @param inserted inserted unit
  93. */
  94. addUnit(inserted: any) {
  95. this.emitNewUnit.emit(inserted);
  96. }
  97. }