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 return this.authService.getUser()
52 this.subActivatedRoute = this.route.queryParams.subscribe(
54 const querySearch = queryParams['search']
56 // Search updated, reset filters
57 if (this.currentSearch !== querySearch) {
58 this.resetPagination()
59 this.advancedSearch.reset()
61 this.currentSearch = querySearch || undefined
65 this.advancedSearch = new AdvancedSearch(queryParams)
67 // Don't hide filters if we have some of them AND the user just came on the webpage
68 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
69 this.isInitialLoad = false
74 err => this.notifier.error(err.text)
79 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
82 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
83 return d instanceof VideoChannel
86 isVideo (v: VideoChannel | Video): v is Video {
87 return v instanceof Video
91 return this.authService.isLoggedIn()
96 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
97 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
100 ([ videosResult, videoChannelsResult ]) => {
101 this.results = this.results
102 .concat(videoChannelsResult.data)
103 .concat(videosResult.videos)
104 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
106 // Focus on channels if there are no enough videos
107 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
108 this.resetPagination()
109 this.firstSearch = false
111 this.channelsPerPage = 10
115 this.firstSearch = false
118 err => this.notifier.error(err.message)
125 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
127 this.pagination.currentPage += 1
132 this.resetPagination()
134 this.updateUrlFromAdvancedSearch()
138 return this.advancedSearch.size()
141 removeVideoFromArray (video: Video) {
142 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
145 private resetPagination () {
146 this.pagination.currentPage = 1
147 this.pagination.totalItems = null
148 this.channelsPerPage = 2
153 private updateTitle () {
154 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
155 this.metaService.setTitle(this.i18n('Search') + suffix)
158 private updateUrlFromAdvancedSearch () {
159 const search = this.currentSearch || undefined
161 this.router.navigate([], {
162 relativeTo: this.route,
163 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })