]>
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' |
21e6dc81 | 5 | import { AuthService, Notifier, ServerService } from '@app/core' |
a5f8b0b4 | 6 | import { of } from 'rxjs' |
7738273b | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
f3aaa9a9 C |
8 | |
9 | @Component({ | |
10 | selector: 'my-header', | |
11 | templateUrl: './header.component.html', | |
12 | styleUrls: [ './header.component.scss' ] | |
13 | }) | |
14 | ||
15 | export class HeaderComponent implements OnInit { | |
16 | searchValue = '' | |
7738273b | 17 | ariaLabelTextForSearch = '' |
f3aaa9a9 | 18 | |
a5f8b0b4 C |
19 | constructor ( |
20 | private router: Router, | |
baeb429d | 21 | private route: ActivatedRoute, |
000eb0e4 RK |
22 | private auth: AuthService, |
23 | private serverService: ServerService, | |
24 | private authService: AuthService, | |
7738273b RK |
25 | private notifier: Notifier, |
26 | private i18n: I18n | |
a5f8b0b4 | 27 | ) {} |
f3aaa9a9 C |
28 | |
29 | ngOnInit () { | |
7738273b RK |
30 | this.ariaLabelTextForSearch = this.i18n('Search videos, channels') |
31 | ||
66dc5907 | 32 | this.router.events |
db400f44 C |
33 | .pipe( |
34 | filter(e => e instanceof NavigationEnd), | |
ff644b30 | 35 | map(() => getParameterByName('search', window.location.href)) |
db400f44 | 36 | ) |
ff644b30 | 37 | .subscribe(searchQuery => this.searchValue = searchQuery || '') |
333952bc RK |
38 | } |
39 | ||
f3aaa9a9 | 40 | doSearch () { |
baeb429d C |
41 | const queryParams: Params = {} |
42 | ||
43 | if (window.location.pathname === '/search' && this.route.snapshot.queryParams) { | |
44 | Object.assign(queryParams, this.route.snapshot.queryParams) | |
a5f8b0b4 C |
45 | } |
46 | ||
baeb429d C |
47 | Object.assign(queryParams, { search: this.searchValue }) |
48 | ||
a5f8b0b4 | 49 | const o = this.auth.isLoggedIn() |
baeb429d | 50 | ? this.loadUserLanguagesIfNeeded(queryParams) |
a5f8b0b4 C |
51 | : of(true) |
52 | ||
53 | o.subscribe(() => this.router.navigate([ '/search' ], { queryParams })) | |
54 | } | |
55 | ||
baeb429d C |
56 | private loadUserLanguagesIfNeeded (queryParams: any) { |
57 | if (queryParams && queryParams.languageOneOf) return of(queryParams) | |
58 | ||
a5f8b0b4 C |
59 | return this.auth.userInformationLoaded |
60 | .pipe( | |
61 | first(), | |
62 | tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages })) | |
63 | ) | |
f3aaa9a9 C |
64 | } |
65 | } |