]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/menu.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
CommitLineData
8afc19a6 1import { Component, OnInit, ViewChild } from '@angular/core'
b33f657c 2import { UserRight } from '../../../../shared/models/users/user-right.enum'
1a00c561 3import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
b33f657c 4import { User } from '../shared/users/user.model'
8afc19a6 5import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
4a216666 6import { HotkeysService } from 'angular2-hotkeys'
ba430d75 7import { ServerConfig } from '@shared/models'
602eb142
C
8
9@Component({
10 selector: 'my-menu',
383bfc83 11 templateUrl: './menu.component.html',
3eeeb87f 12 styleUrls: [ './menu.component.scss' ]
602eb142
C
13})
14export class MenuComponent implements OnInit {
f36da21e 15 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
8afc19a6 16
b33f657c 17 user: User
df98563e 18 isLoggedIn: boolean
954605a8 19 userHasAdminAccess = false
4a216666 20 helpVisible = false
954605a8 21
ba430d75 22 private serverConfig: ServerConfig
c199c427 23 private routesPerRight: { [ role in UserRight ]?: string } = {
954605a8 24 [UserRight.MANAGE_USERS]: '/admin/users',
4610bc5b 25 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
ad76628b
C
26 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
27 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
28 [UserRight.MANAGE_JOBS]: '/admin/jobs',
29 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
954605a8 30 }
602eb142
C
31
32 constructor (
33 private authService: AuthService,
db7af09b 34 private serverService: ServerService,
8c985ef5 35 private redirectService: RedirectService,
4a216666 36 private themeService: ThemeService,
cc680494 37 private hotkeysService: HotkeysService
602eb142
C
38 ) {}
39
df98563e 40 ngOnInit () {
ba430d75
C
41 this.serverConfig = this.serverService.getTmpConfig()
42 this.serverService.getConfig()
43 .subscribe(config => this.serverConfig = config)
44
df98563e 45 this.isLoggedIn = this.authService.isLoggedIn()
b33f657c 46 if (this.isLoggedIn === true) this.user = this.authService.getUser()
954605a8 47 this.computeIsUserHasAdminAccess()
602eb142
C
48
49 this.authService.loginChangedSource.subscribe(
50 status => {
51 if (status === AuthStatus.LoggedIn) {
df98563e 52 this.isLoggedIn = true
b33f657c 53 this.user = this.authService.getUser()
954605a8 54 this.computeIsUserHasAdminAccess()
df98563e 55 console.log('Logged in.')
602eb142 56 } else if (status === AuthStatus.LoggedOut) {
df98563e 57 this.isLoggedIn = false
b33f657c 58 this.user = undefined
954605a8 59 this.computeIsUserHasAdminAccess()
df98563e 60 console.log('Logged out.')
602eb142 61 } else {
df98563e 62 console.error('Unknown auth status: ' + status)
602eb142
C
63 }
64 }
df98563e 65 )
4a216666
RK
66
67 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
68 this.helpVisible = isOpen
69 })
602eb142
C
70 }
71
291e8d3e 72 isRegistrationAllowed () {
ba430d75
C
73 return this.serverConfig.signup.allowed &&
74 this.serverConfig.signup.allowedForCurrentIP
a184c71b
C
75 }
76
954605a8
C
77 getFirstAdminRightAvailable () {
78 const user = this.authService.getUser()
79 if (!user) return undefined
80
81 const adminRights = [
82 UserRight.MANAGE_USERS,
4610bc5b 83 UserRight.MANAGE_SERVER_FOLLOW,
954605a8 84 UserRight.MANAGE_VIDEO_ABUSES,
ad76628b
C
85 UserRight.MANAGE_VIDEO_BLACKLIST,
86 UserRight.MANAGE_JOBS,
87 UserRight.MANAGE_CONFIGURATION
954605a8
C
88 ]
89
90 for (const adminRight of adminRights) {
91 if (user.hasRight(adminRight)) {
92 return adminRight
93 }
94 }
95
96 return undefined
97 }
98
99 getFirstAdminRouteAvailable () {
100 const right = this.getFirstAdminRightAvailable()
101
102 return this.routesPerRight[right]
602eb142
C
103 }
104
b33f657c
C
105 logout (event: Event) {
106 event.preventDefault()
107
df98563e 108 this.authService.logout()
602eb142 109 // Redirect to home page
b1d40cff 110 this.redirectService.redirectToHomepage()
602eb142 111 }
954605a8 112
8afc19a6
C
113 openLanguageChooser () {
114 this.languageChooserModal.show()
115 }
116
4a216666
RK
117 openHotkeysCheatSheet () {
118 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
119 }
120
954605a8
C
121 private computeIsUserHasAdminAccess () {
122 const right = this.getFirstAdminRightAvailable()
123
124 this.userHasAdminAccess = right !== undefined
125 }
602eb142 126}