]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { UserRight } from '../../../../shared/models/users/user-right.enum'
3 import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
4 import { User } from '../shared/users/user.model'
5 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
6 import { HotkeysService } from 'angular2-hotkeys'
7 import { ServerConfig } from '@shared/models'
8
9 @Component({
10 selector: 'my-menu',
11 templateUrl: './menu.component.html',
12 styleUrls: [ './menu.component.scss' ]
13 })
14 export class MenuComponent implements OnInit {
15 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
16
17 user: User
18 isLoggedIn: boolean
19 userHasAdminAccess = false
20 helpVisible = false
21
22 private serverConfig: ServerConfig
23 private routesPerRight: { [ role in UserRight ]?: string } = {
24 [UserRight.MANAGE_USERS]: '/admin/users',
25 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
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'
30 }
31
32 constructor (
33 private authService: AuthService,
34 private serverService: ServerService,
35 private redirectService: RedirectService,
36 private themeService: ThemeService,
37 private hotkeysService: HotkeysService
38 ) {}
39
40 ngOnInit () {
41 this.serverConfig = this.serverService.getTmpConfig()
42 this.serverService.getConfig()
43 .subscribe(config => this.serverConfig = config)
44
45 this.isLoggedIn = this.authService.isLoggedIn()
46 if (this.isLoggedIn === true) this.user = this.authService.getUser()
47 this.computeIsUserHasAdminAccess()
48
49 this.authService.loginChangedSource.subscribe(
50 status => {
51 if (status === AuthStatus.LoggedIn) {
52 this.isLoggedIn = true
53 this.user = this.authService.getUser()
54 this.computeIsUserHasAdminAccess()
55 console.log('Logged in.')
56 } else if (status === AuthStatus.LoggedOut) {
57 this.isLoggedIn = false
58 this.user = undefined
59 this.computeIsUserHasAdminAccess()
60 console.log('Logged out.')
61 } else {
62 console.error('Unknown auth status: ' + status)
63 }
64 }
65 )
66
67 this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
68 this.helpVisible = isOpen
69 })
70 }
71
72 isRegistrationAllowed () {
73 return this.serverConfig.signup.allowed &&
74 this.serverConfig.signup.allowedForCurrentIP
75 }
76
77 getFirstAdminRightAvailable () {
78 const user = this.authService.getUser()
79 if (!user) return undefined
80
81 const adminRights = [
82 UserRight.MANAGE_USERS,
83 UserRight.MANAGE_SERVER_FOLLOW,
84 UserRight.MANAGE_VIDEO_ABUSES,
85 UserRight.MANAGE_VIDEO_BLACKLIST,
86 UserRight.MANAGE_JOBS,
87 UserRight.MANAGE_CONFIGURATION
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]
103 }
104
105 logout (event: Event) {
106 event.preventDefault()
107
108 this.authService.logout()
109 // Redirect to home page
110 this.redirectService.redirectToHomepage()
111 }
112
113 openLanguageChooser () {
114 this.languageChooserModal.show()
115 }
116
117 openHotkeysCheatSheet () {
118 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
119 }
120
121 private computeIsUserHasAdminAccess () {
122 const right = this.getFirstAdminRightAvailable()
123
124 this.userHasAdminAccess = right !== undefined
125 }
126 }