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 { ComponentPagination } from '../rest/component-pagination.model'
7 import { SortField } from './sort-field.type'
8 import { Video } from './video.model'
10 export abstract class AbstractVideoList implements OnInit {
11 pagination: ComponentPagination = {
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
30 protected otherParams = {}
32 private loadedPages: { [ id: number ]: boolean } = {}
34 abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
37 return this.authService.getUser()
41 // Subscribe to route changes
42 const routeParams = this.route.snapshot.params
43 this.loadRouteParams(routeParams)
45 if (this.loadOnInit === true) this.loadMoreVideos('after')
49 if (this.pagination.currentPage > 1) {
55 if (this.hasMoreVideos()) {
63 this.loadMoreVideos('before')
66 loadMoreVideos (where: 'before' | 'after') {
67 if (this.loadedPages[this.pagination.currentPage] === true) return
69 const observable = this.getVideosObservable()
72 ({ videos, totalVideos }) => {
73 // Paging is too high, return to the first one
74 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
75 this.pagination.currentPage = 1
76 this.setNewRouteParams()
77 return this.reloadVideos()
80 this.loadedPages[this.pagination.currentPage] = true
81 this.pagination.totalItems = totalVideos
83 if (where === 'before') {
84 this.videos = videos.concat(this.videos)
86 this.videos = this.videos.concat(videos)
89 error => this.notificationsService.error('Error', error.message)
93 protected hasMoreVideos () {
95 if (this.pagination.totalItems === 0) return false
98 if (!this.pagination.totalItems) return true
100 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
101 return maxPage > this.pagination.currentPage
104 protected previousPage () {
105 this.pagination.currentPage--
107 this.setNewRouteParams()
108 this.loadMoreVideos('before')
111 protected nextPage () {
112 this.pagination.currentPage++
114 this.setNewRouteParams()
115 this.loadMoreVideos('after')
118 protected buildRouteParams () {
119 // There is always a sort and a current page
122 page: this.pagination.currentPage
125 return Object.assign(params, this.otherParams)
128 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
129 this.sort = routeParams['sort'] as SortField || this.defaultSort
131 if (routeParams['page'] !== undefined) {
132 this.pagination.currentPage = parseInt(routeParams['page'], 10)
134 this.pagination.currentPage = 1
138 protected setNewRouteParams () {
139 const routeParams = this.buildRouteParams()
140 this.router.navigate([ this.currentRoute, routeParams ])