1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { forkJoin, Subscription } from 'rxjs'
6 import { SearchService } from '@app/search/search.service'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { MetaService } from '@ngx-meta/core'
10 import { AdvancedSearch } from '@app/search/advanced-search.model'
11 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
12 import { immutableAssign } from '@app/shared/misc/utils'
13 import { Video } from '@app/shared/video/video.model'
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 notificationsService: NotificationsService,
44 private searchService: SearchService,
45 private authService: AuthService
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.notificationsService.error('Error', err.text)
76 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
79 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
80 return d instanceof VideoChannel
83 isVideo (v: VideoChannel | Video): v is Video {
84 return v instanceof Video
88 return this.authService.isLoggedIn()
93 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
94 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
97 ([ videosResult, videoChannelsResult ]) => {
98 this.results = this.results
99 .concat(videoChannelsResult.data)
100 .concat(videosResult.videos)
101 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
103 // Focus on channels if there are no enough videos
104 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
105 this.resetPagination()
106 this.firstSearch = false
108 this.channelsPerPage = 10
112 this.firstSearch = false
116 this.notificationsService.error(this.i18n('Error'), error.message)
124 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
126 this.pagination.currentPage += 1
131 this.resetPagination()
133 this.updateUrlFromAdvancedSearch()
137 return this.advancedSearch.size()
140 private resetPagination () {
141 this.pagination.currentPage = 1
142 this.pagination.totalItems = null
143 this.channelsPerPage = 2
148 private updateTitle () {
149 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
152 private updateUrlFromAdvancedSearch () {
153 const search = this.currentSearch || undefined
155 this.router.navigate([], {
156 relativeTo: this.route,
157 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })