1 import { OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { isInMobileView, isInSmallView } from '@app/shared/misc/utils'
4 import { NotificationsService } from 'angular2-notifications'
5 import { Observable } from 'rxjs/Observable'
6 import { AuthService } from '../../core/auth'
7 import { ComponentPagination } from '../rest/component-pagination.model'
8 import { SortField } from './sort-field.type'
9 import { Video } from './video.model'
11 export abstract class AbstractVideoList implements OnInit {
12 pagination: ComponentPagination = {
17 sort: SortField = '-createdAt'
18 defaultSort: SortField = '-createdAt'
22 protected abstract notificationsService: NotificationsService
23 protected abstract authService: AuthService
24 protected abstract router: Router
25 protected abstract route: ActivatedRoute
27 protected abstract currentRoute: string
29 abstract titlePage: string
31 protected otherParams = {}
33 private loadedPages: { [ id: number ]: boolean } = {}
35 abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
38 return this.authService.getUser()
42 // Subscribe to route changes
43 const routeParams = this.route.snapshot.params
44 this.loadRouteParams(routeParams)
46 if (isInMobileView()) {
47 this.pagination.itemsPerPage = 5
50 if (this.loadOnInit === true) this.loadMoreVideos('after')
54 if (this.pagination.currentPage > 1) {
60 if (this.hasMoreVideos()) {
68 this.loadMoreVideos('before')
71 loadMoreVideos (where: 'before' | 'after') {
72 if (this.loadedPages[this.pagination.currentPage] === true) return
74 const observable = this.getVideosObservable()
77 ({ videos, totalVideos }) => {
78 // Paging is too high, return to the first one
79 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
80 this.pagination.currentPage = 1
81 this.setNewRouteParams()
82 return this.reloadVideos()
85 this.loadedPages[this.pagination.currentPage] = true
86 this.pagination.totalItems = totalVideos
88 if (where === 'before') {
89 this.videos = videos.concat(this.videos)
91 this.videos = this.videos.concat(videos)
94 error => this.notificationsService.error('Error', error.message)
98 protected hasMoreVideos () {
100 if (this.pagination.totalItems === 0) return false
103 if (!this.pagination.totalItems) return true
105 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
106 return maxPage > this.pagination.currentPage
109 protected previousPage () {
110 this.pagination.currentPage--
112 this.setNewRouteParams()
113 this.loadMoreVideos('before')
116 protected nextPage () {
117 this.pagination.currentPage++
119 this.setNewRouteParams()
120 this.loadMoreVideos('after')
123 protected buildRouteParams () {
124 // There is always a sort and a current page
127 page: this.pagination.currentPage
130 return Object.assign(params, this.otherParams)
133 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
134 this.sort = routeParams['sort'] as SortField || this.defaultSort
136 if (routeParams['page'] !== undefined) {
137 this.pagination.currentPage = parseInt(routeParams['page'], 10)
139 this.pagination.currentPage = 1
143 protected setNewRouteParams () {
144 const routeParams = this.buildRouteParams()
145 this.router.navigate([ this.currentRoute, routeParams ])