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'
11 selector: 'my-header',
12 templateUrl: './header.component.html',
13 styleUrls: [ './header.component.scss' ]
16 export class HeaderComponent implements OnInit {
18 ariaLabelTextForSearch = ''
20 private serverConfig: ServerConfig
23 private router: Router,
24 private route: ActivatedRoute,
25 private auth: AuthService,
26 private serverService: ServerService,
27 private authService: AuthService,
28 private notifier: Notifier,
33 this.ariaLabelTextForSearch = this.i18n('Search videos, channels')
37 filter(e => e instanceof NavigationEnd),
38 map(() => getParameterByName('search', window.location.href))
40 .subscribe(searchQuery => this.searchValue = searchQuery || '')
42 this.serverConfig = this.serverService.getTmpConfig()
43 this.serverService.getConfig().subscribe(
44 config => this.serverConfig = config,
46 err => this.notifier.error(err.message)
51 if (this.isUserLoggedIn()) {
52 return [ '/videos/upload' ]
53 } else if (this.isRegistrationAllowed()) {
56 return [ '/login', { fromUpload: true } ]
61 const queryParams: Params = {}
63 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
64 Object.assign(queryParams, this.route.snapshot.queryParams)
67 Object.assign(queryParams, { search: this.searchValue })
69 const o = this.auth.isLoggedIn()
70 ? this.loadUserLanguagesIfNeeded(queryParams)
73 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
77 return this.authService.isLoggedIn()
80 isRegistrationAllowed () {
81 return this.serverConfig.signup.allowed &&
82 this.serverConfig.signup.allowedForCurrentIP
85 private loadUserLanguagesIfNeeded (queryParams: any) {
86 if (queryParams && queryParams.languageOneOf) return of(queryParams)
88 return this.auth.userInformationLoaded
91 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))