]>
Commit | Line | Data |
---|---|---|
a5f8b0b4 | 1 | import { filter, first, map, tap } from 'rxjs/operators' |
f3aaa9a9 | 2 | import { Component, OnInit } from '@angular/core' |
baeb429d | 3 | import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router' |
f3aaa9a9 | 4 | import { getParameterByName } from '../shared/misc/utils' |
000eb0e4 | 5 | import { AuthService, ServerService, Notifier } from '@app/core' |
a5f8b0b4 | 6 | import { of } from 'rxjs' |
000eb0e4 | 7 | import { ServerConfig } from '@shared/models' |
7738273b | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
f3aaa9a9 C |
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 = '' | |
7738273b | 18 | ariaLabelTextForSearch = '' |
f3aaa9a9 | 19 | |
000eb0e4 RK |
20 | private serverConfig: ServerConfig |
21 | ||
a5f8b0b4 C |
22 | constructor ( |
23 | private router: Router, | |
baeb429d | 24 | private route: ActivatedRoute, |
000eb0e4 RK |
25 | private auth: AuthService, |
26 | private serverService: ServerService, | |
27 | private authService: AuthService, | |
7738273b RK |
28 | private notifier: Notifier, |
29 | private i18n: I18n | |
a5f8b0b4 | 30 | ) {} |
f3aaa9a9 C |
31 | |
32 | ngOnInit () { | |
7738273b RK |
33 | this.ariaLabelTextForSearch = this.i18n('Search videos, channels') |
34 | ||
66dc5907 | 35 | this.router.events |
db400f44 C |
36 | .pipe( |
37 | filter(e => e instanceof NavigationEnd), | |
ff644b30 | 38 | map(() => getParameterByName('search', window.location.href)) |
db400f44 | 39 | ) |
ff644b30 | 40 | .subscribe(searchQuery => this.searchValue = searchQuery || '') |
000eb0e4 RK |
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 | ) | |
f3aaa9a9 C |
48 | } |
49 | ||
333952bc RK |
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 | ||
f3aaa9a9 | 60 | doSearch () { |
baeb429d C |
61 | const queryParams: Params = {} |
62 | ||
63 | if (window.location.pathname === '/search' && this.route.snapshot.queryParams) { | |
64 | Object.assign(queryParams, this.route.snapshot.queryParams) | |
a5f8b0b4 C |
65 | } |
66 | ||
baeb429d C |
67 | Object.assign(queryParams, { search: this.searchValue }) |
68 | ||
a5f8b0b4 | 69 | const o = this.auth.isLoggedIn() |
baeb429d | 70 | ? this.loadUserLanguagesIfNeeded(queryParams) |
a5f8b0b4 C |
71 | : of(true) |
72 | ||
73 | o.subscribe(() => this.router.navigate([ '/search' ], { queryParams })) | |
74 | } | |
75 | ||
000eb0e4 RK |
76 | isUserLoggedIn () { |
77 | return this.authService.isLoggedIn() | |
78 | } | |
79 | ||
80 | isRegistrationAllowed () { | |
81 | return this.serverConfig.signup.allowed && | |
82 | this.serverConfig.signup.allowedForCurrentIP | |
83 | } | |
84 | ||
baeb429d C |
85 | private loadUserLanguagesIfNeeded (queryParams: any) { |
86 | if (queryParams && queryParams.languageOneOf) return of(queryParams) | |
87 | ||
a5f8b0b4 C |
88 | return this.auth.userInformationLoaded |
89 | .pipe( | |
90 | first(), | |
91 | tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages })) | |
92 | ) | |
f3aaa9a9 C |
93 | } |
94 | } |