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 logger = 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()
94 this.htmlServerConfig = this.serverService.getHTMLConfig()
95 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
97 this.updateUserState()
98 this.buildMenuSections()
100 this.authService.loginChangedSource.subscribe(
102 if (status === AuthStatus.LoggedIn) {
103 this.isLoggedIn = true
104 } else if (status === AuthStatus.LoggedOut) {
105 this.isLoggedIn = false
108 this.updateUserState()
109 this.buildMenuSections()
113 this.hotkeysService.cheatSheetToggle
114 .subscribe(isOpen => this.helpVisible = isOpen)
116 this.serverService.getVideoLanguages()
117 .subscribe(languages => {
118 this.languages = languages
120 this.authService.userInformationLoaded
121 .subscribe(() => this.buildUserLanguages())
124 this.modalService.openQuickSettingsSubject
125 .subscribe(() => this.openQuickSettings())
128 isRegistrationAllowed () {
129 if (!this.serverConfig) return false
131 return this.serverConfig.signup.allowed &&
132 this.serverConfig.signup.allowedForCurrentIP
135 getFirstAdminRightAvailable () {
136 const user = this.authService.getUser()
137 if (!user) return undefined
139 const adminRights = [
140 UserRight.MANAGE_USERS,
141 UserRight.MANAGE_SERVER_FOLLOW,
142 UserRight.MANAGE_ABUSES,
143 UserRight.MANAGE_VIDEO_BLACKLIST,
144 UserRight.MANAGE_JOBS,
145 UserRight.MANAGE_CONFIGURATION
148 for (const adminRight of adminRights) {
149 if (user.hasRight(adminRight)) {
157 getFirstAdminRouteAvailable () {
158 const right = this.getFirstAdminRightAvailable()
160 return this.routesPerRight[right]
163 logout (event: Event) {
164 event.preventDefault()
166 this.authService.logout()
167 // Redirect to home page
168 this.redirectService.redirectToHomepage()
171 openLanguageChooser () {
172 this.languageChooserModal.show()
175 openHotkeysCheatSheet () {
176 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
179 openQuickSettings () {
180 this.quickSettingsModal.show()
184 if (!this.user) return
185 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
187 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
188 .subscribe(() => this.authService.refreshUserInformation())
191 langForLocale (localeId: string) {
192 if (localeId === '_unknown') return $localize`Unknown`
194 return this.languages.find(lang => lang.id === localeId).label
197 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
198 const linkURL = link.getAttribute('href')
199 const linkHash = link.getAttribute('fragment')
201 // On same url without fragment restore top scroll position
202 if (!linkHash && this.router.url.includes(linkURL)) {
203 scrollToTop('smooth')
206 // On same url with fragment restore anchor scroll position
207 if (linkHash && this.router.url === linkURL) {
208 this.viewportScroller.scrollToAnchor(linkHash)
211 if (this.screenService.isInSmallView()) {
212 this.menuService.toggleMenu()
216 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
217 onMenuScrollEvent () {
218 document.querySelector('menu').scrollTo(0, 0)
221 onDropdownOpenChange (opened: boolean) {
222 if (this.screenService.isInMobileView()) return
224 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
225 const onWindowScroll = () => {
226 this.dropdown?.close()
227 window.removeEventListener('scroll', onWindowScroll)
231 window.addEventListener('scroll', onWindowScroll)
232 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
233 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
235 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
239 private async buildMenuSections () {
240 const menuSections = []
242 if (this.isLoggedIn) {
244 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
249 this.menuService.buildCommonLinks(this.htmlServerConfig)
252 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
255 private buildUserLanguages () {
257 this.videoLanguages = []
261 if (!this.user.videoLanguages) {
262 this.videoLanguages = [ $localize`any language` ]
266 this.videoLanguages = this.user.videoLanguages
267 .map(locale => this.langForLocale(locale))
268 .map(value => value === undefined ? '?' : value)
271 private computeAdminAccess () {
272 const right = this.getFirstAdminRightAvailable()
274 this.userHasAdminAccess = right !== undefined
277 private computeVideosLink () {
278 if (!this.isLoggedIn) return
280 this.authService.userInformationLoaded
282 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
284 if (res === true) logger('User can see videos link.')
285 else logger('User cannot see videos link.')
289 private computeNSFWPolicy () {
291 this.nsfwPolicy = null
295 switch (this.user.nsfwPolicy) {
297 this.nsfwPolicy = $localize`hide`
301 this.nsfwPolicy = $localize`blur`
305 this.nsfwPolicy = $localize`display`
310 private updateUserState () {
311 this.user = this.isLoggedIn
312 ? this.authService.getUser()
315 this.computeAdminAccess()
316 this.computeNSFWPolicy()
317 this.computeVideosLink()