]>
Commit | Line | Data |
---|---|---|
5fb2e288 | 1 | import { forkJoin, of, Subscription } from 'rxjs' |
57c36b27 | 2 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0b18f4aa | 3 | import { ActivatedRoute, Router } from '@angular/router' |
5fb2e288 C |
4 | import { AuthService, Notifier, ServerService } from '@app/core' |
5 | import { HooksService } from '@app/core/plugins/hooks.service' | |
6 | import { AdvancedSearch } from '@app/search/advanced-search.model' | |
57c36b27 | 7 | import { SearchService } from '@app/search/search.service' |
5fb2e288 | 8 | import { immutableAssign } from '@app/shared/misc/utils' |
57c36b27 | 9 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' |
f37dc0dd | 10 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' |
26fabbd6 | 11 | import { Video } from '@app/shared/video/video.model' |
5fb2e288 C |
12 | import { MetaService } from '@ngx-meta/core' |
13 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
14 | import { ServerConfig } from '@shared/models' | |
15 | import { UserService } from '@app/shared' | |
7c87746e | 16 | import { SearchTargetType } from '@shared/models/search/search-target-query.model' |
57c36b27 C |
17 | |
18 | @Component({ | |
19 | selector: 'my-search', | |
20 | styleUrls: [ './search.component.scss' ], | |
21 | templateUrl: './search.component.html' | |
22 | }) | |
23 | export class SearchComponent implements OnInit, OnDestroy { | |
26fabbd6 | 24 | results: (Video | VideoChannel)[] = [] |
f37dc0dd | 25 | |
57c36b27 C |
26 | pagination: ComponentPagination = { |
27 | currentPage: 1, | |
f37dc0dd | 28 | itemsPerPage: 10, // Only for videos, use another variable for channels |
57c36b27 C |
29 | totalItems: null |
30 | } | |
0b18f4aa C |
31 | advancedSearch: AdvancedSearch = new AdvancedSearch() |
32 | isSearchFilterCollapsed = true | |
f37dc0dd | 33 | currentSearch: string |
57c36b27 | 34 | |
5fb2e288 C |
35 | errorMessage: string |
36 | serverConfig: ServerConfig | |
37 | ||
57c36b27 | 38 | private subActivatedRoute: Subscription |
c5d04b4f | 39 | private isInitialLoad = false // set to false to show the search filters on first arrival |
b1ee8526 | 40 | private firstSearch = true |
57c36b27 | 41 | |
f37dc0dd C |
42 | private channelsPerPage = 2 |
43 | ||
7c87746e C |
44 | private lastSearchTarget: SearchTargetType |
45 | ||
57c36b27 C |
46 | constructor ( |
47 | private i18n: I18n, | |
48 | private route: ActivatedRoute, | |
0b18f4aa | 49 | private router: Router, |
57c36b27 | 50 | private metaService: MetaService, |
f8b2c1b4 | 51 | private notifier: Notifier, |
26fabbd6 | 52 | private searchService: SearchService, |
93cae479 | 53 | private authService: AuthService, |
5fb2e288 C |
54 | private hooks: HooksService, |
55 | private serverService: ServerService | |
57c36b27 C |
56 | ) { } |
57 | ||
e2409062 C |
58 | get user () { |
59 | return this.authService.getUser() | |
60 | } | |
61 | ||
57c36b27 | 62 | ngOnInit () { |
5fb2e288 C |
63 | this.serverService.getConfig() |
64 | .subscribe(config => this.serverConfig = config) | |
65 | ||
57c36b27 | 66 | this.subActivatedRoute = this.route.queryParams.subscribe( |
5fb2e288 | 67 | async queryParams => { |
57c36b27 | 68 | const querySearch = queryParams['search'] |
7c87746e | 69 | const searchTarget = queryParams['searchTarget'] |
57c36b27 | 70 | |
0b18f4aa | 71 | // Search updated, reset filters |
7c87746e | 72 | if (this.currentSearch !== querySearch || searchTarget !== this.advancedSearch.searchTarget) { |
7afea880 C |
73 | this.resetPagination() |
74 | this.advancedSearch.reset() | |
75 | ||
47879669 | 76 | this.currentSearch = querySearch || undefined |
7afea880 C |
77 | this.updateTitle() |
78 | } | |
79 | ||
80 | this.advancedSearch = new AdvancedSearch(queryParams) | |
5fb2e288 C |
81 | if (!this.advancedSearch.searchTarget) { |
82 | this.advancedSearch.searchTarget = await this.serverService.getDefaultSearchTarget() | |
83 | } | |
0b18f4aa | 84 | |
7afea880 C |
85 | // Don't hide filters if we have some of them AND the user just came on the webpage |
86 | this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues() | |
87 | this.isInitialLoad = false | |
57c36b27 | 88 | |
7afea880 | 89 | this.search() |
57c36b27 C |
90 | }, |
91 | ||
f8b2c1b4 | 92 | err => this.notifier.error(err.text) |
57c36b27 | 93 | ) |
7663e55a | 94 | |
c9e3eeed | 95 | this.hooks.runAction('action:search.init', 'search') |
57c36b27 C |
96 | } |
97 | ||
98 | ngOnDestroy () { | |
99 | if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe() | |
100 | } | |
101 | ||
26fabbd6 C |
102 | isVideoChannel (d: VideoChannel | Video): d is VideoChannel { |
103 | return d instanceof VideoChannel | |
104 | } | |
105 | ||
106 | isVideo (v: VideoChannel | Video): v is Video { | |
107 | return v instanceof Video | |
108 | } | |
109 | ||
110 | isUserLoggedIn () { | |
111 | return this.authService.isLoggedIn() | |
112 | } | |
113 | ||
57c36b27 | 114 | search () { |
f37dc0dd | 115 | forkJoin([ |
93cae479 C |
116 | this.getVideosObs(), |
117 | this.getVideoChannelObs() | |
5fb2e288 C |
118 | ]).subscribe( |
119 | ([videosResult, videoChannelsResult]) => { | |
120 | this.results = this.results | |
121 | .concat(videoChannelsResult.data) | |
122 | .concat(videosResult.data) | |
123 | ||
124 | this.pagination.totalItems = videosResult.total + videoChannelsResult.total | |
7c87746e | 125 | this.lastSearchTarget = this.advancedSearch.searchTarget |
b1ee8526 | 126 | |
5fb2e288 C |
127 | // Focus on channels if there are no enough videos |
128 | if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) { | |
129 | this.resetPagination() | |
b1ee8526 | 130 | this.firstSearch = false |
57c36b27 | 131 | |
5fb2e288 C |
132 | this.channelsPerPage = 10 |
133 | this.search() | |
134 | } | |
135 | ||
136 | this.firstSearch = false | |
137 | }, | |
138 | ||
139 | err => { | |
140 | if (this.advancedSearch.searchTarget !== 'search-index') this.notifier.error(err.message) | |
141 | ||
142 | this.notifier.error( | |
143 | this.i18n('Search index is unavailable. Retrying with instance results instead.'), | |
144 | this.i18n('Search error') | |
145 | ) | |
146 | this.advancedSearch.searchTarget = 'local' | |
147 | this.search() | |
148 | } | |
149 | ) | |
57c36b27 C |
150 | } |
151 | ||
152 | onNearOfBottom () { | |
153 | // Last page | |
154 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
155 | ||
156 | this.pagination.currentPage += 1 | |
157 | this.search() | |
158 | } | |
159 | ||
0b18f4aa | 160 | onFiltered () { |
7afea880 | 161 | this.resetPagination() |
0b18f4aa | 162 | |
7afea880 | 163 | this.updateUrlFromAdvancedSearch() |
0b18f4aa C |
164 | } |
165 | ||
c5d04b4f RK |
166 | numberOfFilters () { |
167 | return this.advancedSearch.size() | |
168 | } | |
169 | ||
be27ef3b C |
170 | // Add VideoChannel for typings, but the template already checks "video" argument is a video |
171 | removeVideoFromArray (video: Video | VideoChannel) { | |
3a0fb65c C |
172 | this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id) |
173 | } | |
174 | ||
5fb2e288 C |
175 | getChannelUrl (channel: VideoChannel) { |
176 | if (this.advancedSearch.searchTarget === 'search-index' && channel.url) { | |
177 | const remoteUriConfig = this.serverConfig.search.remoteUri | |
178 | ||
179 | // Redirect on the external instance if not allowed to fetch remote data | |
180 | const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users | |
181 | const fromPath = window.location.pathname + window.location.search | |
182 | ||
183 | return [ '/search/lazy-load-channel', { url: channel.url, externalRedirect, fromPath } ] | |
184 | } | |
185 | ||
186 | return [ '/video-channels', channel.nameWithHost ] | |
187 | } | |
188 | ||
189 | hideActions () { | |
7c87746e | 190 | return this.lastSearchTarget === 'search-index' |
5fb2e288 C |
191 | } |
192 | ||
7afea880 | 193 | private resetPagination () { |
57c36b27 C |
194 | this.pagination.currentPage = 1 |
195 | this.pagination.totalItems = null | |
aa55a4da | 196 | this.channelsPerPage = 2 |
57c36b27 | 197 | |
26fabbd6 | 198 | this.results = [] |
57c36b27 C |
199 | } |
200 | ||
201 | private updateTitle () { | |
f107470e C |
202 | const suffix = this.currentSearch ? ' ' + this.currentSearch : '' |
203 | this.metaService.setTitle(this.i18n('Search') + suffix) | |
57c36b27 | 204 | } |
0b18f4aa C |
205 | |
206 | private updateUrlFromAdvancedSearch () { | |
47879669 | 207 | const search = this.currentSearch || undefined |
c5d04b4f | 208 | |
0b18f4aa C |
209 | this.router.navigate([], { |
210 | relativeTo: this.route, | |
c5d04b4f | 211 | queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search }) |
0b18f4aa C |
212 | }) |
213 | } | |
93cae479 C |
214 | |
215 | private getVideosObs () { | |
216 | const params = { | |
217 | search: this.currentSearch, | |
218 | componentPagination: this.pagination, | |
219 | advancedSearch: this.advancedSearch | |
220 | } | |
221 | ||
222 | return this.hooks.wrapObsFun( | |
223 | this.searchService.searchVideos.bind(this.searchService), | |
224 | params, | |
e8f902c0 | 225 | 'search', |
93cae479 C |
226 | 'filter:api.search.videos.list.params', |
227 | 'filter:api.search.videos.list.result' | |
228 | ) | |
229 | } | |
230 | ||
231 | private getVideoChannelObs () { | |
44df5c75 C |
232 | if (!this.currentSearch) return of({ data: [], total: 0 }) |
233 | ||
93cae479 C |
234 | const params = { |
235 | search: this.currentSearch, | |
5fb2e288 C |
236 | componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }), |
237 | searchTarget: this.advancedSearch.searchTarget | |
93cae479 C |
238 | } |
239 | ||
240 | return this.hooks.wrapObsFun( | |
241 | this.searchService.searchVideoChannels.bind(this.searchService), | |
242 | params, | |
e8f902c0 | 243 | 'search', |
93cae479 C |
244 | 'filter:api.search.video-channels.list.params', |
245 | 'filter:api.search.video-channels.list.result' | |
246 | ) | |
247 | } | |
57c36b27 | 248 | } |