]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Add ability to search video channels
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
CommitLineData
57c36b27 1import { Component, OnDestroy, OnInit } from '@angular/core'
0b18f4aa 2import { ActivatedRoute, Router } from '@angular/router'
57c36b27
C
3import { RedirectService } from '@app/core'
4import { NotificationsService } from 'angular2-notifications'
f37dc0dd 5import { forkJoin, Subscription } from 'rxjs'
57c36b27
C
6import { SearchService } from '@app/search/search.service'
7import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8import { I18n } from '@ngx-translate/i18n-polyfill'
9import { Video } from '../../../../shared'
10import { MetaService } from '@ngx-meta/core'
0b18f4aa 11import { AdvancedSearch } from '@app/search/advanced-search.model'
f37dc0dd
C
12import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13import { immutableAssign } from '@app/shared/misc/utils'
57c36b27
C
14
15@Component({
16 selector: 'my-search',
17 styleUrls: [ './search.component.scss' ],
18 templateUrl: './search.component.html'
19})
20export class SearchComponent implements OnInit, OnDestroy {
21 videos: Video[] = []
f37dc0dd
C
22 videoChannels: VideoChannel[] = []
23
57c36b27
C
24 pagination: ComponentPagination = {
25 currentPage: 1,
f37dc0dd 26 itemsPerPage: 10, // Only for videos, use another variable for channels
57c36b27
C
27 totalItems: null
28 }
0b18f4aa
C
29 advancedSearch: AdvancedSearch = new AdvancedSearch()
30 isSearchFilterCollapsed = true
f37dc0dd 31 currentSearch: string
57c36b27
C
32
33 private subActivatedRoute: Subscription
7afea880 34 private isInitialLoad = true
57c36b27 35
f37dc0dd
C
36 private channelsPerPage = 2
37
57c36b27
C
38 constructor (
39 private i18n: I18n,
40 private route: ActivatedRoute,
0b18f4aa 41 private router: Router,
57c36b27
C
42 private metaService: MetaService,
43 private redirectService: RedirectService,
44 private notificationsService: NotificationsService,
45 private searchService: SearchService
46 ) { }
47
48 ngOnInit () {
49 this.subActivatedRoute = this.route.queryParams.subscribe(
50 queryParams => {
51 const querySearch = queryParams['search']
52
4278710d
C
53 // New empty search
54 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
57c36b27 55
0b18f4aa 56 // Search updated, reset filters
7afea880
C
57 if (this.currentSearch !== querySearch) {
58 this.resetPagination()
59 this.advancedSearch.reset()
60
61 this.currentSearch = querySearch
62 this.updateTitle()
63 }
64
65 this.advancedSearch = new AdvancedSearch(queryParams)
0b18f4aa 66
7afea880
C
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
57c36b27 70
7afea880 71 this.search()
57c36b27
C
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
76 }
77
78 ngOnDestroy () {
79 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
80 }
81
82 search () {
f37dc0dd
C
83 forkJoin([
84 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
85 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
86 ])
57c36b27 87 .subscribe(
f37dc0dd
C
88 ([ videosResult, videoChannelsResult ]) => {
89 this.videos = this.videos.concat(videosResult.videos)
90 this.pagination.totalItems = videosResult.totalVideos
91
92 this.videoChannels = videoChannelsResult.data
57c36b27
C
93 },
94
95 error => {
96 this.notificationsService.error(this.i18n('Error'), error.message)
97 }
98 )
f37dc0dd 99
57c36b27
C
100 }
101
102 onNearOfBottom () {
103 // Last page
104 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
105
106 this.pagination.currentPage += 1
107 this.search()
108 }
109
0b18f4aa 110 onFiltered () {
7afea880 111 this.resetPagination()
0b18f4aa 112
7afea880 113 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
114 }
115
7afea880 116 private resetPagination () {
57c36b27
C
117 this.pagination.currentPage = 1
118 this.pagination.totalItems = null
119
120 this.videos = []
57c36b27
C
121 }
122
123 private updateTitle () {
124 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
125 }
0b18f4aa
C
126
127 private updateUrlFromAdvancedSearch () {
128 this.router.navigate([], {
129 relativeTo: this.route,
130 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
131 })
132 }
57c36b27 133}