]>
Commit | Line | Data |
---|---|---|
57c36b27 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 2 | import { ActivatedRoute, Router } from '@angular/router' |
3a0fb65c | 3 | import { AuthService, Notifier } from '@app/core' |
f37dc0dd | 4 | import { forkJoin, Subscription } from 'rxjs' |
57c36b27 C |
5 | import { SearchService } from '@app/search/search.service' |
6 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
7 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
57c36b27 | 8 | import { MetaService } from '@ngx-meta/core' |
0b18f4aa | 9 | import { AdvancedSearch } from '@app/search/advanced-search.model' |
f37dc0dd C |
10 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
11 | import { immutableAssign } from '@app/shared/misc/utils' | |
26fabbd6 | 12 | import { Video } from '@app/shared/video/video.model' |
93cae479 | 13 | import { HooksService } from '@app/core/plugins/hooks.service' |
57c36b27 C |
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 { | |
26fabbd6 | 21 | results: (Video | VideoChannel)[] = [] |
f37dc0dd | 22 | |
57c36b27 C |
23 | pagination: ComponentPagination = { |
24 | currentPage: 1, | |
f37dc0dd | 25 | itemsPerPage: 10, // Only for videos, use another variable for channels |
57c36b27 C |
26 | totalItems: null |
27 | } | |
0b18f4aa C |
28 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
29 | isSearchFilterCollapsed = true | |
f37dc0dd | 30 | currentSearch: string |
57c36b27 C |
31 | |
32 | private subActivatedRoute: Subscription | |
c5d04b4f | 33 | private isInitialLoad = false // set to false to show the search filters on first arrival |
b1ee8526 | 34 | private firstSearch = true |
57c36b27 | 35 | |
f37dc0dd C |
36 | private channelsPerPage = 2 |
37 | ||
57c36b27 C |
38 | constructor ( |
39 | private i18n: I18n, | |
40 | private route: ActivatedRoute, | |
0b18f4aa | 41 | private router: Router, |
57c36b27 | 42 | private metaService: MetaService, |
f8b2c1b4 | 43 | private notifier: Notifier, |
26fabbd6 | 44 | private searchService: SearchService, |
93cae479 C |
45 | private authService: AuthService, |
46 | private hooks: HooksService | |
57c36b27 C |
47 | ) { } |
48 | ||
e2409062 C |
49 | get user () { |
50 | return this.authService.getUser() | |
51 | } | |
52 | ||
57c36b27 C |
53 | ngOnInit () { |
54 | this.subActivatedRoute = this.route.queryParams.subscribe( | |
55 | queryParams => { | |
56 | const querySearch = queryParams['search'] | |
57 | ||
0b18f4aa | 58 | // Search updated, reset filters |
7afea880 C |
59 | if (this.currentSearch !== querySearch) { |
60 | this.resetPagination() | |
61 | this.advancedSearch.reset() | |
62 | ||
47879669 | 63 | this.currentSearch = querySearch || undefined |
7afea880 C |
64 | this.updateTitle() |
65 | } | |
66 | ||
67 | this.advancedSearch = new AdvancedSearch(queryParams) | |
0b18f4aa | 68 | |
7afea880 C |
69 | // Don't hide filters if we have some of them AND the user just came on the webpage |
70 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
71 | this.isInitialLoad = false | |
57c36b27 | 72 | |
7afea880 | 73 | this.search() |
57c36b27 C |
74 | }, |
75 | ||
f8b2c1b4 | 76 | err => this.notifier.error(err.text) |
57c36b27 C |
77 | ) |
78 | } | |
79 | ||
80 | ngOnDestroy () { | |
81 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
82 | } | |
83 | ||
26fabbd6 C |
84 | isVideoChannel (d: VideoChannel | Video): d is VideoChannel { |
85 | return d instanceof VideoChannel | |
86 | } | |
87 | ||
88 | isVideo (v: VideoChannel | Video): v is Video { | |
89 | return v instanceof Video | |
90 | } | |
91 | ||
92 | isUserLoggedIn () { | |
93 | return this.authService.isLoggedIn() | |
94 | } | |
95 | ||
57c36b27 | 96 | search () { |
f37dc0dd | 97 | forkJoin([ |
93cae479 C |
98 | this.getVideosObs(), |
99 | this.getVideoChannelObs() | |
f37dc0dd | 100 | ]) |
57c36b27 | 101 | .subscribe( |
f37dc0dd | 102 | ([ videosResult, videoChannelsResult ]) => { |
26fabbd6 C |
103 | this.results = this.results |
104 | .concat(videoChannelsResult.data) | |
93cae479 C |
105 | .concat(videosResult.data) |
106 | this.pagination.totalItems = videosResult.total + videoChannelsResult.total | |
f37dc0dd | 107 | |
b1ee8526 | 108 | // Focus on channels if there are no enough videos |
93cae479 | 109 | if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) { |
aa55a4da | 110 | this.resetPagination() |
b1ee8526 | 111 | this.firstSearch = false |
aa55a4da C |
112 | |
113 | this.channelsPerPage = 10 | |
114 | this.search() | |
115 | } | |
b1ee8526 C |
116 | |
117 | this.firstSearch = false | |
57c36b27 C |
118 | }, |
119 | ||
f8b2c1b4 | 120 | err => this.notifier.error(err.message) |
57c36b27 C |
121 | ) |
122 | } | |
123 | ||
124 | onNearOfBottom () { | |
125 | // Last page | |
126 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
127 | ||
128 | this.pagination.currentPage += 1 | |
129 | this.search() | |
130 | } | |
131 | ||
0b18f4aa | 132 | onFiltered () { |
7afea880 | 133 | this.resetPagination() |
0b18f4aa | 134 | |
7afea880 | 135 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
136 | } |
137 | ||
c5d04b4f RK |
138 | numberOfFilters () { |
139 | return this.advancedSearch.size() | |
140 | } | |
141 | ||
3a0fb65c C |
142 | removeVideoFromArray (video: Video) { |
143 | this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id) | |
144 | } | |
145 | ||
7afea880 | 146 | private resetPagination () { |
57c36b27 C |
147 | this.pagination.currentPage = 1 |
148 | this.pagination.totalItems = null | |
aa55a4da | 149 | this.channelsPerPage = 2 |
57c36b27 | 150 | |
26fabbd6 | 151 | this.results = [] |
57c36b27 C |
152 | } |
153 | ||
154 | private updateTitle () { | |
f107470e C |
155 | const suffix = this.currentSearch ? ' ' + this.currentSearch : '' |
156 | this.metaService.setTitle(this.i18n('Search') + suffix) | |
57c36b27 | 157 | } |
0b18f4aa C |
158 | |
159 | private updateUrlFromAdvancedSearch () { | |
47879669 | 160 | const search = this.currentSearch || undefined |
c5d04b4f | 161 | |
0b18f4aa C |
162 | this.router.navigate([], { |
163 | relativeTo: this.route, | |
c5d04b4f | 164 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search }) |
0b18f4aa C |
165 | }) |
166 | } | |
93cae479 C |
167 | |
168 | private getVideosObs () { | |
169 | const params = { | |
170 | search: this.currentSearch, | |
171 | componentPagination: this.pagination, | |
172 | advancedSearch: this.advancedSearch | |
173 | } | |
174 | ||
175 | return this.hooks.wrapObsFun( | |
176 | this.searchService.searchVideos.bind(this.searchService), | |
177 | params, | |
178 | 'common', | |
179 | 'filter:api.search.videos.list.params', | |
180 | 'filter:api.search.videos.list.result' | |
181 | ) | |
182 | } | |
183 | ||
184 | private getVideoChannelObs () { | |
185 | const params = { | |
186 | search: this.currentSearch, | |
187 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }) | |
188 | } | |
189 | ||
190 | return this.hooks.wrapObsFun( | |
191 | this.searchService.searchVideoChannels.bind(this.searchService), | |
192 | params, | |
193 | 'common', | |
194 | 'filter:api.search.video-channels.list.params', | |
195 | 'filter:api.search.video-channels.list.result' | |
196 | ) | |
197 | } | |
57c36b27 | 198 | } |