1 import { OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { Observable } from 'rxjs/Observable'
5 import { AuthService } from '../../core/auth'
6 import { SortField } from './sort-field.type'
7 import { VideoPagination } from './video-pagination.model'
8 import { Video } from './video.model'
10 export abstract class AbstractVideoList implements OnInit {
11 pagination: VideoPagination = {
16 sort: SortField = '-createdAt'
17 defaultSort: SortField = '-createdAt'
21 protected abstract notificationsService: NotificationsService
22 protected abstract authService: AuthService
23 protected abstract router: Router
24 protected abstract route: ActivatedRoute
26 protected abstract currentRoute: string
28 abstract titlePage: string
29 private loadedPages: { [ id: number ]: boolean } = {}
31 abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
34 return this.authService.getUser()
38 // Subscribe to route changes
39 const routeParams = this.route.snapshot.params
40 this.loadRouteParams(routeParams)
42 if (this.loadOnInit === true) this.loadMoreVideos('after')
46 if (this.pagination.currentPage > 1) {
52 if (this.hasMoreVideos()) {
60 this.loadMoreVideos('before')
63 loadMoreVideos (where: 'before' | 'after') {
64 if (this.loadedPages[this.pagination.currentPage] === true) return
66 const observable = this.getVideosObservable()
69 ({ videos, totalVideos }) => {
70 // Paging is too high, return to the first one
71 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
72 this.pagination.currentPage = 1
73 this.setNewRouteParams()
74 return this.reloadVideos()
77 this.loadedPages[this.pagination.currentPage] = true
78 this.pagination.totalItems = totalVideos
80 if (where === 'before') {
81 this.videos = videos.concat(this.videos)
83 this.videos = this.videos.concat(videos)
86 error => this.notificationsService.error('Error', error.text)
90 protected hasMoreVideos () {
92 if (this.pagination.totalItems === 0) return false
95 if (!this.pagination.totalItems) return true
97 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
98 return maxPage > this.pagination.currentPage
101 protected previousPage () {
102 this.pagination.currentPage--
104 this.setNewRouteParams()
105 this.loadMoreVideos('before')
108 protected nextPage () {
109 this.pagination.currentPage++
111 this.setNewRouteParams()
112 this.loadMoreVideos('after')
115 protected buildRouteParams () {
116 // There is always a sort and a current page
119 page: this.pagination.currentPage
125 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
126 this.sort = routeParams['sort'] as SortField || this.defaultSort
128 if (routeParams['page'] !== undefined) {
129 this.pagination.currentPage = parseInt(routeParams['page'], 10)
131 this.pagination.currentPage = 1
135 protected setNewRouteParams () {
136 const routeParams = this.buildRouteParams()
137 this.router.navigate([ this.currentRoute, routeParams ])