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.serverService.getConfig()
126 .subscribe(config => this.serverConfig = config)
128 this.modalService.openQuickSettingsSubject
129 .subscribe(() => this.openQuickSettings())
132 isRegistrationAllowed () {
133 if (!this.serverConfig) return false
135 return this.serverConfig.signup.allowed &&
136 this.serverConfig.signup.allowedForCurrentIP
139 getFirstAdminRightAvailable () {
140 const user = this.authService.getUser()
141 if (!user) return undefined
143 const adminRights = [
144 UserRight.MANAGE_USERS,
145 UserRight.MANAGE_SERVER_FOLLOW,
146 UserRight.MANAGE_ABUSES,
147 UserRight.MANAGE_VIDEO_BLACKLIST,
148 UserRight.MANAGE_JOBS,
149 UserRight.MANAGE_CONFIGURATION
152 for (const adminRight of adminRights) {
153 if (user.hasRight(adminRight)) {
161 getFirstAdminRouteAvailable () {
162 const right = this.getFirstAdminRightAvailable()
164 return this.routesPerRight[right]
167 logout (event: Event) {
168 event.preventDefault()
170 this.authService.logout()
171 // Redirect to home page
172 this.redirectService.redirectToHomepage()
175 openLanguageChooser () {
176 this.languageChooserModal.show()
179 openHotkeysCheatSheet () {
180 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
183 openQuickSettings () {
184 this.quickSettingsModal.show()
188 if (!this.user) return
189 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
191 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
192 .subscribe(() => this.authService.refreshUserInformation())
195 langForLocale (localeId: string) {
196 if (localeId === '_unknown') return $localize`Unknown`
198 return this.languages.find(lang => lang.id === localeId).label
201 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
202 const linkURL = link.getAttribute('href')
203 const linkHash = link.getAttribute('fragment')
205 // On same url without fragment restore top scroll position
206 if (!linkHash && this.router.url.includes(linkURL)) {
207 scrollToTop('smooth')
210 // On same url with fragment restore anchor scroll position
211 if (linkHash && this.router.url === linkURL) {
212 this.viewportScroller.scrollToAnchor(linkHash)
215 if (this.screenService.isInSmallView()) {
216 this.menuService.toggleMenu()
220 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
221 onMenuScrollEvent () {
222 document.querySelector('menu').scrollTo(0, 0)
225 onDropdownOpenChange (opened: boolean) {
226 if (this.screenService.isInMobileView()) return
228 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
229 const onWindowScroll = () => {
230 this.dropdown?.close()
231 window.removeEventListener('scroll', onWindowScroll)
235 window.addEventListener('scroll', onWindowScroll)
236 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
237 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
239 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
243 private async buildMenuSections () {
244 const menuSections = []
246 if (this.isLoggedIn) {
248 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
253 this.menuService.buildCommonLinks(this.htmlServerConfig)
256 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
259 private buildUserLanguages () {
261 this.videoLanguages = []
265 if (!this.user.videoLanguages) {
266 this.videoLanguages = [ $localize`any language` ]
270 this.videoLanguages = this.user.videoLanguages
271 .map(locale => this.langForLocale(locale))
272 .map(value => value === undefined ? '?' : value)
275 private computeAdminAccess () {
276 const right = this.getFirstAdminRightAvailable()
278 this.userHasAdminAccess = right !== undefined
281 private computeVideosLink () {
282 if (!this.isLoggedIn) return
284 this.authService.userInformationLoaded
286 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
288 if (res === true) logger('User can see videos link.')
289 else logger('User cannot see videos link.')
293 private computeNSFWPolicy () {
295 this.nsfwPolicy = null
299 switch (this.user.nsfwPolicy) {
301 this.nsfwPolicy = $localize`hide`
305 this.nsfwPolicy = $localize`blur`
309 this.nsfwPolicy = $localize`display`
314 private updateUserState () {
315 this.user = this.isLoggedIn
316 ? this.authService.getUser()
319 this.computeAdminAccess()
320 this.computeNSFWPolicy()
321 this.computeVideosLink()