]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/search/search.component.ts
Lazy load client script scopes
[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 } 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 import { HooksService } from '@app/core/plugins/hooks.service'
14 import { PluginService } from '@app/core/plugins/plugin.service'
15
16 @Component({
17 selector: 'my-search',
18 styleUrls: [ './search.component.scss' ],
19 templateUrl: './search.component.html'
20 })
21 export class SearchComponent implements OnInit, OnDestroy {
22 results: (Video | VideoChannel)[] = []
23
24 pagination: ComponentPagination = {
25 currentPage: 1,
26 itemsPerPage: 10, // Only for videos, use another variable for channels
27 totalItems: null
28 }
29 advancedSearch: AdvancedSearch = new AdvancedSearch()
30 isSearchFilterCollapsed = true
31 currentSearch: string
32
33 private subActivatedRoute: Subscription
34 private isInitialLoad = false // set to false to show the search filters on first arrival
35 private firstSearch = true
36
37 private channelsPerPage = 2
38
39 constructor (
40 private i18n: I18n,
41 private route: ActivatedRoute,
42 private router: Router,
43 private metaService: MetaService,
44 private notifier: Notifier,
45 private searchService: SearchService,
46 private authService: AuthService,
47 private hooks: HooksService,
48 private pluginService: PluginService
49 ) { }
50
51 get user () {
52 return this.authService.getUser()
53 }
54
55 ngOnInit () {
56 this.subActivatedRoute = this.route.queryParams.subscribe(
57 queryParams => {
58 const querySearch = queryParams['search']
59
60 // Search updated, reset filters
61 if (this.currentSearch !== querySearch) {
62 this.resetPagination()
63 this.advancedSearch.reset()
64
65 this.currentSearch = querySearch || undefined
66 this.updateTitle()
67 }
68
69 this.advancedSearch = new AdvancedSearch(queryParams)
70
71 // Don't hide filters if we have some of them AND the user just came on the webpage
72 this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
73 this.isInitialLoad = false
74
75 this.search()
76 },
77
78 err => this.notifier.error(err.text)
79 )
80
81 this.hooks.runAction('action:search.init', 'search')
82 }
83
84 ngOnDestroy () {
85 if (this.subActivatedRoute) this.subActivatedRoute.unsubscribe()
86 }
87
88 isVideoChannel (d: VideoChannel | Video): d is VideoChannel {
89 return d instanceof VideoChannel
90 }
91
92 isVideo (v: VideoChannel | Video): v is Video {
93 return v instanceof Video
94 }
95
96 isUserLoggedIn () {
97 return this.authService.isLoggedIn()
98 }
99
100 search () {
101 forkJoin([
102 this.getVideosObs(),
103 this.getVideoChannelObs()
104 ])
105 .subscribe(
106 ([ videosResult, videoChannelsResult ]) => {
107 this.results = this.results
108 .concat(videoChannelsResult.data)
109 .concat(videosResult.data)
110 this.pagination.totalItems = videosResult.total + videoChannelsResult.total
111
112 // Focus on channels if there are no enough videos
113 if (this.firstSearch === true && videosResult.data.length < this.pagination.itemsPerPage) {
114 this.resetPagination()
115 this.firstSearch = false
116
117 this.channelsPerPage = 10
118 this.search()
119 }
120
121 this.firstSearch = false
122 },
123
124 err => this.notifier.error(err.message)
125 )
126 }
127
128 onNearOfBottom () {
129 // Last page
130 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
131
132 this.pagination.currentPage += 1
133 this.search()
134 }
135
136 onFiltered () {
137 this.resetPagination()
138
139 this.updateUrlFromAdvancedSearch()
140 }
141
142 numberOfFilters () {
143 return this.advancedSearch.size()
144 }
145
146 removeVideoFromArray (video: Video) {
147 this.results = this.results.filter(r => !this.isVideo(r) || r.id !== video.id)
148 }
149
150 private resetPagination () {
151 this.pagination.currentPage = 1
152 this.pagination.totalItems = null
153 this.channelsPerPage = 2
154
155 this.results = []
156 }
157
158 private updateTitle () {
159 const suffix = this.currentSearch ? ' ' + this.currentSearch : ''
160 this.metaService.setTitle(this.i18n('Search') + suffix)
161 }
162
163 private updateUrlFromAdvancedSearch () {
164 const search = this.currentSearch || undefined
165
166 this.router.navigate([], {
167 relativeTo: this.route,
168 queryParams: Object.assign({}, this.advancedSearch.toUrlObject(), { search })
169 })
170 }
171
172 private getVideosObs () {
173 const params = {
174 search: this.currentSearch,
175 componentPagination: this.pagination,
176 advancedSearch: this.advancedSearch
177 }
178
179 return this.hooks.wrapObsFun(
180 this.searchService.searchVideos.bind(this.searchService),
181 params,
182 'search',
183 'filter:api.search.videos.list.params',
184 'filter:api.search.videos.list.result'
185 )
186 }
187
188 private getVideoChannelObs () {
189 const params = {
190 search: this.currentSearch,
191 componentPagination: immutableAssign(this.pagination, { itemsPerPage: this.channelsPerPage })
192 }
193
194 return this.hooks.wrapObsFun(
195 this.searchService.searchVideoChannels.bind(this.searchService),
196 params,
197 'search',
198 'filter:api.search.video-channels.list.params',
199 'filter:api.search.video-channels.list.result'
200 )
201 }
202 }