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