]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
Better label for video privacies
[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, RedirectService } from '@app/core'
4 import { NotificationsService } from 'angular2-notifications'
5 import { forkJoin, Subscription } from 'rxjs'
6 import { SearchService } from '@app/search/search.service'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { MetaService } from '@ngx-meta/core'
10 import { AdvancedSearch } from '@app/search/advanced-search.model'
11 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
12 import { immutableAssign } from '@app/shared/misc/utils'
13 import { Video } from '@app/shared/video/video.model'
14
15 @Component({
16 selector: 'my-search',
17 styleUrls: [ './search.component.scss' ],
18 templateUrl: './search.component.html'
19 })
20 export class SearchComponent implements OnInit, OnDestroy {
21 results: (Video | VideoChannel)[] = []
22
23 pagination: ComponentPagination = {
24 currentPage: 1,
25 itemsPerPage: 10, // Only for videos, use another variable for channels
26 totalItems: null
27 }
28 advancedSearch: AdvancedSearch = new AdvancedSearch()
29 isSearchFilterCollapsed = true
30 currentSearch: string
31
32 private subActivatedRoute: Subscription
33 private isInitialLoad = 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 redirectService: RedirectService,
43 private notificationsService: NotificationsService,
44 private searchService: SearchService,
45 private authService: AuthService
46 ) { }
47
48 ngOnInit () {
49 this.subActivatedRoute = this.route.queryParams.subscribe(
50 queryParams => {
51 const querySearch = queryParams['search']
52
53 // New empty search
54 if (this.currentSearch && !querySearch) return this.redirectService.redirectToHomepage()
55
56 // Search updated, reset filters
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)
66
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
70
71 this.search()
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
76 }
77
78 ngOnDestroy () {
79 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
80 }
81
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
94 search () {
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 ])
99 .subscribe(
100 ([ videosResult, videoChannelsResult ]) => {
101 this.results = this.results
102 .concat(videoChannelsResult.data)
103 .concat(videosResult.videos)
104 this.pagination.totalItems = videosResult.totalVideos + videoChannelsResult.total
105
106 // Focus on channels
107 if (this.channelsPerPage !== 10 && videosResult.videos.length < this.pagination.itemsPerPage) {
108 this.resetPagination()
109
110 this.channelsPerPage = 10
111 this.search()
112 }
113 },
114
115 error => {
116 this.notificationsService.error(this.i18n('Error'), error.message)
117 }
118 )
119
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
130 onFiltered () {
131 this.resetPagination()
132
133 this.updateUrlFromAdvancedSearch()
134 }
135
136 private resetPagination () {
137 this.pagination.currentPage = 1
138 this.pagination.totalItems = null
139 this.channelsPerPage = 2
140
141 this.results = []
142 }
143
144 private updateTitle () {
145 this.metaService.setTitle(this.i18n('Search') + ' ' + this.currentSearch)
146 }
147
148 private updateUrlFromAdvancedSearch () {
149 this.router.navigate([], {
150 relativeTo: this.route,
151 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search: this.currentSearch })
152 })
153 }
154 }