1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
4 import { User } from '../shared/users/user.model'
5 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
10 templateUrl: './menu.component.html',
11 styleUrls: [ './menu.component.scss' ]
13 export class MenuComponent implements OnInit {
14 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
18 userHasAdminAccess = false
21 private routesPerRight = {
22 [UserRight.MANAGE_USERS]: '/admin/users',
23 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
24 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
25 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
27 private theme = document.querySelector('body')
28 private previousTheme = { }
31 private authService: AuthService,
32 private serverService: ServerService,
33 private redirectService: RedirectService,
34 private hotkeysService: HotkeysService
38 this.isLoggedIn = this.authService.isLoggedIn()
39 if (this.isLoggedIn === true) this.user = this.authService.getUser()
40 this.computeIsUserHasAdminAccess()
42 this.authService.loginChangedSource.subscribe(
44 if (status === AuthStatus.LoggedIn) {
45 this.isLoggedIn = true
46 this.user = this.authService.getUser()
47 this.computeIsUserHasAdminAccess()
48 console.log('Logged in.')
49 } else if (status === AuthStatus.LoggedOut) {
50 this.isLoggedIn = false
52 this.computeIsUserHasAdminAccess()
53 console.log('Logged out.')
55 console.error('Unknown auth status: ' + status)
60 // initialise the alternative theme with dark theme colors
61 this.previousTheme['mainBackgroundColor'] = '#111111'
62 this.previousTheme['mainForegroundColor'] = '#fff'
63 this.previousTheme['submenuColor'] = 'rgb(32,32,32)'
64 this.previousTheme['inputColor'] = 'gray'
65 this.previousTheme['inputPlaceholderColor'] = '#fff'
68 new Hotkey('T', (event: KeyboardEvent): boolean => {
69 this.toggleDarkTheme()
71 }, undefined, 'Toggle Dark theme')
73 this.hotkeysService.add(this.hotkeys)
76 isRegistrationAllowed () {
77 return this.serverService.getConfig().signup.allowed &&
78 this.serverService.getConfig().signup.allowedForCurrentIP
81 getFirstAdminRightAvailable () {
82 const user = this.authService.getUser()
83 if (!user) return undefined
86 UserRight.MANAGE_USERS,
87 UserRight.MANAGE_SERVER_FOLLOW,
88 UserRight.MANAGE_VIDEO_ABUSES,
89 UserRight.MANAGE_VIDEO_BLACKLIST
92 for (const adminRight of adminRights) {
93 if (user.hasRight(adminRight)) {
101 getFirstAdminRouteAvailable () {
102 const right = this.getFirstAdminRightAvailable()
104 return this.routesPerRight[right]
107 logout (event: Event) {
108 event.preventDefault()
110 this.authService.logout()
111 // Redirect to home page
112 this.redirectService.redirectToHomepage()
115 openLanguageChooser () {
116 this.languageChooserModal.show()
121 this.switchProperty('mainBackgroundColor')
122 this.switchProperty('mainForegroundColor')
123 this.switchProperty('submenuColor')
124 this.switchProperty('inputColor')
125 this.switchProperty('inputPlaceholderColor')
128 private switchProperty (property, newValue?) {
129 const propertyOldvalue = window.getComputedStyle(this.theme).getPropertyValue('--' + property)
130 this.theme.style.setProperty('--' + property, (newValue) ? newValue : this.previousTheme[property])
131 this.previousTheme[property] = propertyOldvalue
134 private computeIsUserHasAdminAccess () {
135 const right = this.getFirstAdminRightAvailable()
137 this.userHasAdminAccess = right !== undefined