navbar.component.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Observable } from 'rxjs';
  2. import { Component, OnInit, ElementRef } from '@angular/core';
  3. import { ROUTES } from '../sidebar/sidebar.component';
  4. import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
  5. import { Router, ActivatedRoute } from '@angular/router';
  6. //import { AuthenticationService } from 'src/app/services/authentication.service';
  7. import { AdminLayoutRoutes } from 'src/app/layouts/admin/admin.routing';
  8. import { AuthService } from '../../../services/auth2.service';
  9. import Swal from 'sweetalert2';
  10. @Component({
  11. selector: 'app-navbar',
  12. templateUrl: './navbar.component.html',
  13. styleUrls: ['./navbar.component.scss']
  14. })
  15. export class NavbarComponent implements OnInit {
  16. private listTitles: any[];
  17. private leTitles: any[];
  18. location: Location;
  19. mobile_menu_visible: any = 0;
  20. private toggleButton: any;
  21. private sidebarVisible: boolean;
  22. private sidebarMini: boolean;
  23. constructor(location: Location, private element: ElementRef, private router: Router, private auth: AuthService, private route: ActivatedRoute) {
  24. this.location = location;
  25. this.sidebarVisible = false;
  26. this.sidebarMini = false;
  27. }
  28. ngOnInit(){
  29. this.listTitles = ROUTES.filter(listTitle => listTitle);
  30. this.leTitles = AdminLayoutRoutes.filter(listTitle => listTitle);
  31. const navbar: HTMLElement = this.element.nativeElement;
  32. this.toggleButton = navbar.getElementsByClassName('navbar-toggler')[0];
  33. this.router.events.subscribe((event) => {
  34. this.sidebarClose();
  35. var $layer: any = document.getElementsByClassName('close-layer')[0];
  36. if ($layer) {
  37. $layer.remove();
  38. this.mobile_menu_visible = 0;
  39. }
  40. });
  41. }
  42. sidebarMiniOn() {
  43. const toggleButton = this.toggleButton;
  44. const body = document.getElementsByTagName('body')[0];
  45. setTimeout(function(){
  46. toggleButton.classList.add('sidebar-mini');
  47. }, 500);
  48. body.classList.add('sidebar-mini');
  49. this.sidebarMini = true;
  50. };
  51. sidebarMiniOff() {
  52. const body = document.getElementsByTagName('body')[0];
  53. //this.toggleButton.classList.remove('');
  54. this.sidebarMini = false;
  55. body.classList.remove('sidebar-mini');
  56. };
  57. menuToggle(){
  58. const body = document.getElementsByTagName('body')[0];
  59. //body.classList.add('sidebar-mini');
  60. if (this.sidebarMini === false) {
  61. this.sidebarMiniOn();
  62. } else {
  63. this.sidebarMiniOff();
  64. }
  65. }
  66. sidebarOpen() {
  67. const toggleButton = this.toggleButton;
  68. const body = document.getElementsByTagName('body')[0];
  69. setTimeout(function(){
  70. toggleButton.classList.add('toggled');
  71. }, 500);
  72. body.classList.add('nav-open');
  73. this.sidebarVisible = true;
  74. };
  75. sidebarClose() {
  76. const body = document.getElementsByTagName('body')[0];
  77. this.toggleButton.classList.remove('toggled');
  78. this.sidebarVisible = false;
  79. body.classList.remove('nav-open');
  80. };
  81. sidebarToggle() {
  82. // const toggleButton = this.toggleButton;
  83. // const body = document.getElementsByTagName('body')[0];
  84. var $toggle = document.getElementsByClassName('navbar-toggler')[0];
  85. if (this.sidebarVisible === false) {
  86. this.sidebarOpen();
  87. } else {
  88. this.sidebarClose();
  89. }
  90. const body = document.getElementsByTagName('body')[0];
  91. if (this.mobile_menu_visible == 1) {
  92. // $('html').removeClass('nav-open');
  93. body.classList.remove('nav-open');
  94. if ($layer) {
  95. $layer.remove();
  96. }
  97. setTimeout(function() {
  98. $toggle.classList.remove('toggled');
  99. }, 400);
  100. this.mobile_menu_visible = 0;
  101. } else {
  102. setTimeout(function() {
  103. $toggle.classList.add('toggled');
  104. }, 430);
  105. var $layer = document.createElement('div');
  106. $layer.setAttribute('class', 'close-layer');
  107. if (body.querySelectorAll('.main-panel')) {
  108. document.getElementsByClassName('main-panel')[0].appendChild($layer);
  109. }else if (body.classList.contains('off-canvas-sidebar')) {
  110. document.getElementsByClassName('wrapper-full-page')[0].appendChild($layer);
  111. }
  112. setTimeout(function() {
  113. $layer.classList.add('visible');
  114. }, 100);
  115. $layer.onclick = function() { //asign a function
  116. body.classList.remove('nav-open');
  117. this.mobile_menu_visible = 0;
  118. $layer.classList.remove('visible');
  119. setTimeout(function() {
  120. $layer.remove();
  121. $toggle.classList.remove('toggled');
  122. }, 400);
  123. }.bind(this);
  124. body.classList.add('nav-open');
  125. this.mobile_menu_visible = 1;
  126. }
  127. };
  128. logout() {
  129. Swal.fire({
  130. allowOutsideClick: false,
  131. type: 'info',
  132. text: 'Espere por favor...'
  133. });
  134. Swal.showLoading();
  135. this.auth.logout();
  136. //this.router.navigateByUrl("login");
  137. }
  138. getTitle(){
  139. var titlee = this.location.prepareExternalUrl(this.location.path());
  140. var component_local = this.router.url;
  141. var main_title:string;
  142. if(titlee.charAt(0) === '#'){
  143. titlee = titlee.slice( 1 );
  144. }
  145. var item=0
  146. while( item < this.leTitles.length){
  147. if("/"+this.leTitles[item].path === titlee){
  148. main_title = this.leTitles[item].data['title'];
  149. break;
  150. }
  151. item++;
  152. }
  153. if (main_title === undefined) {
  154. return 'Dashboard';
  155. }
  156. else {
  157. return main_title;
  158. }
  159. }
  160. }