]>
Commit | Line | Data |
---|---|---|
57c36b27 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 2 | import { ActivatedRoute, Router } from '@angular/router' |
57c36b27 C |
3 | import { RedirectService } from '@app/core' |
4 | import { NotificationsService } from 'angular2-notifications' | |
5 | import { Subscription } from 'rxjs' | |
6 | import { SearchService } from '@app/search/search.service' | |
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { Video } from '../../../../shared' | |
10 | import { MetaService } from '@ngx-meta/core' | |
0b18f4aa | 11 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
57c36b27 C |
12 | |
13 | @Component({ | |
14 | selector: 'my-search', | |
15 | styleUrls: [ './search.component.scss' ], | |
16 | templateUrl: './search.component.html' | |
17 | }) | |
18 | export class SearchComponent implements OnInit, OnDestroy { | |
19 | videos: Video[] = [] | |
20 | pagination: ComponentPagination = { | |
21 | currentPage: 1, | |
22 | itemsPerPage: 10, // It's per object type (so 10 videos, 10 video channels etc) | |
23 | totalItems: null | |
24 | } | |
0b18f4aa C |
25 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
26 | isSearchFilterCollapsed = true | |
57c36b27 C |
27 | |
28 | private subActivatedRoute: Subscription | |
29 | private currentSearch: string | |
7afea880 | 30 | private isInitialLoad = true |
57c36b27 C |
31 | |
32 | constructor ( | |
33 | private i18n: I18n, | |
34 | private route: ActivatedRoute, | |
0b18f4aa | 35 | private router: Router, |
57c36b27 C |
36 | private metaService: MetaService, |
37 | private redirectService: RedirectService, | |
38 | private notificationsService: NotificationsService, | |
39 | private searchService: SearchService | |
40 | ) { } | |
41 | ||
42 | ngOnInit () { | |
43 | this.subActivatedRoute = this.route.queryParams.subscribe( | |
44 | queryParams => { | |
45 | const querySearch = queryParams['search'] | |
46 | ||
47 | if (!querySearch) return this.redirectService.redirectToHomepage() | |
57c36b27 | 48 | |
0b18f4aa | 49 | // Search updated, reset filters |
7afea880 C |
50 | if (this.currentSearch !== querySearch) { |
51 | this.resetPagination() | |
52 | this.advancedSearch.reset() | |
53 | ||
54 | this.currentSearch = querySearch | |
55 | this.updateTitle() | |
56 | } | |
57 | ||
58 | this.advancedSearch = new AdvancedSearch(queryParams) | |
0b18f4aa | 59 | |
7afea880 C |
60 | // Don't hide filters if we have some of them AND the user just came on the webpage |
61 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
62 | this.isInitialLoad = false | |
57c36b27 | 63 | |
7afea880 | 64 | this.search() |
57c36b27 C |
65 | }, |
66 | ||
67 | err => this.notificationsService.error('Error', err.text) | |
68 | ) | |
69 | } | |
70 | ||
71 | ngOnDestroy () { | |
72 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
73 | } | |
74 | ||
75 | search () { | |
0b18f4aa | 76 | return this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch) |
57c36b27 C |
77 | .subscribe( |
78 | ({ videos, totalVideos }) => { | |
79 | this.videos = this.videos.concat(videos) | |
80 | this.pagination.totalItems = totalVideos | |
81 | }, | |
82 | ||
83 | error => { | |
84 | this.notificationsService.error(this.i18n('Error'), error.message) | |
85 | } | |
86 | ) | |
87 | } | |
88 | ||
89 | onNearOfBottom () { | |
90 | // Last page | |
91 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
92 | ||
93 | this.pagination.currentPage += 1 | |
94 | this.search() | |
95 | } | |
96 | ||
0b18f4aa | 97 | onFiltered () { |
7afea880 | 98 | this.resetPagination() |
0b18f4aa | 99 | |
7afea880 | 100 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
101 | } |
102 | ||
7afea880 | 103 | private resetPagination () { |
57c36b27 C |
104 | this.pagination.currentPage = 1 |
105 | this.pagination.totalItems = null | |
106 | ||
107 | this.videos = [] | |
57c36b27 C |
108 | } |
109 | ||
110 | private updateTitle () { | |
111 | this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch) | |
112 | } | |
0b18f4aa C |
113 | |
114 | private updateUrlFromAdvancedSearch () { | |
115 | this.router.navigate([], { | |
116 | relativeTo: this.route, | |
117 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch }) | |
118 | }) | |
119 | } | |
57c36b27 | 120 | } |