1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, Notifier } from '@app/core'
4 import { forkJoin, of, Subscription } from 'rxjs'
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'
8 import { MetaService } from '@ngx-meta/core'
9 import { AdvancedSearch } from '@app/search/advanced-search.model'
10 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
11 import { immutableAssign } from '@app/shared/misc/utils'
12 import { Video } from '@app/shared/video/video.model'
13 import { HooksService } from '@app/core/plugins/hooks.service'
16 selector: 'my-search',
17 styleUrls: [ './search.component.scss' ],
18 templateUrl: './search.component.html'
20 export class SearchComponent implements OnInit, OnDestroy {
21 results: (Video | VideoChannel)[] = []
23 pagination: ComponentPagination = {
25 itemsPerPage: 10, // Only for videos, use another variable for channels
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
32 private subActivatedRoute: Subscription
33 private isInitialLoad = false // set to false to show the search filters on first arrival
34 private firstSearch = true
36 private channelsPerPage = 2
40 private route: ActivatedRoute,
41 private router: Router,
42 private metaService: MetaService,
43 private notifier: Notifier,
44 private searchService: SearchService,
45 private authService: AuthService,
46 private hooks: HooksService
50 return this.authService.getUser()
54 this.subActivatedRoute = this.route.queryParams.subscribe(
56 const querySearch = queryParams['search']
58 // Search updated, reset filters
59 if (this.currentSearch !== querySearch) {
60 this.resetPagination()
61 this.advancedSearch.reset()
63 this.currentSearch = querySearch || undefined
67 this.advancedSearch = new AdvancedSearch(queryParams)
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
76 err => this.notifier.error(err.text)
79 this.hooks.runAction('action:search.init', 'search')
83 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
86 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
87 return d instanceof VideoChannel
90 isVideo (v: VideoChannel | Video): v is Video {
91 return v instanceof Video
95 return this.authService.isLoggedIn()
101 this.getVideoChannelObs()
104 ([ videosResult, videoChannelsResult ]) => {
105 this.results = this.results
106 .concat(videoChannelsResult.data)
107 .concat(videosResult.data)
108 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
110 // Focus on channels if there are no enough videos
111 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
112 this.resetPagination()
113 this.firstSearch = false
115 this.channelsPerPage = 10
119 this.firstSearch = false
122 err => this.notifier.error(err.message)
128 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
130 this.pagination.currentPage += 1
135 this.resetPagination()
137 this.updateUrlFromAdvancedSearch()
141 return this.advancedSearch.size()
144 removeVideoFromArray (video: Video) {
145 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
148 private resetPagination () {
149 this.pagination.currentPage = 1
150 this.pagination.totalItems = null
151 this.channelsPerPage = 2
156 private updateTitle () {
157 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
158 this.metaService.setTitle(this.i18n('Search') + suffix)
161 private updateUrlFromAdvancedSearch () {
162 const search = this.currentSearch || undefined
164 this.router.navigate([], {
165 relativeTo: this.route,
166 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
170 private getVideosObs () {
172 search: this.currentSearch,
173 componentPagination: this.pagination,
174 advancedSearch: this.advancedSearch
177 return this.hooks.wrapObsFun(
178 this.searchService.searchVideos.bind(this.searchService),
181 'filter:api.search.videos.list.params',
182 'filter:api.search.videos.list.result'
186 private getVideoChannelObs () {
187 if (!this.currentSearch) return of({ data: [], total: 0 })
190 search: this.currentSearch,
191 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })
194 return this.hooks.wrapObsFun(
195 this.searchService.searchVideoChannels.bind(this.searchService),
198 'filter:api.search.video-channels.list.params',
199 'filter:api.search.video-channels.list.result'