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.isLoggedIn = this.authService.isLoggedIn()
98 this.updateUserState()
99 this.buildMenuSections()
101 this.authService.loginChangedSource.subscribe(
103 if (status === AuthStatus.LoggedIn) {
104 this.isLoggedIn = true
105 } else if (status === AuthStatus.LoggedOut) {
106 this.isLoggedIn = false
109 this.updateUserState()
110 this.buildMenuSections()
114 this.hotkeysService.cheatSheetToggle
115 .subscribe(isOpen => this.helpVisible = isOpen)
117 this.serverService.getVideoLanguages()
118 .subscribe(languages => {
119 this.languages = languages
121 this.authService.userInformationLoaded
122 .subscribe(() => this.buildUserLanguages())
125 this.modalService.openQuickSettingsSubject
126 .subscribe(() => this.openQuickSettings())
129 isRegistrationAllowed () {
130 if (!this.serverConfig) return false
132 return this.serverConfig.signup.allowed &&
133 this.serverConfig.signup.allowedForCurrentIP
136 getFirstAdminRightAvailable () {
137 const user = this.authService.getUser()
138 if (!user) return undefined
140 const adminRights = [
141 UserRight.MANAGE_USERS,
142 UserRight.MANAGE_SERVER_FOLLOW,
143 UserRight.MANAGE_ABUSES,
144 UserRight.MANAGE_VIDEO_BLACKLIST,
145 UserRight.MANAGE_JOBS,
146 UserRight.MANAGE_CONFIGURATION
149 for (const adminRight of adminRights) {
150 if (user.hasRight(adminRight)) {
158 getFirstAdminRouteAvailable () {
159 const right = this.getFirstAdminRightAvailable()
161 return this.routesPerRight[right]
164 logout (event: Event) {
165 event.preventDefault()
167 this.authService.logout()
168 // Redirect to home page
169 this.redirectService.redirectToHomepage()
172 openLanguageChooser () {
173 this.languageChooserModal.show()
176 openHotkeysCheatSheet () {
177 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
180 openQuickSettings () {
181 this.quickSettingsModal.show()
185 if (!this.user) return
186 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
188 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
189 .subscribe(() => this.authService.refreshUserInformation())
192 langForLocale (localeId: string) {
193 if (localeId === '_unknown') return $localize`Unknown`
195 return this.languages.find(lang => lang.id === localeId).label
198 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
199 const linkURL = link.getAttribute('href')
200 const linkHash = link.getAttribute('fragment')
202 // On same url without fragment restore top scroll position
203 if (!linkHash && this.router.url.includes(linkURL)) {
204 scrollToTop('smooth')
207 // On same url with fragment restore anchor scroll position
208 if (linkHash && this.router.url === linkURL) {
209 this.viewportScroller.scrollToAnchor(linkHash)
212 if (this.screenService.isInSmallView()) {
213 this.menuService.toggleMenu()
217 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
218 onMenuScrollEvent () {
219 document.querySelector('menu').scrollTo(0, 0)
222 onDropdownOpenChange (opened: boolean) {
223 if (this.screenService.isInMobileView()) return
225 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
226 const onWindowScroll = () => {
227 this.dropdown?.close()
228 window.removeEventListener('scroll', onWindowScroll)
232 window.addEventListener('scroll', onWindowScroll)
233 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
234 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
236 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
240 private async buildMenuSections () {
241 const menuSections = []
243 if (this.isLoggedIn) {
245 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
250 this.menuService.buildCommonLinks(this.htmlServerConfig)
253 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
256 private buildUserLanguages () {
258 this.videoLanguages = []
262 if (!this.user.videoLanguages) {
263 this.videoLanguages = [ $localize`any language` ]
267 this.videoLanguages = this.user.videoLanguages
268 .map(locale => this.langForLocale(locale))
269 .map(value => value === undefined ? '?' : value)
272 private computeAdminAccess () {
273 const right = this.getFirstAdminRightAvailable()
275 this.userHasAdminAccess = right !== undefined
278 private computeVideosLink () {
279 if (!this.isLoggedIn) return
281 this.authService.userInformationLoaded
283 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
285 if (res === true) logger('User can see videos link.')
286 else logger('User cannot see videos link.')
290 private computeNSFWPolicy () {
292 this.nsfwPolicy = null
296 switch (this.user.nsfwPolicy) {
298 this.nsfwPolicy = $localize`hide`
302 this.nsfwPolicy = $localize`blur`
306 this.nsfwPolicy = $localize`display`
311 private updateUserState () {
312 this.user = this.isLoggedIn
313 ? this.authService.getUser()
316 this.computeAdminAccess()
317 this.computeNSFWPolicy()
318 this.computeVideosLink()