]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/abstract-video-list.ts
feature: initial syndication feeds support
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
1 import { ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { isInMobileView } from '@app/shared/misc/utils'
4 import { InfiniteScrollerDirective } from '@app/shared/video/infinite-scroller.directive'
5 import { NotificationsService } from 'angular2-notifications'
6 import { PopoverModule } from 'ngx-bootstrap/popover'
7 import 'rxjs/add/operator/debounceTime'
8 import { Observable } from 'rxjs/Observable'
9 import { fromEvent } from 'rxjs/observable/fromEvent'
10 import { Subscription } from 'rxjs/Subscription'
11 import { AuthService } from '../../core/auth'
12 import { ComponentPagination } from '../rest/component-pagination.model'
13 import { SortField } from './sort-field.type'
14 import { Video } from './video.model'
15 import { FeedFormat } from '../../../../../shared'
16 import { VideoFeedComponent } from '@app/shared/video/video-feed.component'
17
18 export abstract class AbstractVideoList implements OnInit, OnDestroy {
19 private static LINES_PER_PAGE = 4
20
21 @ViewChild('videosElement') videosElement: ElementRef
22 @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective
23
24 pagination: ComponentPagination = {
25 currentPage: 1,
26 itemsPerPage: 10,
27 totalItems: null
28 }
29 sort: SortField = '-createdAt'
30 defaultSort: SortField = '-createdAt'
31 syndicationItems = {}
32
33 loadOnInit = true
34 pageHeight: number
35 videoWidth: number
36 videoHeight: number
37 videoPages: Video[][] = []
38
39 protected baseVideoWidth = 215
40 protected baseVideoHeight = 230
41
42 protected abstract notificationsService: NotificationsService
43 protected abstract authService: AuthService
44 protected abstract router: Router
45 protected abstract route: ActivatedRoute
46 protected abstract currentRoute: string
47 abstract titlePage: string
48
49 protected loadedPages: { [ id: number ]: Video[] } = {}
50 protected otherRouteParams = {}
51
52 private resizeSubscription: Subscription
53
54 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}>
55 abstract generateSyndicationList ()
56
57 get user () {
58 return this.authService.getUser()
59 }
60
61 ngOnInit () {
62 // Subscribe to route changes
63 const routeParams = this.route.snapshot.queryParams
64 this.loadRouteParams(routeParams)
65
66 this.resizeSubscription = fromEvent(window, 'resize')
67 .debounceTime(500)
68 .subscribe(() => this.calcPageSizes())
69
70 this.calcPageSizes()
71 if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
72 }
73
74 ngOnDestroy () {
75 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
76 }
77
78 onNearOfTop () {
79 this.previousPage()
80 }
81
82 onNearOfBottom () {
83 if (this.hasMoreVideos()) {
84 this.nextPage()
85 }
86 }
87
88 onPageChanged (page: number) {
89 this.pagination.currentPage = page
90 this.setNewRouteParams()
91 }
92
93 reloadVideos () {
94 this.loadedPages = {}
95 this.loadMoreVideos(this.pagination.currentPage)
96 }
97
98 loadMoreVideos (page: number) {
99 if (this.loadedPages[page] !== undefined) return
100
101 const observable = this.getVideosObservable(page)
102
103 observable.subscribe(
104 ({ videos, totalVideos }) => {
105 // Paging is too high, return to the first one
106 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
107 this.pagination.currentPage = 1
108 this.setNewRouteParams()
109 return this.reloadVideos()
110 }
111
112 this.loadedPages[page] = videos
113 this.buildVideoPages()
114 this.pagination.totalItems = totalVideos
115
116 // Initialize infinite scroller now we loaded the first page
117 if (Object.keys(this.loadedPages).length === 1) {
118 // Wait elements creation
119 setTimeout(() => this.infiniteScroller.initialize(), 500)
120 }
121 },
122 error => this.notificationsService.error('Error', error.message)
123 )
124 }
125
126 protected hasMoreVideos () {
127 // No results
128 if (this.pagination.totalItems === 0) return false
129
130 // Not loaded yet
131 if (!this.pagination.totalItems) return true
132
133 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
134 return maxPage > this.maxPageLoaded()
135 }
136
137 protected previousPage () {
138 const min = this.minPageLoaded()
139
140 if (min > 1) {
141 this.loadMoreVideos(min - 1)
142 }
143 }
144
145 protected nextPage () {
146 this.loadMoreVideos(this.maxPageLoaded() + 1)
147 }
148
149 protected buildRouteParams () {
150 // There is always a sort and a current page
151 const params = {
152 sort: this.sort,
153 page: this.pagination.currentPage
154 }
155
156 return Object.assign(params, this.otherRouteParams)
157 }
158
159 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
160 this.sort = routeParams['sort'] as SortField || this.defaultSort
161
162 if (routeParams['page'] !== undefined) {
163 this.pagination.currentPage = parseInt(routeParams['page'], 10)
164 } else {
165 this.pagination.currentPage = 1
166 }
167 }
168
169 protected setNewRouteParams () {
170 const routeParams = this.buildRouteParams()
171 this.router.navigate([ this.currentRoute ], { queryParams: routeParams })
172 }
173
174 protected buildVideoPages () {
175 this.videoPages = Object.values(this.loadedPages)
176 }
177
178 protected buildVideoHeight () {
179 // Same ratios than base width/height
180 return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth)
181 }
182
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 }
190
191 private calcPageSizes () {
192 if (isInMobileView() || this.baseVideoWidth === -1) {
193 this.pagination.itemsPerPage = 5
194
195 // Video takes all the width
196 this.videoWidth = -1
197 this.videoHeight = this.buildVideoHeight()
198 this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
199 } else {
200 this.videoWidth = this.baseVideoWidth
201 this.videoHeight = this.baseVideoHeight
202
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
209 const videos = [].concat(...this.videoPages)
210 this.loadedPages = {}
211
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++
217 }
218
219 // Re fetch the last page
220 if (videos.length !== 0) {
221 this.loadMoreVideos(i)
222 } else {
223 this.buildVideoPages()
224 }
225
226 console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage)
227 }
228 }