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