PeerTube/client/src/app/menu/menu.component.ts

140 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-06-28 13:59:48 +02:00
import { Component, OnInit, ViewChild } from '@angular/core'
2017-12-01 09:20:19 +01:00
import { UserRight } from '../../../../shared/models/users/user-right.enum'
2018-06-04 16:21:17 +02:00
import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
2017-12-01 09:20:19 +01:00
import { User } from '../shared/users/user.model'
2018-06-28 13:59:48 +02:00
import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
2018-09-04 23:14:31 +02:00
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
@Component({
selector: 'my-menu',
2017-04-21 11:06:33 +02:00
templateUrl: './menu.component.html',
2017-05-01 18:05:28 +02:00
styleUrls: [ './menu.component.scss' ]
})
export class MenuComponent implements OnInit {
2018-06-28 13:59:48 +02:00
@ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
2017-12-01 09:20:19 +01:00
user: User
isLoggedIn: boolean
userHasAdminAccess = false
2018-09-04 23:14:31 +02:00
hotkeys: Hotkey[]
private routesPerRight = {
[UserRight.MANAGE_USERS]: '/admin/users',
2017-11-16 17:16:42 +01:00
[UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
[UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
[UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
}
private theme = document.querySelector('body')
private previousTheme = { }
constructor (
private authService: AuthService,
private serverService: ServerService,
2018-09-04 23:14:31 +02:00
private redirectService: RedirectService,
private hotkeysService: HotkeysService
) {}
ngOnInit () {
this.isLoggedIn = this.authService.isLoggedIn()
2017-12-01 09:20:19 +01: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 09:20:19 +01:00
this.user = this.authService.getUser()
this.computeIsUserHasAdminAccess()
console.log('Logged in.')
} else if (status === AuthStatus.LoggedOut) {
this.isLoggedIn = false
2017-12-01 09:20:19 +01:00
this.user = undefined
this.computeIsUserHasAdminAccess()
console.log('Logged out.')
} else {
console.error('Unknown auth status: ' + status)
}
}
)
// initialise the alternative theme with dark theme colors
this.previousTheme['mainBackgroundColor'] = '#111111'
this.previousTheme['mainForegroundColor'] = '#fff'
this.previousTheme['submenuColor'] = 'rgb(32,32,32)'
this.previousTheme['inputColor'] = 'gray'
this.previousTheme['inputPlaceholderColor'] = '#fff'
2018-09-04 23:14:31 +02:00
this.hotkeys = [
new Hotkey('T', (event: KeyboardEvent): boolean => {
this.toggleDarkTheme()
return false
}, undefined, 'Toggle Dark theme')
]
this.hotkeysService.add(this.hotkeys)
}
isRegistrationAllowed () {
return this.serverService.getConfig().signup.allowed &&
this.serverService.getConfig().signup.allowedForCurrentIP
2017-04-10 20:29:33 +02:00
}
getFirstAdminRightAvailable () {
const user = this.authService.getUser()
if (!user) return undefined
const adminRights = [
UserRight.MANAGE_USERS,
2017-11-16 17:16:42 +01:00
UserRight.MANAGE_SERVER_FOLLOW,
UserRight.MANAGE_VIDEO_ABUSES,
UserRight.MANAGE_VIDEO_BLACKLIST
]
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 09:20:19 +01:00
logout (event: Event) {
event.preventDefault()
this.authService.logout()
// Redirect to home page
2018-06-04 16:21:17 +02:00
this.redirectService.redirectToHomepage()
}
2018-06-28 13:59:48 +02:00
openLanguageChooser () {
this.languageChooserModal.show()
}
toggleDarkTheme () {
// switch properties
this.switchProperty('mainBackgroundColor')
this.switchProperty('mainForegroundColor')
this.switchProperty('submenuColor')
this.switchProperty('inputColor')
this.switchProperty('inputPlaceholderColor')
}
private switchProperty (property, newValue?) {
const propertyOldvalue = window.getComputedStyle(this.theme).getPropertyValue('--' + property)
this.theme.style.setProperty('--' + property, (newValue) ? newValue : this.previousTheme[property])
this.previousTheme[property] = propertyOldvalue
}
private computeIsUserHasAdminAccess () {
const right = this.getFirstAdminRightAvailable()
this.userHasAdminAccess = right !== undefined
}
}