]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/menu/menu.component.ts
wt-plugin: clearify err msg
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
index 2c55b9a8482f8cfaecf6bf7ed56117a93527fe3c..bdc95127b4a91d57ba55a08c4d5abfdc51505530 100644 (file)
@@ -1,11 +1,13 @@
+import { ViewportScroller } from '@angular/common'
 import { HotkeysService } from 'angular2-hotkeys'
 import * as debug from 'debug'
 import { switchMap } from 'rxjs/operators'
 import { Component, OnInit, ViewChild } from '@angular/core'
-import { AuthService, AuthStatus, AuthUser, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
+import { Router } from '@angular/router'
+import { scrollToTop } from '@app/helpers'
+import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
-import { I18n } from '@ngx-translate/i18n-polyfill'
 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
 
 const logger = debug('peertube:menu:MenuComponent')
@@ -26,6 +28,11 @@ export class MenuComponent implements OnInit {
   helpVisible = false
 
   videoLanguages: string[] = []
+  nsfwPolicy: string
+
+  loggedInMorePlacement: string
+
+  currentInterfaceLanguage: string
 
   private languages: VideoConstant<string>[] = []
   private serverConfig: ServerConfig
@@ -39,25 +46,19 @@ export class MenuComponent implements OnInit {
   }
 
   constructor (
+    private viewportScroller: ViewportScroller,
     private authService: AuthService,
     private userService: UserService,
     private serverService: ServerService,
     private redirectService: RedirectService,
     private hotkeysService: HotkeysService,
     private screenService: ScreenService,
-    private i18n: I18n
+    private menuService: MenuService,
+    private router: Router
   ) { }
 
-  get isInMobileView () {
-    return this.screenService.isInMobileView()
-  }
-
-  get placement () {
-    if (this.isInMobileView) {
-      return 'left-top auto'
-    } else {
-      return 'right-top auto'
-    }
+  get instanceName () {
+    return this.serverConfig.instance.name
   }
 
   ngOnInit () {
@@ -68,11 +69,19 @@ export class MenuComponent implements OnInit {
     this.isLoggedIn = this.authService.isLoggedIn()
     if (this.isLoggedIn === true) {
       this.user = this.authService.getUser()
+
+      this.computeNSFWPolicy()
       this.computeVideosLink()
     }
 
     this.computeAdminAccess()
 
+    this.loggedInMorePlacement = this.screenService.isInMobileView()
+      ? 'left-top auto'
+      : 'right-top auto'
+
+    this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
+
     this.authService.loginChangedSource.subscribe(
       status => {
         if (status === AuthStatus.LoggedIn) {
@@ -108,25 +117,6 @@ export class MenuComponent implements OnInit {
       })
   }
 
-  get language () {
-    return this.languageChooserModal.getCurrentLanguage()
-  }
-
-  get nsfwPolicy () {
-    if (!this.user) return
-
-    switch (this.user.nsfwPolicy) {
-      case 'do_not_list':
-        return this.i18n('hide')
-
-      case 'blur':
-        return this.i18n('blur')
-
-      case 'display':
-        return this.i18n('display')
-    }
-  }
-
   isRegistrationAllowed () {
     return this.serverConfig.signup.allowed &&
       this.serverConfig.signup.allowedForCurrentIP
@@ -189,11 +179,30 @@ export class MenuComponent implements OnInit {
   }
 
   langForLocale (localeId: string) {
-    if (localeId === '_unknown') return this.i18n('Unknown')
+    if (localeId === '_unknown') return $localize`Unknown`
 
     return this.languages.find(lang => lang.id === localeId).label
   }
 
+  onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
+    const linkURL = link.getAttribute('href')
+    const linkHash = link.getAttribute('fragment')
+
+    // On same url without fragment restore top scroll position
+    if (!linkHash && this.router.url.includes(linkURL)) {
+      scrollToTop('smooth')
+    }
+
+    // On same url with fragment restore anchor scroll position
+    if (linkHash && this.router.url === linkURL) {
+      this.viewportScroller.scrollToAnchor(linkHash)
+    }
+
+    if (this.screenService.isInSmallView()) {
+      this.menuService.toggleMenu()
+    }
+  }
+
   private buildUserLanguages () {
     if (!this.user) {
       this.videoLanguages = []
@@ -201,7 +210,7 @@ export class MenuComponent implements OnInit {
     }
 
     if (!this.user.videoLanguages) {
-      this.videoLanguages = [this.i18n('any language')]
+      this.videoLanguages = [$localize`any language`]
       return
     }
 
@@ -225,4 +234,25 @@ export class MenuComponent implements OnInit {
         else logger('User cannot see videos link.')
       })
   }
+
+  private computeNSFWPolicy () {
+    if (!this.user) {
+      this.nsfwPolicy = null
+      return
+    }
+
+    switch (this.user.nsfwPolicy) {
+      case 'do_not_list':
+        this.nsfwPolicy = $localize`hide`
+        break
+
+      case 'blur':
+        this.nsfwPolicy = $localize`blur`
+        break
+
+      case 'display':
+        this.nsfwPolicy = $localize`display`
+        break
+    }
+  }
 }