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