1 import { ViewportScroller } from '@angular/common'
2 import { HotkeysService } from 'angular2-hotkeys'
3 import * as debug from 'debug'
4 import { switchMap } from 'rxjs/operators'
5 import { Component, OnInit, ViewChild } from '@angular/core'
6 import { Router } from '@angular/router'
7 import { scrollToTop } from '@app/helpers'
8 import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
9 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
10 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
11 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
12 import { NgbDropdown, NgbDropdownConfig } from '@ng-bootstrap/ng-bootstrap'
13 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
15 const logger = debug('peertube:menu:MenuComponent')
19 templateUrl: './menu.component.html',
20 styleUrls: ['./menu.component.scss']
22 export class MenuComponent implements OnInit {
23 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
24 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
25 @ViewChild('dropdown') dropdown: NgbDropdown
30 userHasAdminAccess = false
33 videoLanguages: string[] = []
36 currentInterfaceLanguage: string
38 private languages: VideoConstant<string>[] = []
39 private serverConfig: ServerConfig
40 private routesPerRight: { [role in UserRight]?: string } = {
41 [UserRight.MANAGE_USERS]: '/admin/users',
42 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
43 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
44 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
45 [UserRight.MANAGE_JOBS]: '/admin/jobs',
46 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
50 private viewportScroller: ViewportScroller,
51 private authService: AuthService,
52 private userService: UserService,
53 private serverService: ServerService,
54 private redirectService: RedirectService,
55 private hotkeysService: HotkeysService,
56 private screenService: ScreenService,
57 private menuService: MenuService,
58 private modalService: PeertubeModalService,
59 private dropdownConfig: NgbDropdownConfig,
60 private router: Router
62 this.dropdownConfig.container = 'body'
65 get isInMobileView () {
66 return this.screenService.isInMobileView()
69 get dropdownContainer () {
70 if (this.isInMobileView) {
73 return this.dropdownConfig.container
78 return this.languageChooserModal.getCurrentLanguage()
82 return this.serverConfig.instance.name
86 this.serverConfig = this.serverService.getTmpConfig()
87 this.serverService.getConfig()
88 .subscribe(config => this.serverConfig = config)
90 this.isLoggedIn = this.authService.isLoggedIn()
91 if (this.isLoggedIn === true) {
92 this.user = this.authService.getUser()
94 this.computeNSFWPolicy()
95 this.computeVideosLink()
98 this.computeAdminAccess()
100 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
102 this.authService.loginChangedSource.subscribe(
104 if (status === AuthStatus.LoggedIn) {
105 this.isLoggedIn = true
106 this.user = this.authService.getUser()
108 this.computeAdminAccess()
109 this.computeVideosLink()
112 } else if (status === AuthStatus.LoggedOut) {
113 this.isLoggedIn = false
114 this.user = undefined
116 this.computeAdminAccess()
118 logger('Logged out.')
120 console.error('Unknown auth status: ' + status)
125 this.hotkeysService.cheatSheetToggle
126 .subscribe(isOpen => this.helpVisible = isOpen)
128 this.serverService.getVideoLanguages()
129 .subscribe(languages => {
130 this.languages = languages
132 this.authService.userInformationLoaded
133 .subscribe(() => this.buildUserLanguages())
136 this.modalService.openQuickSettingsSubject
137 .subscribe(() => this.openQuickSettings())
140 isRegistrationAllowed () {
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.webTorrentEnabled = !this.user.webTorrentEnabled
197 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
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 buildUserLanguages () {
251 this.videoLanguages = []
255 if (!this.user.videoLanguages) {
256 this.videoLanguages = [$localize`any language`]
260 this.videoLanguages = this.user.videoLanguages
261 .map(locale => this.langForLocale(locale))
262 .map(value => value === undefined ? '?' : value)
265 private computeAdminAccess () {
266 const right = this.getFirstAdminRightAvailable()
268 this.userHasAdminAccess = right !== undefined
271 private computeVideosLink () {
272 this.authService.userInformationLoaded
274 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
276 if (res === true) logger('User can see videos link.')
277 else logger('User cannot see videos link.')
281 private computeNSFWPolicy () {
283 this.nsfwPolicy = null
287 switch (this.user.nsfwPolicy) {
289 this.nsfwPolicy = $localize`hide`
293 this.nsfwPolicy = $localize`blur`
297 this.nsfwPolicy = $localize`display`