1
0
Fork 0
peertube/client/src/app/menu/menu.component.ts

125 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-06-28 07:59:48 -04:00
import { Component, OnInit, ViewChild } from '@angular/core'
2017-12-01 03:20:19 -05:00
import { UserRight } from '../../../../shared/models/users/user-right.enum'
2018-09-06 06:00:53 -04:00
import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
2017-12-01 03:20:19 -05:00
import { User } from '../shared/users/user.model'
2018-06-28 07:59:48 -04:00
import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
2018-09-26 08:23:10 -04:00
import { HotkeysService } from 'angular2-hotkeys'
@Component({
selector: 'my-menu',
2017-04-21 05:06:33 -04:00
templateUrl: './menu.component.html',
2017-05-01 12:05:28 -04:00
styleUrls: [ './menu.component.scss' ]
})
export class MenuComponent implements OnInit {
2018-06-28 07:59:48 -04:00
@ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
2017-12-01 03:20:19 -05:00
user: User
isLoggedIn: boolean
userHasAdminAccess = false
2018-09-26 08:23:10 -04:00
helpVisible = false
private routesPerRight = {
[UserRight.MANAGE_USERS]: '/admin/users',
2017-11-16 11:16:42 -05:00
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
2018-09-19 03:53:49 -04:00
[UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
[UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist',
[UserRight.MANAGE_JOBS]: '/admin/jobs',
[UserRight.MANAGE_CONFIGURATION]: '/admin/config'
}
constructor (
private authService: AuthService,
private serverService: ServerService,
2018-09-04 17:14:31 -04:00
private redirectService: RedirectService,
2018-09-26 08:23:10 -04:00
private themeService: ThemeService,
private hotkeysService: HotkeysService
) {}
ngOnInit () {
this.isLoggedIn = this.authService.isLoggedIn()
2017-12-01 03:20:19 -05:00
if (this.isLoggedIn === true) this.user = this.authService.getUser()
this.computeIsUserHasAdminAccess()
this.authService.loginChangedSource.subscribe(
status => {
if (status === AuthStatus.LoggedIn) {
this.isLoggedIn = true
2017-12-01 03:20:19 -05:00
this.user = this.authService.getUser()
this.computeIsUserHasAdminAccess()
console.log('Logged in.')
} else if (status === AuthStatus.LoggedOut) {
this.isLoggedIn = false
2017-12-01 03:20:19 -05:00
this.user = undefined
this.computeIsUserHasAdminAccess()
console.log('Logged out.')
} else {
console.error('Unknown auth status: ' + status)
}
}
)
2018-09-26 08:23:10 -04:00
this.hotkeysService.cheatSheetToggle.subscribe(isOpen => {
this.helpVisible = isOpen
})
}
isRegistrationAllowed () {
return this.serverService.getConfig().signup.allowed &&
this.serverService.getConfig().signup.allowedForCurrentIP
2017-04-10 14:29:33 -04:00
}
getFirstAdminRightAvailable () {
const user = this.authService.getUser()
if (!user) return undefined
const adminRights = [
UserRight.MANAGE_USERS,
2017-11-16 11:16:42 -05:00
UserRight.MANAGE_SERVER_FOLLOW,
UserRight.MANAGE_VIDEO_ABUSES,
2018-09-19 03:53:49 -04:00
UserRight.MANAGE_VIDEO_BLACKLIST,
UserRight.MANAGE_JOBS,
UserRight.MANAGE_CONFIGURATION
]
for (const adminRight of adminRights) {
if (user.hasRight(adminRight)) {
return adminRight
}
}
return undefined
}
getFirstAdminRouteAvailable () {
const right = this.getFirstAdminRightAvailable()
return this.routesPerRight[right]
}
2017-12-01 03:20:19 -05:00
logout (event: Event) {
event.preventDefault()
this.authService.logout()
// Redirect to home page
2018-06-04 10:21:17 -04:00
this.redirectService.redirectToHomepage()
}
2018-06-28 07:59:48 -04:00
openLanguageChooser () {
this.languageChooserModal.show()
}
2018-09-26 08:23:10 -04:00
openHotkeysCheatSheet () {
this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
}
toggleDarkTheme () {
2018-09-06 06:00:53 -04:00
this.themeService.toggleDarkTheme()
}
private computeIsUserHasAdminAccess () {
const right = this.getFirstAdminRightAvailable()
this.userHasAdminAccess = right !== undefined
}
}