]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
Merge branch 'develop' into pr/1217
[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, ServerService } 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 private serverService: ServerService
46 ) { }
47
48 ngOnInit () {
49 this.subActivatedRoute = this.route.queryParams.subscribe(
50 queryParams => {
51 const querySearch = queryParams['search']
52
53 // Search updated, reset filters
54 if (this.currentSearch !== querySearch) {
55 this.resetPagination()
56 this.advancedSearch.reset()
57
58 this.currentSearch = querySearch || undefined
59 this.updateTitle()
60 }
61
62 this.advancedSearch = new AdvancedSearch(queryParams)
63
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
67
68 this.search()
69 },
70
71 err => this.notifier.error(err.text)
72 )
73 }
74
75 ngOnDestroy () {
76 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
77 }
78
79 isVideoBlur (video: Video) {
80 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
81 }
82
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
95 search () {
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 ])
100 .subscribe(
101 ([ videosResult, videoChannelsResult ]) => {
102 this.results = this.results
103 .concat(videoChannelsResult.data)
104 .concat(videosResult.videos)
105 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
106
107 // Focus on channels if there are no enough videos
108 if (this.firstSearch === true && videosResult.videos.length < this.pagination.itemsPerPage) {
109 this.resetPagination()
110 this.firstSearch = false
111
112 this.channelsPerPage = 10
113 this.search()
114 }
115
116 this.firstSearch = false
117 },
118
119 err => this.notifier.error(err.message)
120 )
121
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
132 onFiltered () {
133 this.resetPagination()
134
135 this.updateUrlFromAdvancedSearch()
136 }
137
138 numberOfFilters () {
139 return this.advancedSearch.size()
140 }
141
142 private resetPagination () {
143 this.pagination.currentPage = 1
144 this.pagination.totalItems = null
145 this.channelsPerPage = 2
146
147 this.results = []
148 }
149
150 private updateTitle () {
151 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
152 this.metaService.setTitle(this.i18n('Search') + suffix)
153 }
154
155 private updateUrlFromAdvancedSearch () {
156 const search = this.currentSearch || undefined
157
158 this.router.navigate([], {
159 relativeTo: this.route,
160 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
161 })
162 }
163 }