1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { switchMap } from 'rxjs/operators'
4 import { environment } from 'src/environments/environment'
5 import { ViewportScroller } from '@angular/common'
6 import { Component, OnInit, ViewChild } from '@angular/core'
7 import { Router } from '@angular/router'
20 import { scrollToTop } from '@app/helpers'
21 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
22 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
23 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
24 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
25 import { PluginsManager } from '@root-helpers/plugins-manager'
26 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
28 const debugLogger = debug('peertube:menu:MenuComponent')
32 templateUrl: './menu.component.html',
33 styleUrls: [ './menu.component.scss' ]
35 export class MenuComponent implements OnInit {
36 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
37 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
38 @ViewChild('dropdown') dropdown: NgbDropdown
43 userHasAdminAccess = false
46 videoLanguages: string[] = []
49 currentInterfaceLanguage: string
51 menuSections: MenuSection[] = []
53 private languages: VideoConstant<string>[] = []
55 private htmlServerConfig: HTMLServerConfig
56 private serverConfig: ServerConfig
58 private routesPerRight: { [role in UserRight]?: string } = {
59 [UserRight.MANAGE_USERS]: '/admin/users',
60 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
61 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
62 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
63 [UserRight.MANAGE_JOBS]: '/admin/jobs',
64 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
68 private viewportScroller: ViewportScroller,
69 private authService: AuthService,
70 private userService: UserService,
71 private serverService: ServerService,
72 private redirectService: RedirectService,
73 private hotkeysService: HotkeysService,
74 private screenService: ScreenService,
75 private menuService: MenuService,
76 private modalService: PeertubeModalService,
77 private router: Router,
78 private hooks: HooksService
81 get isInMobileView () {
82 return this.screenService.isInMobileView()
85 get dropdownContainer () {
86 if (this.isInMobileView) return null
88 return 'body' as 'body'
92 return this.languageChooserModal.getCurrentLanguage()
96 this.htmlServerConfig = this.serverService.getHTMLConfig()
97 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
99 this.isLoggedIn = this.authService.isLoggedIn()
100 this.updateUserState()
101 this.buildMenuSections()
103 this.authService.loginChangedSource.subscribe(
105 if (status === AuthStatus.LoggedIn) {
106 this.isLoggedIn = true
107 } else if (status === AuthStatus.LoggedOut) {
108 this.isLoggedIn = false
111 this.updateUserState()
112 this.buildMenuSections()
116 this.hotkeysService.cheatSheetToggle
117 .subscribe(isOpen => this.helpVisible = isOpen)
119 this.serverService.getVideoLanguages()
120 .subscribe(languages => {
121 this.languages = languages
123 this.authService.userInformationLoaded
124 .subscribe(() => this.buildUserLanguages())
127 this.serverService.getConfig()
128 .subscribe(config => this.serverConfig = config)
130 this.modalService.openQuickSettingsSubject
131 .subscribe(() => this.openQuickSettings())
134 getExternalLoginHref () {
135 return PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverConfig)
138 isRegistrationAllowed () {
139 if (!this.serverConfig) return false
141 return this.serverConfig.signup.allowed &&
142 this.serverConfig.signup.allowedForCurrentIP
145 getFirstAdminRightAvailable () {
146 const user = this.authService.getUser()
147 if (!user) return undefined
149 const adminRights = [
150 UserRight.MANAGE_USERS,
151 UserRight.MANAGE_SERVER_FOLLOW,
152 UserRight.MANAGE_ABUSES,
153 UserRight.MANAGE_VIDEO_BLACKLIST,
154 UserRight.MANAGE_JOBS,
155 UserRight.MANAGE_CONFIGURATION
158 for (const adminRight of adminRights) {
159 if (user.hasRight(adminRight)) {
167 getFirstAdminRouteAvailable () {
168 const right = this.getFirstAdminRightAvailable()
170 return this.routesPerRight[right]
173 logout (event: Event) {
174 event.preventDefault()
176 this.authService.logout()
177 // Redirect to home page
178 this.redirectService.redirectToHomepage()
181 openLanguageChooser () {
182 this.languageChooserModal.show()
185 openHotkeysCheatSheet () {
186 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
189 openQuickSettings () {
190 this.quickSettingsModal.show()
194 if (!this.user) return
195 this.user.p2pEnabled = !this.user.p2pEnabled
197 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
198 .subscribe(() => this.authService.refreshUserInformation())
201 langForLocale (localeId: string) {
202 if (localeId === '_unknown') return $localize`Unknown`
204 return this.languages.find(lang => lang.id === localeId).label
207 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
208 const linkURL = link.getAttribute('href')
209 const linkHash = link.getAttribute('fragment')
211 // On same url without fragment restore top scroll position
212 if (!linkHash && this.router.url.includes(linkURL)) {
213 scrollToTop('smooth')
216 // On same url with fragment restore anchor scroll position
217 if (linkHash && this.router.url === linkURL) {
218 this.viewportScroller.scrollToAnchor(linkHash)
221 if (this.screenService.isInSmallView()) {
222 this.menuService.toggleMenu()
226 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
227 onMenuScrollEvent () {
228 document.querySelector('menu').scrollTo(0, 0)
231 onDropdownOpenChange (opened: boolean) {
232 if (this.screenService.isInMobileView()) return
234 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
235 const onWindowScroll = () => {
236 this.dropdown?.close()
237 window.removeEventListener('scroll', onWindowScroll)
241 window.addEventListener('scroll', onWindowScroll)
242 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
243 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
245 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
249 private async buildMenuSections () {
250 const menuSections = []
252 if (this.isLoggedIn) {
254 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
259 this.menuService.buildCommonLinks(this.htmlServerConfig)
262 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
265 private buildUserLanguages () {
267 this.videoLanguages = []
271 if (!this.user.videoLanguages) {
272 this.videoLanguages = [ $localize`any language` ]
276 this.videoLanguages = this.user.videoLanguages
277 .map(locale => this.langForLocale(locale))
278 .map(value => value === undefined ? '?' : value)
281 private computeAdminAccess () {
282 const right = this.getFirstAdminRightAvailable()
284 this.userHasAdminAccess = right !== undefined
287 private computeVideosLink () {
288 if (!this.isLoggedIn) return
290 this.authService.userInformationLoaded
292 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
294 if (res === true) debugLogger('User can see videos link.')
295 else debugLogger('User cannot see videos link.')
299 private computeNSFWPolicy () {
301 this.nsfwPolicy = null
305 switch (this.user.nsfwPolicy) {
307 this.nsfwPolicy = $localize`hide`
311 this.nsfwPolicy = $localize`blur`
315 this.nsfwPolicy = $localize`display`
320 private updateUserState () {
321 this.user = this.isLoggedIn
322 ? this.authService.getUser()
325 this.computeAdminAccess()
326 this.computeNSFWPolicy()
327 this.computeVideosLink()