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