]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/header/header.component.ts
Translated using Weblate (Catalan)
[github/Chocobozzz/PeerTube.git] / client / src / app / header / header.component.ts
1 import { filter, first, map, tap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'
4 import { getParameterByName } from '../shared/misc/utils'
5 import { AuthService, ServerService, Notifier } from '@app/core'
6 import { of } from 'rxjs'
7 import { ServerConfig } from '@shared/models'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9
10 @Component({
11 selector: 'my-header',
12 templateUrl: './header.component.html',
13 styleUrls: [ './header.component.scss' ]
14 })
15
16 export class HeaderComponent implements OnInit {
17 searchValue = ''
18 ariaLabelTextForSearch = ''
19
20 private serverConfig: ServerConfig
21
22 constructor (
23 private router: Router,
24 private route: ActivatedRoute,
25 private auth: AuthService,
26 private serverService: ServerService,
27 private authService: AuthService,
28 private notifier: Notifier,
29 private i18n: I18n
30 ) {}
31
32 ngOnInit () {
33 this.ariaLabelTextForSearch = this.i18n('Search videos, channels')
34
35 this.router.events
36 .pipe(
37 filter(e => e instanceof NavigationEnd),
38 map(() => getParameterByName('search', window.location.href))
39 )
40 .subscribe(searchQuery => this.searchValue = searchQuery || '')
41
42 this.serverConfig = this.serverService.getTmpConfig()
43 this.serverService.getConfig().subscribe(
44 config => this.serverConfig = config,
45
46 err => this.notifier.error(err.message)
47 )
48 }
49
50 get routerLink () {
51 if (this.isUserLoggedIn()) {
52 return [ '/videos/upload' ]
53 } else if (this.isRegistrationAllowed()) {
54 return [ '/signup' ]
55 } else {
56 return [ '/login', { fromUpload: true } ]
57 }
58 }
59
60 doSearch () {
61 const queryParams: Params = {}
62
63 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
64 Object.assign(queryParams, this.route.snapshot.queryParams)
65 }
66
67 Object.assign(queryParams, { search: this.searchValue })
68
69 const o = this.auth.isLoggedIn()
70 ? this.loadUserLanguagesIfNeeded(queryParams)
71 : of(true)
72
73 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
74 }
75
76 isUserLoggedIn () {
77 return this.authService.isLoggedIn()
78 }
79
80 isRegistrationAllowed () {
81 return this.serverConfig.signup.allowed &&
82 this.serverConfig.signup.allowedForCurrentIP
83 }
84
85 private loadUserLanguagesIfNeeded (queryParams: any) {
86 if (queryParams && queryParams.languageOneOf) return of(queryParams)
87
88 return this.auth.userInformationLoaded
89 .pipe(
90 first(),
91 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))
92 )
93 }
94 }