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