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
35 private channelsPerPage = 2
39 private route: ActivatedRoute,
40 private router: Router,
41 private metaService: MetaService,
42 private redirectService: RedirectService,
43 private notificationsService: NotificationsService,
44 private searchService: SearchService,
45 private authService: AuthService
49 this.subActivatedRoute = this.route.queryParams.subscribe(
51 const querySearch = queryParams['search']
54 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
56 // Search updated, reset filters
57 if (this.currentSearch !== querySearch) {
58 this.resetPagination()
59 this.advancedSearch.reset()
61 this.currentSearch = querySearch
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.notificationsService.error('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
107 if (this.channelsPerPage !== 10 && videosResult.videos.length < this.pagination.itemsPerPage) {
108 this.resetPagination()
110 this.channelsPerPage = 10
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()
136 private resetPagination () {
137 this.pagination.currentPage = 1
138 this.pagination.totalItems = null
139 this.channelsPerPage = 2
144 private updateTitle () {
145 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
148 private updateUrlFromAdvancedSearch () {
149 this.router.navigate([], {
150 relativeTo: this.route,
151 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })