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'
15 selector: 'my-search',
16 styleUrls: [ './search.component.scss' ],
17 templateUrl: './search.component.html'
19 export class SearchComponent implements OnInit, OnDestroy {
20 results: (Video | VideoChannel)[] = []
22 pagination: ComponentPagination = {
24 itemsPerPage: 10, // Only for videos, use another variable for channels
27 advancedSearch: AdvancedSearch = new AdvancedSearch()
28 isSearchFilterCollapsed = true
31 private subActivatedRoute: Subscription
32 private isInitialLoad = false // set to false to show the search filters on first arrival
33 private firstSearch = true
35 private channelsPerPage = 2
39 private route: ActivatedRoute,
40 private router: Router,
41 private metaService: MetaService,
42 private notifier: Notifier,
43 private searchService: SearchService,
44 private authService: AuthService
48 this.subActivatedRoute = this.route.queryParams.subscribe(
50 const querySearch = queryParams['search']
52 // Search updated, reset filters
53 if (this.currentSearch !== querySearch) {
54 this.resetPagination()
55 this.advancedSearch.reset()
57 this.currentSearch = querySearch || undefined
61 this.advancedSearch = new AdvancedSearch(queryParams)
63 // Don't hide filters if we have some of them AND the user just came on the webpage
64 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
65 this.isInitialLoad = false
70 err => this.notifier.error(err.text)
75 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
78 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
79 return d instanceof VideoChannel
82 isVideo (v: VideoChannel | Video): v is Video {
83 return v instanceof Video
87 return this.authService.isLoggedIn()
92 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
93 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
96 ([ videosResult, videoChannelsResult ]) => {
97 this.results = this.results
98 .concat(videoChannelsResult.data)
99 .concat(videosResult.videos)
100 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
102 // Focus on channels if there are no enough videos
103 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
104 this.resetPagination()
105 this.firstSearch = false
107 this.channelsPerPage = 10
111 this.firstSearch = false
114 err => this.notifier.error(err.message)
121 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
123 this.pagination.currentPage += 1
128 this.resetPagination()
130 this.updateUrlFromAdvancedSearch()
134 return this.advancedSearch.size()
137 private resetPagination () {
138 this.pagination.currentPage = 1
139 this.pagination.totalItems = null
140 this.channelsPerPage = 2
145 private updateTitle () {
146 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
147 this.metaService.setTitle(this.i18n('Search') + suffix)
150 private updateUrlFromAdvancedSearch () {
151 const search = this.currentSearch || undefined
153 this.router.navigate([], {
154 relativeTo: this.route,
155 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })