]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/search/search.component.ts
Better label for video privacies
[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
7afea880 33 private isInitialLoad = 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
C
41 private metaService: MetaService,
42 private redirectService: RedirectService,
43 private notificationsService: NotificationsService,
26fabbd6
C
44 private searchService: SearchService,
45 private authService: AuthService
57c36b27
C
46 ) { }
47
48 ngOnInit () {
49 this.subActivatedRoute = this.route.queryParams.subscribe(
50 queryParams => {
51 const querySearch = queryParams['search']
52
4278710d
C
53 // New empty search
54 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
57c36b27 55
0b18f4aa 56 // Search updated, reset filters
7afea880
C
57 if (this.currentSearch !== querySearch) {
58 this.resetPagination()
59 this.advancedSearch.reset()
60
61 this.currentSearch = querySearch
62 this.updateTitle()
63 }
64
65 this.advancedSearch = new AdvancedSearch(queryParams)
0b18f4aa 66
7afea880
C
67 // Don't hide filters if we have some of them AND the user just came on the webpage
68 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
69 this.isInitialLoad = false
57c36b27 70
7afea880 71 this.search()
57c36b27
C
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
76 }
77
78 ngOnDestroy () {
79 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
80 }
81
26fabbd6
C
82 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
83 return d instanceof VideoChannel
84 }
85
86 isVideo (v: VideoChannel | Video): v is Video {
87 return v instanceof Video
88 }
89
90 isUserLoggedIn () {
91 return this.authService.isLoggedIn()
92 }
93
57c36b27 94 search () {
f37dc0dd
C
95 forkJoin([
96 this.searchService.searchVideos(this.currentSearch, this.pagination, this.advancedSearch),
97 this.searchService.searchVideoChannels(this.currentSearch, immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage }))
98 ])
57c36b27 99 .subscribe(
f37dc0dd 100 ([ videosResult, videoChannelsResult ]) => {
26fabbd6
C
101 this.results = this.results
102 .concat(videoChannelsResult.data)
103 .concat(videosResult.videos)
aa55a4da 104 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
f37dc0dd 105
aa55a4da 106 // Focus on channels
26fabbd6 107 if (this.channelsPerPage !== 10 && videosResult.videos.length < this.pagination.itemsPerPage) {
aa55a4da
C
108 this.resetPagination()
109
110 this.channelsPerPage = 10
111 this.search()
112 }
57c36b27
C
113 },
114
115 error => {
116 this.notificationsService.error(this.i18n('Error'), error.message)
117 }
118 )
f37dc0dd 119
57c36b27
C
120 }
121
122 onNearOfBottom () {
123 // Last page
124 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
125
126 this.pagination.currentPage += 1
127 this.search()
128 }
129
0b18f4aa 130 onFiltered () {
7afea880 131 this.resetPagination()
0b18f4aa 132
7afea880 133 this.updateUrlFromAdvancedSearch()
0b18f4aa
C
134 }
135
7afea880 136 private resetPagination () {
57c36b27
C
137 this.pagination.currentPage = 1
138 this.pagination.totalItems = null
aa55a4da 139 this.channelsPerPage = 2
57c36b27 140
26fabbd6 141 this.results = []
57c36b27
C
142 }
143
144 private updateTitle () {
145 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
146 }
0b18f4aa
C
147
148 private updateUrlFromAdvancedSearch () {
149 this.router.navigate([], {
150 relativeTo: this.route,
151 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
152 })
153 }
57c36b27 154}