]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
Begin to add avatar to actors
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
CommitLineData
f3aaa9a9 1import { OnInit } from '@angular/core'
fd45e8f4 2import { ActivatedRoute, Router } from '@angular/router'
fd45e8f4 3import { NotificationsService } from 'angular2-notifications'
9bf9d2a5 4import { Observable } from 'rxjs/Observable'
b2731bff 5import { AuthService } from '../../core/auth'
4635f59d 6import { ComponentPagination } from '../rest/component-pagination.model'
202f6b6c 7import { SortField } from './sort-field.type'
202f6b6c 8import { Video } from './video.model'
fd45e8f4 9
f3aaa9a9 10export abstract class AbstractVideoList implements OnInit {
4635f59d 11 pagination: ComponentPagination = {
fd45e8f4
C
12 currentPage: 1,
13 itemsPerPage: 25,
14 totalItems: null
15 }
9bf9d2a5 16 sort: SortField = '-createdAt'
f3aaa9a9 17 defaultSort: SortField = '-createdAt'
fd45e8f4 18 videos: Video[] = []
f3aaa9a9 19 loadOnInit = true
fd45e8f4 20
b2731bff
C
21 protected abstract notificationsService: NotificationsService
22 protected abstract authService: AuthService
23 protected abstract router: Router
24 protected abstract route: ActivatedRoute
fd45e8f4 25
2bbb3412
C
26 protected abstract currentRoute: string
27
9bf9d2a5 28 abstract titlePage: string
2bbb3412
C
29 private loadedPages: { [ id: number ]: boolean } = {}
30
fd45e8f4
C
31 abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}>
32
b2731bff
C
33 get user () {
34 return this.authService.getUser()
35 }
36
fd45e8f4
C
37 ngOnInit () {
38 // Subscribe to route changes
2bbb3412
C
39 const routeParams = this.route.snapshot.params
40 this.loadRouteParams(routeParams)
a2b817d3 41
f3aaa9a9 42 if (this.loadOnInit === true) this.loadMoreVideos('after')
fd45e8f4
C
43 }
44
2bbb3412
C
45 onNearOfTop () {
46 if (this.pagination.currentPage > 1) {
47 this.previousPage()
48 }
49 }
50
51 onNearOfBottom () {
52 if (this.hasMoreVideos()) {
53 this.nextPage()
54 }
55 }
56
f3aaa9a9
C
57 reloadVideos () {
58 this.videos = []
59 this.loadedPages = {}
60 this.loadMoreVideos('before')
61 }
62
2bbb3412
C
63 loadMoreVideos (where: 'before' | 'after') {
64 if (this.loadedPages[this.pagination.currentPage] === true) return
fd45e8f4
C
65
66 const observable = this.getVideosObservable()
67
68 observable.subscribe(
69 ({ videos, totalVideos }) => {
a2b817d3 70 // Paging is too high, return to the first one
f595d394 71 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
a2b817d3
C
72 this.pagination.currentPage = 1
73 this.setNewRouteParams()
74 return this.reloadVideos()
75 }
76
2bbb3412 77 this.loadedPages[this.pagination.currentPage] = true
fd45e8f4 78 this.pagination.totalItems = totalVideos
2bbb3412
C
79
80 if (where === 'before') {
81 this.videos = videos.concat(this.videos)
82 } else {
83 this.videos = this.videos.concat(videos)
84 }
fd45e8f4 85 },
c5911fd3 86 error => this.notificationsService.error('Error', error.message)
fd45e8f4
C
87 )
88 }
89
2bbb3412 90 protected hasMoreVideos () {
f595d394
C
91 // No results
92 if (this.pagination.totalItems === 0) return false
93
94 // Not loaded yet
2bbb3412
C
95 if (!this.pagination.totalItems) return true
96
202f6b6c 97 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
2bbb3412
C
98 return maxPage > this.pagination.currentPage
99 }
100
101 protected previousPage () {
102 this.pagination.currentPage--
103
104 this.setNewRouteParams()
105 this.loadMoreVideos('before')
106 }
107
108 protected nextPage () {
109 this.pagination.currentPage++
fd45e8f4 110
2bbb3412
C
111 this.setNewRouteParams()
112 this.loadMoreVideos('after')
fd45e8f4
C
113 }
114
fd45e8f4
C
115 protected buildRouteParams () {
116 // There is always a sort and a current page
117 const params = {
118 sort: this.sort,
119 page: this.pagination.currentPage
120 }
121
122 return params
123 }
124
125 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
f3aaa9a9 126 this.sort = routeParams['sort'] as SortField || this.defaultSort
fd45e8f4
C
127
128 if (routeParams['page'] !== undefined) {
129 this.pagination.currentPage = parseInt(routeParams['page'], 10)
130 } else {
131 this.pagination.currentPage = 1
132 }
133 }
134
2bbb3412 135 protected setNewRouteParams () {
fd45e8f4 136 const routeParams = this.buildRouteParams()
2bbb3412 137 this.router.navigate([ this.currentRoute, routeParams ])
fd45e8f4
C
138 }
139}