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'
18 export abstract class AbstractVideoList implements OnInit, OnDestroy {
19 private static LINES_PER_PAGE = 4
21 @ViewChild('videosElement') videosElement: ElementRef
22 @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective
24 pagination: ComponentPagination = {
29 sort: SortField = '-createdAt'
30 defaultSort: SortField = '-createdAt'
37 videoPages: Video[][] = []
39 protected baseVideoWidth = 215
40 protected baseVideoHeight = 230
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
49 protected loadedPages: { [ id: number ]: Video[] } = {}
50 protected otherRouteParams = {}
52 private resizeSubscription: Subscription
54 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}>
55 abstract generateSyndicationList ()
58 return this.authService.getUser()
62 // Subscribe to route changes
63 const routeParams = this.route.snapshot.queryParams
64 this.loadRouteParams(routeParams)
66 this.resizeSubscription = fromEvent(window, 'resize')
68 .subscribe(() => this.calcPageSizes())
71 if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
75 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
83 if (this.hasMoreVideos()) {
88 onPageChanged (page: number) {
89 this.pagination.currentPage = page
90 this.setNewRouteParams()
95 this.loadMoreVideos(this.pagination.currentPage)
98 loadMoreVideos (page: number) {
99 if (this.loadedPages[page] !== undefined) return
101 const observable = this.getVideosObservable(page)
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()
112 this.loadedPages[page] = videos
113 this.buildVideoPages()
114 this.pagination.totalItems = totalVideos
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)
122 error => this.notificationsService.error('Error', error.message)
126 protected hasMoreVideos () {
128 if (this.pagination.totalItems === 0) return false
131 if (!this.pagination.totalItems) return true
133 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
134 return maxPage > this.maxPageLoaded()
137 protected previousPage () {
138 const min = this.minPageLoaded()
141 this.loadMoreVideos(min - 1)
145 protected nextPage () {
146 this.loadMoreVideos(this.maxPageLoaded() + 1)
149 protected buildRouteParams () {
150 // There is always a sort and a current page
153 page: this.pagination.currentPage
156 return Object.assign(params, this.otherRouteParams)
159 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
160 this.sort = routeParams['sort'] as SortField || this.defaultSort
162 if (routeParams['page'] !== undefined) {
163 this.pagination.currentPage = parseInt(routeParams['page'], 10)
165 this.pagination.currentPage = 1
169 protected setNewRouteParams () {
170 const routeParams = this.buildRouteParams()
171 this.router.navigate([ this.currentRoute ], { queryParams: routeParams })
174 protected buildVideoPages () {
175 this.videoPages = Object.values(this.loadedPages)
178 protected buildVideoHeight () {
179 // Same ratios than base width/height
180 return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth)
183 private minPageLoaded () {
184 return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
187 private maxPageLoaded () {
188 return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
191 private calcPageSizes () {
192 if (isInMobileView() || this.baseVideoWidth === -1) {
193 this.pagination.itemsPerPage = 5
195 // Video takes all the width
197 this.videoHeight = this.buildVideoHeight()
198 this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
200 this.videoWidth = this.baseVideoWidth
201 this.videoHeight = this.baseVideoHeight
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
208 // Rebuild pages because maybe we modified the number of items per page
209 const videos = [].concat(...this.videoPages)
210 this.loadedPages = {}
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)
219 // Re fetch the last page
220 if (videos.length !== 0) {
221 this.loadMoreVideos(i)
223 this.buildVideoPages()
226 console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage)