1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, RedirectService } 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 = true
34 private firstSearch = true
36 private channelsPerPage = 2
40 private route: ActivatedRoute,
41 private router: Router,
42 private metaService: MetaService,
43 private redirectService: RedirectService,
44 private notificationsService: NotificationsService,
45 private searchService: SearchService,
46 private authService: AuthService
50 this.subActivatedRoute = this.route.queryParams.subscribe(
52 const querySearch = queryParams['search']
55 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
57 // Search updated, reset filters
58 if (this.currentSearch !== querySearch) {
59 this.resetPagination()
60 this.advancedSearch.reset()
62 this.currentSearch = querySearch
66 this.advancedSearch = new AdvancedSearch(queryParams)
68 // Don't hide filters if we have some of them AND the user just came on the webpage
69 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
70 this.isInitialLoad = false
75 err => this.notificationsService.error('Error', err.text)
80 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
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
120 this.notificationsService.error(this.i18n('Error'), error.message)
128 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
130 this.pagination.currentPage += 1
135 this.resetPagination()
137 this.updateUrlFromAdvancedSearch()
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 this.router.navigate([], {
154 relativeTo: this.route,
155 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })