1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { switchMap } from 'rxjs/operators'
4 import { ViewportScroller } from '@angular/common'
5 import { Component, OnInit, ViewChild } from '@angular/core'
6 import { Router } from '@angular/router'
19 import { scrollToTop } from '@app/helpers'
20 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
21 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
22 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
23 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
24 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
26 const debugLogger = debug('peertube:menu:MenuComponent')
30 templateUrl: './menu.component.html',
31 styleUrls: [ './menu.component.scss' ]
33 export class MenuComponent implements OnInit {
34 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
35 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
36 @ViewChild('dropdown') dropdown: NgbDropdown
41 userHasAdminAccess = false
44 videoLanguages: string[] = []
47 currentInterfaceLanguage: string
49 menuSections: MenuSection[] = []
51 private languages: VideoConstant<string>[] = []
53 private htmlServerConfig: HTMLServerConfig
54 private serverConfig: ServerConfig
56 private routesPerRight: { [role in UserRight]?: string } = {
57 [UserRight.MANAGE_USERS]: '/admin/users',
58 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
59 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
60 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
61 [UserRight.MANAGE_JOBS]: '/admin/jobs',
62 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
66 private viewportScroller: ViewportScroller,
67 private authService: AuthService,
68 private userService: UserService,
69 private serverService: ServerService,
70 private redirectService: RedirectService,
71 private hotkeysService: HotkeysService,
72 private screenService: ScreenService,
73 private menuService: MenuService,
74 private modalService: PeertubeModalService,
75 private router: Router,
76 private hooks: HooksService
79 get isInMobileView () {
80 return this.screenService.isInMobileView()
83 get dropdownContainer () {
84 if (this.isInMobileView) return null
86 return 'body' as 'body'
90 return this.languageChooserModal.getCurrentLanguage()
93 get requiresApproval () {
94 return this.serverConfig.signup.requiresApproval
98 this.htmlServerConfig = this.serverService.getHTMLConfig()
99 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
101 this.isLoggedIn = this.authService.isLoggedIn()
102 this.updateUserState()
103 this.buildMenuSections()
105 this.authService.loginChangedSource.subscribe(
107 if (status === AuthStatus.LoggedIn) {
108 this.isLoggedIn = true
109 } else if (status === AuthStatus.LoggedOut) {
110 this.isLoggedIn = false
113 this.updateUserState()
114 this.buildMenuSections()
118 this.hotkeysService.cheatSheetToggle
119 .subscribe(isOpen => this.helpVisible = isOpen)
121 this.serverService.getVideoLanguages()
122 .subscribe(languages => {
123 this.languages = languages
125 this.authService.userInformationLoaded
126 .subscribe(() => this.buildUserLanguages())
129 this.serverService.getConfig()
130 .subscribe(config => this.serverConfig = config)
132 this.modalService.openQuickSettingsSubject
133 .subscribe(() => this.openQuickSettings())
136 isRegistrationAllowed () {
137 if (!this.serverConfig) return false
139 return this.serverConfig.signup.allowed &&
140 this.serverConfig.signup.allowedForCurrentIP
143 getFirstAdminRightAvailable () {
144 const user = this.authService.getUser()
145 if (!user) return undefined
147 const adminRights = [
148 UserRight.MANAGE_USERS,
149 UserRight.MANAGE_SERVER_FOLLOW,
150 UserRight.MANAGE_ABUSES,
151 UserRight.MANAGE_VIDEO_BLACKLIST,
152 UserRight.MANAGE_JOBS,
153 UserRight.MANAGE_CONFIGURATION
156 for (const adminRight of adminRights) {
157 if (user.hasRight(adminRight)) {
165 getFirstAdminRouteAvailable () {
166 const right = this.getFirstAdminRightAvailable()
168 return this.routesPerRight[right]
171 logout (event: Event) {
172 event.preventDefault()
174 this.authService.logout()
175 // Redirect to home page
176 this.redirectService.redirectToHomepage()
179 openLanguageChooser () {
180 this.languageChooserModal.show()
183 openHotkeysCheatSheet () {
184 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
187 openQuickSettings () {
188 this.quickSettingsModal.show()
192 if (!this.user) return
193 this.user.p2pEnabled = !this.user.p2pEnabled
195 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
196 .subscribe(() => this.authService.refreshUserInformation())
199 langForLocale (localeId: string) {
200 if (localeId === '_unknown') return $localize`Unknown`
202 return this.languages.find(lang => lang.id === localeId).label
205 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
206 const linkURL = link.getAttribute('href')
207 const linkHash = link.getAttribute('fragment')
209 // On same url without fragment restore top scroll position
210 if (!linkHash && this.router.url.includes(linkURL)) {
211 scrollToTop('smooth')
214 // On same url with fragment restore anchor scroll position
215 if (linkHash && this.router.url === linkURL) {
216 this.viewportScroller.scrollToAnchor(linkHash)
219 if (this.screenService.isInSmallView()) {
220 this.menuService.toggleMenu()
224 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
225 onMenuScrollEvent () {
226 document.querySelector('menu').scrollTo(0, 0)
229 onDropdownOpenChange (opened: boolean) {
230 if (this.screenService.isInMobileView()) return
232 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
233 const onWindowScroll = () => {
234 this.dropdown?.close()
235 window.removeEventListener('scroll', onWindowScroll)
239 window.addEventListener('scroll', onWindowScroll)
240 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
241 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
243 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
247 private async buildMenuSections () {
248 const menuSections = []
250 if (this.isLoggedIn) {
252 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
257 this.menuService.buildCommonLinks(this.htmlServerConfig)
260 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
263 private buildUserLanguages () {
265 this.videoLanguages = []
269 if (!this.user.videoLanguages) {
270 this.videoLanguages = [ $localize`any language` ]
274 this.videoLanguages = this.user.videoLanguages
275 .map(locale => this.langForLocale(locale))
276 .map(value => value === undefined ? '?' : value)
279 private computeAdminAccess () {
280 const right = this.getFirstAdminRightAvailable()
282 this.userHasAdminAccess = right !== undefined
285 private computeVideosLink () {
286 if (!this.isLoggedIn) return
288 this.authService.userInformationLoaded
290 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
292 if (res === true) debugLogger('User can see videos link.')
293 else debugLogger('User cannot see videos link.')
297 private computeNSFWPolicy () {
299 this.nsfwPolicy = null
303 switch (this.user.nsfwPolicy) {
305 this.nsfwPolicy = $localize`hide`
309 this.nsfwPolicy = $localize`blur`
313 this.nsfwPolicy = $localize`display`
318 private updateUserState () {
319 this.user = this.isLoggedIn
320 ? this.authService.getUser()
323 this.computeAdminAccess()
324 this.computeNSFWPolicy()
325 this.computeVideosLink()