]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Add tests regarding well known/static text endpoints
[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'
26fabbd6 3import { AuthService, RedirectService } from '@app/core'
57c36b27 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'
57c36b27 9import { MetaService } from '@ngx-meta/core'
0b18f4aa 10import { AdvancedSearch } from '@app/search/advanced-search.model'
f37dc0dd
C
11import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
12import { immutableAssign } from '@app/shared/misc/utils'
26fabbd6 13import { Video } from '@app/shared/video/video.model'
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 {
26fabbd6 21 results: (Video | VideoChannel)[] = []
f37dc0dd 22
57c36b27
C
23 pagination: ComponentPagination = {
24 currentPage: 1,
f37dc0dd 25 itemsPerPage: 10, // Only for videos, use another variable for channels
57c36b27
C
26 totalItems: null
27 }
0b18f4aa
C
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
f37dc0dd 30 currentSearch: string
57c36b27
C
31
32 private subActivatedRoute: Subscription
c5d04b4f 33 private isInitialLoad = false // set to false to show the search filters on first arrival
b1ee8526 34 private firstSearch = 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,
26fabbd6
C
45 private searchService: SearchService,
46 private authService: AuthService
57c36b27
C
47 ) { }
48
49 ngOnInit () {
50 this.subActivatedRoute = this.route.queryParams.subscribe(
51 queryParams => {
52 const querySearch = queryParams['search']
53
4278710d
C
54 // New empty search
55 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
57c36b27 56
0b18f4aa 57 // Search updated, reset filters
7afea880
C
58 if (this.currentSearch !== querySearch) {
59 this.resetPagination()
60 this.advancedSearch.reset()
61
62 this.currentSearch = querySearch
63 this.updateTitle()
64 }
65
66 this.advancedSearch = new AdvancedSearch(queryParams)
0b18f4aa 67
7afea880
C
68 // Don't hide filters if we have some of them AND the user just came on the webpage
69 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
70 this.isInitialLoad = false
57c36b27 71
7afea880 72 this.search()
57c36b27
C
73 },
74
75 err => this.notificationsService.error('Error', err.text)
76 )
77 }
78
79 ngOnDestroy () {
80 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
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
119 error => {
120 this.notificationsService.error(this.i18n('Error'), error.message)
121 }
122 )
f37dc0dd 123
57c36b27
C
124 }
125
126 onNearOfBottom () {
127 // Last page
128 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
129
130 this.pagination.currentPage += 1
131 this.search()
132 }
133
0b18f4aa 134 onFiltered () {
7afea880 135 this.resetPagination()
0b18f4aa 136
7afea880 137 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
138 }
139
c5d04b4f
RK
140 numberOfFilters () {
141 return this.advancedSearch.size()
142 }
143
7afea880 144 private resetPagination () {
57c36b27
C
145 this.pagination.currentPage = 1
146 this.pagination.totalItems = null
aa55a4da 147 this.channelsPerPage = 2
57c36b27 148
26fabbd6 149 this.results = []
57c36b27
C
150 }
151
152 private updateTitle () {
153 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
154 }
0b18f4aa
C
155
156 private updateUrlFromAdvancedSearch () {
c5d04b4f
RK
157 const search = (this.currentSearch && this.currentSearch !== '') ? this.currentSearch : undefined
158
0b18f4aa
C
159 this.router.navigate([], {
160 relativeTo: this.route,
c5d04b4f 161 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
0b18f4aa
C
162 })
163 }
57c36b27 164}