You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

header.component.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { Component, OnDestroy, OnInit } from '@angular/core';
  2. import { NbSidebarService, NbThemeService, NbMenuItem, NbDialogService } from '@nebular/theme';
  3. import { LayoutService } from '../../../@core/utils';
  4. import { map, takeUntil } from 'rxjs/operators';
  5. import { Subject } from 'rxjs';
  6. import { User } from '../../../../../../backend/Types/Types';
  7. import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
  8. import { ChatComponent } from './chat.component';
  9. import { ShoutMessage } from '../../../../../../backend/Components/Shoutbox/Interface';
  10. import { IApiService } from '../../../frontcraft/services/ApiService';
  11. @Component({
  12. selector: 'ngx-header',
  13. styleUrls: ['./header.component.scss'],
  14. templateUrl: './header.component.html',
  15. })
  16. export class HeaderComponent implements OnInit, OnDestroy {
  17. private destroy$: Subject<void> = new Subject<void>();
  18. user: User;
  19. title = "Loading ... ";
  20. themes = [
  21. {
  22. value: 'default',
  23. name: 'Light',
  24. },
  25. {
  26. value: 'dark',
  27. name: 'Dark',
  28. },
  29. {
  30. value: 'cosmic',
  31. name: 'Cosmic',
  32. },
  33. {
  34. value: 'corporate',
  35. name: 'Corporate',
  36. },
  37. ];
  38. currentTheme = 'default';
  39. userMenu: NbMenuItem[] = [];
  40. sendMessage: any = console.log
  41. chatwindow: ChatComponent
  42. chatlog: ShoutMessage[] = []
  43. newmessage = false
  44. lastmessage = "asdasd"
  45. modifyPermissions
  46. constructor(
  47. private sidebarService: NbSidebarService,
  48. private router: Router,
  49. private themeService: NbThemeService,
  50. private layoutService: LayoutService,
  51. private api: IApiService,
  52. private dialogService: NbDialogService,
  53. private route: ActivatedRoute
  54. ) { }
  55. private setUserMenu = (redirect = this.router.url) => {
  56. if (this.user.username === 'Guest' && this.user.rank === 'Guest'){
  57. this.userMenu = [
  58. { title: 'Login', link: '/auth/login', queryParams: { redirect: redirect } },
  59. { title: 'Register', link: '/auth/register', queryParams: { redirect: redirect } },
  60. ]
  61. }else{
  62. this.userMenu = [
  63. { title: 'Profile', link: '/frontcraft/user/' + this.user.username },
  64. { title: 'Log out', link: '/auth/logout', queryParams: { redirect: redirect } },
  65. ]
  66. }
  67. }
  68. ngOnInit() {
  69. this.themeService.changeTheme("dark");
  70. this.currentTheme = this.themeService.currentTheme;
  71. this.modifyPermissions = this.api.get('modifyPermissions')
  72. this.user = this.api.getCurrentUser()
  73. this.setUserMenu()
  74. this.router.events.subscribe((val) => {
  75. if(val instanceof NavigationEnd) {
  76. this.setUserMenu(val.urlAfterRedirects)
  77. }
  78. })
  79. this.api.get('GuildManager').getGuildInfo().then(info => {
  80. this.title = info.name
  81. })
  82. this.api.connectShoutbox((msg) => {
  83. this.chatlog.push(msg)
  84. msg['reply'] = false
  85. if (msg.message != this.lastmessage) {
  86. this.newmessage = true
  87. msg['reply'] = true
  88. }
  89. if (this.chatwindow) this.chatwindow.messages.push(msg)
  90. }).then(sendMsg => {
  91. this.sendMessage = (msg) => {
  92. sendMsg(msg)
  93. this.lastmessage = msg.message
  94. }
  95. this.api.get('Shoutbox').getFeed().then(log => { this.chatlog = log })
  96. })
  97. this.themeService.onThemeChange()
  98. .pipe(
  99. map(({ name }) => name),
  100. takeUntil(this.destroy$),
  101. )
  102. .subscribe(themeName => this.currentTheme = themeName);
  103. }
  104. openChat() {
  105. this.newmessage = false
  106. const ref = this.dialogService.open(ChatComponent, {
  107. context: {
  108. sendMessage: this.sendMessage,
  109. }
  110. });
  111. ref.onClose.subscribe(() => {
  112. this.chatwindow = null
  113. })
  114. this.chatwindow = ref.componentRef.instance
  115. this.chatwindow.messages.push(...this.chatlog)
  116. }
  117. ngOnDestroy() {
  118. this.destroy$.next();
  119. this.destroy$.complete();
  120. }
  121. changeTheme(themeName: string) {
  122. this.themeService.changeTheme(themeName);
  123. }
  124. toggleSidebar(): boolean {
  125. this.sidebarService.toggle(true, 'menu-sidebar');
  126. this.layoutService.changeLayoutSize();
  127. return false;
  128. }
  129. logout() {
  130. this.api.logout()
  131. }
  132. navigateHome() {
  133. this.router.navigateByUrl('/')
  134. return false;
  135. }
  136. }