]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
Add zh-Hans-CN to client.sh
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
CommitLineData
db400f44 1import { debounceTime } from 'rxjs/operators'
9af61e84 2import { ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
fd45e8f4 3import { ActivatedRoute, Router } from '@angular/router'
2a2c19df 4import { Location } from '@angular/common'
0cd4344f 5import { InfiniteScrollerDirective } from '@app/shared/video/infinite-scroller.directive'
fd45e8f4 6import { NotificationsService } from 'angular2-notifications'
db400f44 7import { fromEvent, Observable, Subscription } from 'rxjs'
b2731bff 8import { AuthService } from '../../core/auth'
4635f59d 9import { ComponentPagination } from '../rest/component-pagination.model'
7b87d2d5 10import { VideoSortField } from './sort-field.type'
202f6b6c 11import { Video } from './video.model'
b1d40cff 12import { I18n } from '@ngx-translate/i18n-polyfill'
bbe0f064 13import { ScreenService } from '@app/shared/misc/screen.service'
22a16e36 14import { OwnerDisplayType } from '@app/shared/video/video-miniature.component'
fd45e8f4 15
9af61e84 16export abstract class AbstractVideoList implements OnInit, OnDestroy {
75236b98 17 private static LINES_PER_PAGE = 4
0cd4344f 18
1ff8d7d6 19 @ViewChild('videosElement') videosElement: ElementRef
0cd4344f
C
20 @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective
21
4635f59d 22 pagination: ComponentPagination = {
fd45e8f4 23 currentPage: 1,
0cd4344f 24 itemsPerPage: 10,
fd45e8f4
C
25 totalItems: null
26 }
136cce4d 27 sort: VideoSortField = '-publishedAt'
d59cba29 28 categoryOneOf?: number
136cce4d 29 defaultSort: VideoSortField = '-publishedAt'
cc1561f9 30 syndicationItems = []
244e76a5 31
f3aaa9a9 32 loadOnInit = true
0626e7af 33 marginContent = true
0cd4344f 34 pageHeight: number
caae7a06
C
35 videoWidth: number
36 videoHeight: number
37 videoPages: Video[][] = []
22a16e36 38 ownerDisplayType: OwnerDisplayType = 'account'
d5931e62 39 firstLoadedPage: number
fd45e8f4 40
9af61e84 41 protected baseVideoWidth = 215
a8ecc6f6 42 protected baseVideoHeight = 205
9af61e84 43
b2731bff
C
44 protected abstract notificationsService: NotificationsService
45 protected abstract authService: AuthService
46 protected abstract router: Router
47 protected abstract route: ActivatedRoute
bbe0f064 48 protected abstract screenService: ScreenService
b1d40cff 49 protected abstract i18n: I18n
2a2c19df 50 protected abstract location: Location
2bbb3412 51 protected abstract currentRoute: string
9bf9d2a5 52 abstract titlePage: string
c88593f7 53
0cd4344f 54 protected loadedPages: { [ id: number ]: Video[] } = {}
5f73f5da 55 protected loadingPage: { [ id: number ]: boolean } = {}
0cd4344f 56 protected otherRouteParams = {}
2bbb3412 57
9af61e84
C
58 private resizeSubscription: Subscription
59
0cd4344f 60 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}>
244e76a5 61 abstract generateSyndicationList ()
fd45e8f4 62
b2731bff
C
63 get user () {
64 return this.authService.getUser()
65 }
66
fd45e8f4
C
67 ngOnInit () {
68 // Subscribe to route changes
5b5e333f 69 const routeParams = this.route.snapshot.queryParams
2bbb3412 70 this.loadRouteParams(routeParams)
a2b817d3 71
9af61e84 72 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 73 .pipe(debounceTime(500))
6194c1b4 74 .subscribe(() => this.calcPageSizes())
3290f37c 75
6194c1b4 76 this.calcPageSizes()
0cd4344f 77 if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
fd45e8f4
C
78 }
79
9af61e84
C
80 ngOnDestroy () {
81 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
82 }
83
89724816
C
84 pageByVideoId (index: number, page: Video[]) {
85 // Video are unique in all pages
86 return page[0].id
87 }
88
89 videoById (index: number, video: Video) {
90 return video.id
91 }
92
2bbb3412 93 onNearOfTop () {
0cd4344f 94 this.previousPage()
2bbb3412
C
95 }
96
97 onNearOfBottom () {
98 if (this.hasMoreVideos()) {
99 this.nextPage()
100 }
101 }
102
0cd4344f
C
103 onPageChanged (page: number) {
104 this.pagination.currentPage = page
105 this.setNewRouteParams()
106 }
107
f3aaa9a9 108 reloadVideos () {
f3aaa9a9 109 this.loadedPages = {}
0cd4344f 110 this.loadMoreVideos(this.pagination.currentPage)
f3aaa9a9
C
111 }
112
a8ecc6f6
C
113 loadMoreVideos (page: number, loadOnTop = false) {
114 this.adjustVideoPageHeight()
115
116 const currentY = window.scrollY
117
0cd4344f 118 if (this.loadedPages[page] !== undefined) return
5f73f5da 119 if (this.loadingPage[page] === true) return
fd45e8f4 120
5f73f5da 121 this.loadingPage[page] = true
0cd4344f 122 const observable = this.getVideosObservable(page)
fd45e8f4
C
123
124 observable.subscribe(
125 ({ videos, totalVideos }) => {
5f73f5da
C
126 this.loadingPage[page] = false
127
a8ecc6f6
C
128 if (this.firstLoadedPage === undefined || this.firstLoadedPage > page) this.firstLoadedPage = page
129
a2b817d3 130 // Paging is too high, return to the first one
f595d394 131 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
a2b817d3
C
132 this.pagination.currentPage = 1
133 this.setNewRouteParams()
134 return this.reloadVideos()
135 }
136
0cd4344f
C
137 this.loadedPages[page] = videos
138 this.buildVideoPages()
fd45e8f4 139 this.pagination.totalItems = totalVideos
2bbb3412 140
0cd4344f
C
141 // Initialize infinite scroller now we loaded the first page
142 if (Object.keys(this.loadedPages).length === 1) {
143 // Wait elements creation
a8ecc6f6
C
144 setTimeout(() => {
145 this.infiniteScroller.initialize()
146
147 // At our first load, we did not load the first page
148 // Load the previous page so the user can move on the top (and browser previous pages)
149 if (this.pagination.currentPage > 1) this.loadMoreVideos(this.pagination.currentPage - 1, true)
150 }, 500)
2bbb3412 151 }
a8ecc6f6
C
152
153 // Insert elements on the top but keep the scroll in the previous position
154 if (loadOnTop) setTimeout(() => { window.scrollTo(0, currentY + this.pageHeight) }, 0)
fd45e8f4 155 },
5f73f5da
C
156 error => {
157 this.loadingPage[page] = false
b1d40cff 158 this.notificationsService.error(this.i18n('Error'), error.message)
5f73f5da 159 }
fd45e8f4
C
160 )
161 }
162
2bbb3412 163 protected hasMoreVideos () {
f595d394
C
164 // No results
165 if (this.pagination.totalItems === 0) return false
166
167 // Not loaded yet
2bbb3412
C
168 if (!this.pagination.totalItems) return true
169
202f6b6c 170 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
6a6d92b1 171 return maxPage > this.maxPageLoaded()
2bbb3412
C
172 }
173
174 protected previousPage () {
0cd4344f 175 const min = this.minPageLoaded()
2bbb3412 176
0cd4344f 177 if (min > 1) {
89724816 178 this.loadMoreVideos(min - 1, true)
0cd4344f 179 }
2bbb3412
C
180 }
181
182 protected nextPage () {
0cd4344f 183 this.loadMoreVideos(this.maxPageLoaded() + 1)
fd45e8f4
C
184 }
185
fd45e8f4
C
186 protected buildRouteParams () {
187 // There is always a sort and a current page
188 const params = {
189 sort: this.sort,
190 page: this.pagination.currentPage
191 }
192
0cd4344f 193 return Object.assign(params, this.otherRouteParams)
fd45e8f4
C
194 }
195
196 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
7b87d2d5 197 this.sort = routeParams['sort'] as VideoSortField || this.defaultSort
d59cba29 198 this.categoryOneOf = routeParams['categoryOneOf']
fd45e8f4
C
199 if (routeParams['page'] !== undefined) {
200 this.pagination.currentPage = parseInt(routeParams['page'], 10)
201 } else {
202 this.pagination.currentPage = 1
203 }
204 }
205
2bbb3412 206 protected setNewRouteParams () {
2a2c19df
C
207 const paramsObject = this.buildRouteParams()
208
209 const queryParams = Object.keys(paramsObject).map(p => p + '=' + paramsObject[p]).join('&')
210 this.location.replaceState(this.currentRoute, queryParams)
fd45e8f4 211 }
0cd4344f
C
212
213 protected buildVideoPages () {
214 this.videoPages = Object.values(this.loadedPages)
215 }
216
a8ecc6f6
C
217 protected adjustVideoPageHeight () {
218 const numberOfPagesLoaded = Object.keys(this.loadedPages).length
219 if (!numberOfPagesLoaded) return
220
221 this.pageHeight = this.videosElement.nativeElement.offsetHeight / numberOfPagesLoaded
222 }
223
a86887a4
C
224 protected buildVideoHeight () {
225 // Same ratios than base width/height
226 return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth)
227 }
228
0cd4344f
C
229 private minPageLoaded () {
230 return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
231 }
232
233 private maxPageLoaded () {
234 return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
235 }
6194c1b4
C
236
237 private calcPageSizes () {
bbe0f064 238 if (this.screenService.isInMobileView() || this.baseVideoWidth === -1) {
6194c1b4
C
239 this.pagination.itemsPerPage = 5
240
241 // Video takes all the width
242 this.videoWidth = -1
a86887a4 243 this.videoHeight = this.buildVideoHeight()
6194c1b4
C
244 this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
245 } else {
9af61e84
C
246 this.videoWidth = this.baseVideoWidth
247 this.videoHeight = this.baseVideoHeight
caae7a06 248
6194c1b4
C
249 const videosWidth = this.videosElement.nativeElement.offsetWidth
250 this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE
251 this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE
252 }
253
254 // Rebuild pages because maybe we modified the number of items per page
caae7a06 255 const videos = [].concat(...this.videoPages)
6194c1b4
C
256 this.loadedPages = {}
257
caae7a06
C
258 let i = 1
259 // Don't include the last page if it not complete
260 while (videos.length >= this.pagination.itemsPerPage && i < 10000) { // 10000 -> Hard limit in case of infinite loop
261 this.loadedPages[i] = videos.splice(0, this.pagination.itemsPerPage)
262 i++
6194c1b4
C
263 }
264
9af61e84
C
265 // Re fetch the last page
266 if (videos.length !== 0) {
267 this.loadMoreVideos(i)
268 } else {
269 this.buildVideoPages()
270 }
6194c1b4 271
caae7a06 272 console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage)
6194c1b4 273 }
fd45e8f4 274}