]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Log error on unknown hook
[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'
3a0fb65c 3import { AuthService, Notifier } from '@app/core'
f37dc0dd 4import { forkJoin, Subscription } from 'rxjs'
57c36b27
C
5import { SearchService } from '@app/search/search.service'
6import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
7import { I18n } from '@ngx-translate/i18n-polyfill'
57c36b27 8import { MetaService } from '@ngx-meta/core'
0b18f4aa 9import { AdvancedSearch } from '@app/search/advanced-search.model'
f37dc0dd
C
10import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
11import { immutableAssign } from '@app/shared/misc/utils'
26fabbd6 12import { Video } from '@app/shared/video/video.model'
93cae479 13import { HooksService } from '@app/core/plugins/hooks.service'
e8f902c0 14import { PluginService } from '@app/core/plugins/plugin.service'
57c36b27
C
15
16@Component({
17 selector: 'my-search',
18 styleUrls: [ './search.component.scss' ],
19 templateUrl: './search.component.html'
20})
21export class SearchComponent implements OnInit, OnDestroy {
26fabbd6 22 results: (Video | VideoChannel)[] = []
f37dc0dd 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
c5d04b4f 34 private isInitialLoad = false // set to false to show the search filters on first arrival
b1ee8526 35 private firstSearch = true
57c36b27 36
f37dc0dd
C
37 private channelsPerPage = 2
38
57c36b27
C
39 constructor (
40 private i18n: I18n,
41 private route: ActivatedRoute,
0b18f4aa 42 private router: Router,
57c36b27 43 private metaService: MetaService,
f8b2c1b4 44 private notifier: Notifier,
26fabbd6 45 private searchService: SearchService,
93cae479 46 private authService: AuthService,
e8f902c0
C
47 private hooks: HooksService,
48 private pluginService: PluginService
57c36b27
C
49 ) { }
50
e2409062
C
51 get user () {
52 return this.authService.getUser()
53 }
54
57c36b27 55 ngOnInit () {
e8f902c0
C
56 this.pluginService.loadPluginsByScope('search')
57
57c36b27
C
58 this.subActivatedRoute = this.route.queryParams.subscribe(
59 queryParams => {
60 const querySearch = queryParams['search']
61
0b18f4aa 62 // Search updated, reset filters
7afea880
C
63 if (this.currentSearch !== querySearch) {
64 this.resetPagination()
65 this.advancedSearch.reset()
66
47879669 67 this.currentSearch = querySearch || undefined
7afea880
C
68 this.updateTitle()
69 }
70
71 this.advancedSearch = new AdvancedSearch(queryParams)
0b18f4aa 72
7afea880
C
73 // Don't hide filters if we have some of them AND the user just came on the webpage
74 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
75 this.isInitialLoad = false
57c36b27 76
7afea880 77 this.search()
57c36b27
C
78 },
79
f8b2c1b4 80 err => this.notifier.error(err.text)
57c36b27 81 )
7663e55a
C
82
83 this.hooks.runAction('action:search.init')
57c36b27
C
84 }
85
86 ngOnDestroy () {
87 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
88 }
89
26fabbd6
C
90 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
91 return d instanceof VideoChannel
92 }
93
94 isVideo (v: VideoChannel | Video): v is Video {
95 return v instanceof Video
96 }
97
98 isUserLoggedIn () {
99 return this.authService.isLoggedIn()
100 }
101
57c36b27 102 search () {
f37dc0dd 103 forkJoin([
93cae479
C
104 this.getVideosObs(),
105 this.getVideoChannelObs()
f37dc0dd 106 ])
57c36b27 107 .subscribe(
f37dc0dd 108 ([ videosResult, videoChannelsResult ]) => {
26fabbd6
C
109 this.results = this.results
110 .concat(videoChannelsResult.data)
93cae479
C
111 .concat(videosResult.data)
112 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
f37dc0dd 113
b1ee8526 114 // Focus on channels if there are no enough videos
93cae479 115 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
aa55a4da 116 this.resetPagination()
b1ee8526 117 this.firstSearch = false
aa55a4da
C
118
119 this.channelsPerPage = 10
120 this.search()
121 }
b1ee8526
C
122
123 this.firstSearch = false
57c36b27
C
124 },
125
f8b2c1b4 126 err => this.notifier.error(err.message)
57c36b27
C
127 )
128 }
129
130 onNearOfBottom () {
131 // Last page
132 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
133
134 this.pagination.currentPage += 1
135 this.search()
136 }
137
0b18f4aa 138 onFiltered () {
7afea880 139 this.resetPagination()
0b18f4aa 140
7afea880 141 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
142 }
143
c5d04b4f
RK
144 numberOfFilters () {
145 return this.advancedSearch.size()
146 }
147
3a0fb65c
C
148 removeVideoFromArray (video: Video) {
149 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
150 }
151
7afea880 152 private resetPagination () {
57c36b27
C
153 this.pagination.currentPage = 1
154 this.pagination.totalItems = null
aa55a4da 155 this.channelsPerPage = 2
57c36b27 156
26fabbd6 157 this.results = []
57c36b27
C
158 }
159
160 private updateTitle () {
f107470e
C
161 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
162 this.metaService.setTitle(this.i18n('Search') + suffix)
57c36b27 163 }
0b18f4aa
C
164
165 private updateUrlFromAdvancedSearch () {
47879669 166 const search = this.currentSearch || undefined
c5d04b4f 167
0b18f4aa
C
168 this.router.navigate([], {
169 relativeTo: this.route,
c5d04b4f 170 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
171 })
172 }
93cae479
C
173
174 private getVideosObs () {
175 const params = {
176 search: this.currentSearch,
177 componentPagination: this.pagination,
178 advancedSearch: this.advancedSearch
179 }
180
181 return this.hooks.wrapObsFun(
182 this.searchService.searchVideos.bind(this.searchService),
183 params,
e8f902c0 184 'search',
93cae479
C
185 'filter:api.search.videos.list.params',
186 'filter:api.search.videos.list.result'
187 )
188 }
189
190 private getVideoChannelObs () {
191 const params = {
192 search: this.currentSearch,
193 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })
194 }
195
196 return this.hooks.wrapObsFun(
197 this.searchService.searchVideoChannels.bind(this.searchService),
198 params,
e8f902c0 199 'search',
93cae479
C
200 'filter:api.search.video-channels.list.params',
201 'filter:api.search.video-channels.list.result'
202 )
203 }
57c36b27 204}