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'
18 import { scrollToTop } from '@app/helpers'
19 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
20 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
21 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
22 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
23 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
25 const logger = debug('peertube:menu:MenuComponent')
29 templateUrl: './menu.component.html',
30 styleUrls: ['./menu.component.scss']
32 export class MenuComponent implements OnInit {
33 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
34 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
35 @ViewChild('dropdown') dropdown: NgbDropdown
40 userHasAdminAccess = false
43 videoLanguages: string[] = []
46 currentInterfaceLanguage: string
48 commonMenuLinks: MenuLink[] = []
50 private languages: VideoConstant<string>[] = []
52 private htmlServerConfig: HTMLServerConfig
53 private serverConfig: ServerConfig
55 private routesPerRight: { [role in UserRight]?: string } = {
56 [UserRight.MANAGE_USERS]: '/admin/users',
57 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
58 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
59 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
60 [UserRight.MANAGE_JOBS]: '/admin/jobs',
61 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
65 private viewportScroller: ViewportScroller,
66 private authService: AuthService,
67 private userService: UserService,
68 private serverService: ServerService,
69 private redirectService: RedirectService,
70 private hotkeysService: HotkeysService,
71 private screenService: ScreenService,
72 private menuService: MenuService,
73 private modalService: PeertubeModalService,
74 private router: Router
77 get isInMobileView () {
78 return this.screenService.isInMobileView()
81 get dropdownContainer () {
82 if (this.isInMobileView) return null
84 return 'body' as 'body'
88 return this.languageChooserModal.getCurrentLanguage()
92 return this.htmlServerConfig.instance.name
96 this.htmlServerConfig = this.serverService.getHTMLConfig()
99 this.isLoggedIn = this.authService.isLoggedIn()
100 if (this.isLoggedIn === true) {
101 this.user = this.authService.getUser()
103 this.computeNSFWPolicy()
104 this.computeVideosLink()
107 this.computeAdminAccess()
109 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
111 this.authService.loginChangedSource.subscribe(
113 if (status === AuthStatus.LoggedIn) {
114 this.isLoggedIn = true
115 this.user = this.authService.getUser()
117 this.computeAdminAccess()
118 this.computeVideosLink()
121 } else if (status === AuthStatus.LoggedOut) {
122 this.isLoggedIn = false
123 this.user = undefined
125 this.computeAdminAccess()
127 logger('Logged out.')
129 console.error('Unknown auth status: ' + status)
134 this.hotkeysService.cheatSheetToggle
135 .subscribe(isOpen => this.helpVisible = isOpen)
137 this.serverService.getVideoLanguages()
138 .subscribe(languages => {
139 this.languages = languages
141 this.authService.userInformationLoaded
142 .subscribe(() => this.buildUserLanguages())
145 this.modalService.openQuickSettingsSubject
146 .subscribe(() => this.openQuickSettings())
149 isRegistrationAllowed () {
150 if (!this.serverConfig) return false
152 return this.serverConfig.signup.allowed &&
153 this.serverConfig.signup.allowedForCurrentIP
156 getFirstAdminRightAvailable () {
157 const user = this.authService.getUser()
158 if (!user) return undefined
160 const adminRights = [
161 UserRight.MANAGE_USERS,
162 UserRight.MANAGE_SERVER_FOLLOW,
163 UserRight.MANAGE_ABUSES,
164 UserRight.MANAGE_VIDEO_BLACKLIST,
165 UserRight.MANAGE_JOBS,
166 UserRight.MANAGE_CONFIGURATION
169 for (const adminRight of adminRights) {
170 if (user.hasRight(adminRight)) {
178 getFirstAdminRouteAvailable () {
179 const right = this.getFirstAdminRightAvailable()
181 return this.routesPerRight[right]
184 logout (event: Event) {
185 event.preventDefault()
187 this.authService.logout()
188 // Redirect to home page
189 this.redirectService.redirectToHomepage()
192 openLanguageChooser () {
193 this.languageChooserModal.show()
196 openHotkeysCheatSheet () {
197 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
200 openQuickSettings () {
201 this.quickSettingsModal.show()
205 if (!this.user) return
206 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
208 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
209 .subscribe(() => this.authService.refreshUserInformation())
212 langForLocale (localeId: string) {
213 if (localeId === '_unknown') return $localize`Unknown`
215 return this.languages.find(lang => lang.id === localeId).label
218 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
219 const linkURL = link.getAttribute('href')
220 const linkHash = link.getAttribute('fragment')
222 // On same url without fragment restore top scroll position
223 if (!linkHash && this.router.url.includes(linkURL)) {
224 scrollToTop('smooth')
227 // On same url with fragment restore anchor scroll position
228 if (linkHash && this.router.url === linkURL) {
229 this.viewportScroller.scrollToAnchor(linkHash)
232 if (this.screenService.isInSmallView()) {
233 this.menuService.toggleMenu()
237 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
238 onMenuScrollEvent () {
239 document.querySelector('menu').scrollTo(0, 0)
242 onDropdownOpenChange (opened: boolean) {
243 if (this.screenService.isInMobileView()) return
245 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
246 const onWindowScroll = () => {
247 this.dropdown?.close()
248 window.removeEventListener('scroll', onWindowScroll)
252 window.addEventListener('scroll', onWindowScroll)
253 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
254 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
256 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
260 private buildMenuLinks () {
261 this.commonMenuLinks = this.menuService.buildCommonLinks(this.htmlServerConfig)
264 private buildUserLanguages () {
266 this.videoLanguages = []
270 if (!this.user.videoLanguages) {
271 this.videoLanguages = [$localize`any language`]
275 this.videoLanguages = this.user.videoLanguages
276 .map(locale => this.langForLocale(locale))
277 .map(value => value === undefined ? '?' : value)
280 private computeAdminAccess () {
281 const right = this.getFirstAdminRightAvailable()
283 this.userHasAdminAccess = right !== undefined
286 private computeVideosLink () {
287 this.authService.userInformationLoaded
289 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
291 if (res === true) logger('User can see videos link.')
292 else logger('User cannot see videos link.')
296 private computeNSFWPolicy () {
298 this.nsfwPolicy = null
302 switch (this.user.nsfwPolicy) {
304 this.nsfwPolicy = $localize`hide`
308 this.nsfwPolicy = $localize`blur`
312 this.nsfwPolicy = $localize`display`