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