1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, Notifier, ServerService } 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,
45 private serverService: ServerService
49 this.subActivatedRoute = this.route.queryParams.subscribe(
51 const querySearch = queryParams['search']
53 // Search updated, reset filters
54 if (this.currentSearch !== querySearch) {
55 this.resetPagination()
56 this.advancedSearch.reset()
58 this.currentSearch = querySearch || undefined
62 this.advancedSearch = new AdvancedSearch(queryParams)
64 // Don't hide filters if we have some of them AND the user just came on the webpage
65 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
66 this.isInitialLoad = false
71 err => this.notifier.error(err.text)
76 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
79 isVideoBlur (video: Video) {
80 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
83 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
84 return d instanceof VideoChannel
87 isVideo (v: VideoChannel | Video): v is Video {
88 return v instanceof Video
92 return this.authService.isLoggedIn()
97 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
98 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
101 ([ videosResult, videoChannelsResult ]) => {
102 this.results = this.results
103 .concat(videoChannelsResult.data)
104 .concat(videosResult.videos)
105 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
107 // Focus on channels if there are no enough videos
108 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
109 this.resetPagination()
110 this.firstSearch = false
112 this.channelsPerPage = 10
116 this.firstSearch = false
119 err => this.notifier.error(err.message)
126 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
128 this.pagination.currentPage += 1
133 this.resetPagination()
135 this.updateUrlFromAdvancedSearch()
139 return this.advancedSearch.size()
142 private resetPagination () {
143 this.pagination.currentPage = 1
144 this.pagination.totalItems = null
145 this.channelsPerPage = 2
150 private updateTitle () {
151 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
152 this.metaService.setTitle(this.i18n('Search') + suffix)
155 private updateUrlFromAdvancedSearch () {
156 const search = this.currentSearch || undefined
158 this.router.navigate([], {
159 relativeTo: this.route,
160 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })