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, 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'
14 import { PluginService } from '@app/core/plugins/plugin.service'
17 selector: 'my-search',
18 styleUrls: [ './search.component.scss' ],
19 templateUrl: './search.component.html'
21 export class SearchComponent implements OnInit, OnDestroy {
22 results: (Video | VideoChannel)[] = []
24 pagination: ComponentPagination = {
26 itemsPerPage: 10, // Only for videos, use another variable for channels
29 advancedSearch: AdvancedSearch = new AdvancedSearch()
30 isSearchFilterCollapsed = true
33 private subActivatedRoute: Subscription
34 private isInitialLoad = false // set to false to show the search filters on first arrival
35 private firstSearch = true
37 private channelsPerPage = 2
41 private route: ActivatedRoute,
42 private router: Router,
43 private metaService: MetaService,
44 private notifier: Notifier,
45 private searchService: SearchService,
46 private authService: AuthService,
47 private hooks: HooksService,
48 private pluginService: PluginService
52 return this.authService.getUser()
56 this.pluginService.loadPluginsByScope('search')
58 this.subActivatedRoute = this.route.queryParams.subscribe(
60 const querySearch = queryParams['search']
62 // Search updated, reset filters
63 if (this.currentSearch !== querySearch) {
64 this.resetPagination()
65 this.advancedSearch.reset()
67 this.currentSearch = querySearch || undefined
71 this.advancedSearch = new AdvancedSearch(queryParams)
73 // Don't hide filters if we have some of them AND the user just came on the webpage
74 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
75 this.isInitialLoad = false
80 err => this.notifier.error(err.text)
83 this.hooks.runAction('action:search.init')
87 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
90 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
91 return d instanceof VideoChannel
94 isVideo (v: VideoChannel | Video): v is Video {
95 return v instanceof Video
99 return this.authService.isLoggedIn()
105 this.getVideoChannelObs()
108 ([ videosResult, videoChannelsResult ]) => {
109 this.results = this.results
110 .concat(videoChannelsResult.data)
111 .concat(videosResult.data)
112 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
114 // Focus on channels if there are no enough videos
115 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
116 this.resetPagination()
117 this.firstSearch = false
119 this.channelsPerPage = 10
123 this.firstSearch = false
126 err => this.notifier.error(err.message)
132 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
134 this.pagination.currentPage += 1
139 this.resetPagination()
141 this.updateUrlFromAdvancedSearch()
145 return this.advancedSearch.size()
148 removeVideoFromArray (video: Video) {
149 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
152 private resetPagination () {
153 this.pagination.currentPage = 1
154 this.pagination.totalItems = null
155 this.channelsPerPage = 2
160 private updateTitle () {
161 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
162 this.metaService.setTitle(this.i18n('Search') + suffix)
165 private updateUrlFromAdvancedSearch () {
166 const search = this.currentSearch || undefined
168 this.router.navigate([], {
169 relativeTo: this.route,
170 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
174 private getVideosObs () {
176 search: this.currentSearch,
177 componentPagination: this.pagination,
178 advancedSearch: this.advancedSearch
181 return this.hooks.wrapObsFun(
182 this.searchService.searchVideos.bind(this.searchService),
185 'filter:api.search.videos.list.params',
186 'filter:api.search.videos.list.result'
190 private getVideoChannelObs () {
192 search: this.currentSearch,
193 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })
196 return this.hooks.wrapObsFun(
197 this.searchService.searchVideoChannels.bind(this.searchService),
200 'filter:api.search.video-channels.list.params',
201 'filter:api.search.video-channels.list.result'