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 'rxjs/add/operator/debounceTime'
7 import { Observable } from 'rxjs/Observable'
8 import { fromEvent } from 'rxjs/observable/fromEvent'
9 import { Subscription } from 'rxjs/Subscription'
10 import { AuthService } from '../../core/auth'
11 import { ComponentPagination } from '../rest/component-pagination.model'
12 import { SortField } from './sort-field.type'
13 import { Video } from './video.model'
15 export abstract class AbstractVideoList implements OnInit, OnDestroy {
16 private static LINES_PER_PAGE = 4
18 @ViewChild('videosElement') videosElement: ElementRef
19 @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective
21 pagination: ComponentPagination = {
26 sort: SortField = '-createdAt'
27 defaultSort: SortField = '-createdAt'
34 videoPages: Video[][] = []
36 protected baseVideoWidth = 215
37 protected baseVideoHeight = 230
39 protected abstract notificationsService: NotificationsService
40 protected abstract authService: AuthService
41 protected abstract router: Router
42 protected abstract route: ActivatedRoute
43 protected abstract currentRoute: string
44 abstract titlePage: string
46 protected loadedPages: { [ id: number ]: Video[] } = {}
47 protected otherRouteParams = {}
49 private resizeSubscription: Subscription
51 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}>
52 abstract generateSyndicationList ()
55 return this.authService.getUser()
59 // Subscribe to route changes
60 const routeParams = this.route.snapshot.queryParams
61 this.loadRouteParams(routeParams)
63 this.resizeSubscription = fromEvent(window, 'resize')
65 .subscribe(() => this.calcPageSizes())
68 if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
72 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
80 if (this.hasMoreVideos()) {
85 onPageChanged (page: number) {
86 this.pagination.currentPage = page
87 this.setNewRouteParams()
92 this.loadMoreVideos(this.pagination.currentPage)
95 loadMoreVideos (page: number) {
96 if (this.loadedPages[page] !== undefined) return
98 const observable = this.getVideosObservable(page)
100 observable.subscribe(
101 ({ videos, totalVideos }) => {
102 // Paging is too high, return to the first one
103 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
104 this.pagination.currentPage = 1
105 this.setNewRouteParams()
106 return this.reloadVideos()
109 this.loadedPages[page] = videos
110 this.buildVideoPages()
111 this.pagination.totalItems = totalVideos
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)
119 error => this.notificationsService.error('Error', error.message)
123 protected hasMoreVideos () {
125 if (this.pagination.totalItems === 0) return false
128 if (!this.pagination.totalItems) return true
130 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
131 return maxPage > this.maxPageLoaded()
134 protected previousPage () {
135 const min = this.minPageLoaded()
138 this.loadMoreVideos(min - 1)
142 protected nextPage () {
143 this.loadMoreVideos(this.maxPageLoaded() + 1)
146 protected buildRouteParams () {
147 // There is always a sort and a current page
150 page: this.pagination.currentPage
153 return Object.assign(params, this.otherRouteParams)
156 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
157 this.sort = routeParams['sort'] as SortField || this.defaultSort
159 if (routeParams['page'] !== undefined) {
160 this.pagination.currentPage = parseInt(routeParams['page'], 10)
162 this.pagination.currentPage = 1
166 protected setNewRouteParams () {
167 const routeParams = this.buildRouteParams()
168 this.router.navigate([ this.currentRoute ], { queryParams: routeParams })
171 protected buildVideoPages () {
172 this.videoPages = Object.values(this.loadedPages)
175 protected buildVideoHeight () {
176 // Same ratios than base width/height
177 return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth)
180 private minPageLoaded () {
181 return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
184 private maxPageLoaded () {
185 return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
188 private calcPageSizes () {
189 if (isInMobileView() || this.baseVideoWidth === -1) {
190 this.pagination.itemsPerPage = 5
192 // Video takes all the width
194 this.videoHeight = this.buildVideoHeight()
195 this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
197 this.videoWidth = this.baseVideoWidth
198 this.videoHeight = this.baseVideoHeight
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
205 // Rebuild pages because maybe we modified the number of items per page
206 const videos = [].concat(...this.videoPages)
207 this.loadedPages = {}
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)
216 // Re fetch the last page
217 if (videos.length !== 0) {
218 this.loadMoreVideos(i)
220 this.buildVideoPages()
223 console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage)