]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
474b728244867b58c6dfacac9e128cb087d060ad
[github/Chocobozzz/PeerTube.git] / client / src / app / search / search.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { AuthService, Notifier } 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'
13
14 @Component({
15 selector: 'my-search',
16 styleUrls: [ './search.component.scss' ],
17 templateUrl: './search.component.html'
18 })
19 export class SearchComponent implements OnInit, OnDestroy {
20 results: (Video | VideoChannel)[] = []
21
22 pagination: ComponentPagination = {
23 currentPage: 1,
24 itemsPerPage: 10, // Only for videos, use another variable for channels
25 totalItems: null
26 }
27 advancedSearch: AdvancedSearch = new AdvancedSearch()
28 isSearchFilterCollapsed = true
29 currentSearch: string
30
31 private subActivatedRoute: Subscription
32 private isInitialLoad = false // set to false to show the search filters on first arrival
33 private firstSearch = true
34
35 private channelsPerPage = 2
36
37 constructor (
38 private i18n: I18n,
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 ) { }
46
47 ngOnInit () {
48 this.subActivatedRoute = this.route.queryParams.subscribe(
49 queryParams => {
50 const querySearch = queryParams['search']
51
52 // Search updated, reset filters
53 if (this.currentSearch !== querySearch) {
54 this.resetPagination()
55 this.advancedSearch.reset()
56
57 this.currentSearch = querySearch || undefined
58 this.updateTitle()
59 }
60
61 this.advancedSearch = new AdvancedSearch(queryParams)
62
63 // Don't hide filters if we have some of them AND the user just came on the webpage
64 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
65 this.isInitialLoad = false
66
67 this.search()
68 },
69
70 err => this.notifier.error(err.text)
71 )
72 }
73
74 ngOnDestroy () {
75 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
76 }
77
78 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
79 return d instanceof VideoChannel
80 }
81
82 isVideo (v: VideoChannel | Video): v is Video {
83 return v instanceof Video
84 }
85
86 isUserLoggedIn () {
87 return this.authService.isLoggedIn()
88 }
89
90 search () {
91 forkJoin([
92 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
93 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
94 ])
95 .subscribe(
96 ([ videosResult, videoChannelsResult ]) => {
97 this.results = this.results
98 .concat(videoChannelsResult.data)
99 .concat(videosResult.videos)
100 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
101
102 // Focus on channels if there are no enough videos
103 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
104 this.resetPagination()
105 this.firstSearch = false
106
107 this.channelsPerPage = 10
108 this.search()
109 }
110
111 this.firstSearch = false
112 },
113
114 err => this.notifier.error(err.message)
115 )
116
117 }
118
119 onNearOfBottom () {
120 // Last page
121 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
122
123 this.pagination.currentPage += 1
124 this.search()
125 }
126
127 onFiltered () {
128 this.resetPagination()
129
130 this.updateUrlFromAdvancedSearch()
131 }
132
133 numberOfFilters () {
134 return this.advancedSearch.size()
135 }
136
137 private resetPagination () {
138 this.pagination.currentPage = 1
139 this.pagination.totalItems = null
140 this.channelsPerPage = 2
141
142 this.results = []
143 }
144
145 private updateTitle () {
146 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
147 this.metaService.setTitle(this.i18n('Search') + suffix)
148 }
149
150 private updateUrlFromAdvancedSearch () {
151 const search = this.currentSearch || undefined
152
153 this.router.navigate([], {
154 relativeTo: this.route,
155 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
156 })
157 }
158 }