1 import { ViewportScroller } from '@angular/common'
2 import { HotkeysService } from 'angular2-hotkeys'
3 import * as debug from 'debug'
4 import { switchMap } from 'rxjs/operators'
5 import { Component, OnInit, ViewChild } from '@angular/core'
6 import { Router } from '@angular/router'
7 import { scrollToTop } from '@app/helpers'
8 import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
9 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
10 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
11 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
12 import { NgbDropdown, NgbDropdownConfig } from '@ng-bootstrap/ng-bootstrap'
14 const logger = debug('peertube:menu:MenuComponent')
18 templateUrl: './menu.component.html',
19 styleUrls: ['./menu.component.scss']
21 export class MenuComponent implements OnInit {
22 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
23 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
24 @ViewChild('dropdown') dropdown: NgbDropdown
29 userHasAdminAccess = false
32 videoLanguages: string[] = []
35 currentInterfaceLanguage: string
37 private languages: VideoConstant<string>[] = []
38 private serverConfig: ServerConfig
39 private routesPerRight: { [role in UserRight]?: string } = {
40 [UserRight.MANAGE_USERS]: '/admin/users',
41 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
42 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
43 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
44 [UserRight.MANAGE_JOBS]: '/admin/jobs',
45 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
49 private viewportScroller: ViewportScroller,
50 private authService: AuthService,
51 private userService: UserService,
52 private serverService: ServerService,
53 private redirectService: RedirectService,
54 private hotkeysService: HotkeysService,
55 private screenService: ScreenService,
56 private menuService: MenuService,
57 private dropdownConfig: NgbDropdownConfig,
58 private router: Router
60 this.dropdownConfig.container = 'body'
63 get isInMobileView () {
64 return this.screenService.isInMobileView()
67 get dropdownContainer () {
68 if (this.isInMobileView) {
71 return this.dropdownConfig.container
76 return this.languageChooserModal.getCurrentLanguage()
80 return this.serverConfig.instance.name
84 this.serverConfig = this.serverService.getTmpConfig()
85 this.serverService.getConfig()
86 .subscribe(config => this.serverConfig = config)
88 this.isLoggedIn = this.authService.isLoggedIn()
89 if (this.isLoggedIn === true) {
90 this.user = this.authService.getUser()
92 this.computeNSFWPolicy()
93 this.computeVideosLink()
96 this.computeAdminAccess()
98 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
100 this.authService.loginChangedSource.subscribe(
102 if (status === AuthStatus.LoggedIn) {
103 this.isLoggedIn = true
104 this.user = this.authService.getUser()
106 this.computeAdminAccess()
107 this.computeVideosLink()
110 } else if (status === AuthStatus.LoggedOut) {
111 this.isLoggedIn = false
112 this.user = undefined
114 this.computeAdminAccess()
116 logger('Logged out.')
118 console.error('Unknown auth status: ' + status)
123 this.hotkeysService.cheatSheetToggle
124 .subscribe(isOpen => this.helpVisible = isOpen)
126 this.serverService.getVideoLanguages()
127 .subscribe(languages => {
128 this.languages = languages
130 this.authService.userInformationLoaded
131 .subscribe(() => this.buildUserLanguages())
135 isRegistrationAllowed () {
136 return this.serverConfig.signup.allowed &&
137 this.serverConfig.signup.allowedForCurrentIP
140 getFirstAdminRightAvailable () {
141 const user = this.authService.getUser()
142 if (!user) return undefined
144 const adminRights = [
145 UserRight.MANAGE_USERS,
146 UserRight.MANAGE_SERVER_FOLLOW,
147 UserRight.MANAGE_ABUSES,
148 UserRight.MANAGE_VIDEO_BLACKLIST,
149 UserRight.MANAGE_JOBS,
150 UserRight.MANAGE_CONFIGURATION
153 for (const adminRight of adminRights) {
154 if (user.hasRight(adminRight)) {
162 getFirstAdminRouteAvailable () {
163 const right = this.getFirstAdminRightAvailable()
165 return this.routesPerRight[right]
168 logout (event: Event) {
169 event.preventDefault()
171 this.authService.logout()
172 // Redirect to home page
173 this.redirectService.redirectToHomepage()
176 openLanguageChooser () {
177 this.languageChooserModal.show()
180 openHotkeysCheatSheet () {
181 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
184 openQuickSettings () {
185 this.quickSettingsModal.show()
189 if (!this.user) return
190 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
192 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
193 .subscribe(() => this.authService.refreshUserInformation())
196 langForLocale (localeId: string) {
197 if (localeId === '_unknown') return $localize`Unknown`
199 return this.languages.find(lang => lang.id === localeId).label
202 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
203 const linkURL = link.getAttribute('href')
204 const linkHash = link.getAttribute('fragment')
206 // On same url without fragment restore top scroll position
207 if (!linkHash && this.router.url.includes(linkURL)) {
208 scrollToTop('smooth')
211 // On same url with fragment restore anchor scroll position
212 if (linkHash && this.router.url === linkURL) {
213 this.viewportScroller.scrollToAnchor(linkHash)
216 if (this.screenService.isInSmallView()) {
217 this.menuService.toggleMenu()
221 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
222 onMenuScrollEvent () {
223 document.querySelector('menu').scrollTo(0, 0)
226 onDropdownOpenChange (opened: boolean) {
227 if (this.screenService.isInMobileView()) return
229 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
230 const onWindowScroll = () => {
231 this.dropdown?.close()
232 window.removeEventListener('scroll', onWindowScroll)
236 window.addEventListener('scroll', onWindowScroll)
237 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
238 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
240 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
244 private buildUserLanguages () {
246 this.videoLanguages = []
250 if (!this.user.videoLanguages) {
251 this.videoLanguages = [$localize`any language`]
255 this.videoLanguages = this.user.videoLanguages
256 .map(locale => this.langForLocale(locale))
257 .map(value => value === undefined ? '?' : value)
260 private computeAdminAccess () {
261 const right = this.getFirstAdminRightAvailable()
263 this.userHasAdminAccess = right !== undefined
266 private computeVideosLink () {
267 this.authService.userInformationLoaded
269 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
271 if (res === true) logger('User can see videos link.')
272 else logger('User cannot see videos link.')
276 private computeNSFWPolicy () {
278 this.nsfwPolicy = null
282 switch (this.user.nsfwPolicy) {
284 this.nsfwPolicy = $localize`hide`
288 this.nsfwPolicy = $localize`blur`
292 this.nsfwPolicy = $localize`display`