]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/abstract-video-list.ts
7235b34255e3860aa51ce926787a40103c3b0392
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
1 import { ElementRef, 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 { Observable } from 'rxjs/Observable'
7 import { fromEvent } from 'rxjs/observable/fromEvent'
8 import { AuthService } from '../../core/auth'
9 import { ComponentPagination } from '../rest/component-pagination.model'
10 import { SortField } from './sort-field.type'
11 import { Video } from './video.model'
12
13 export abstract class AbstractVideoList implements OnInit {
14 private static LINES_PER_PAGE = 3
15
16 @ViewChild('videoElement') videosElement: ElementRef
17 @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective
18
19 pagination: ComponentPagination = {
20 currentPage: 1,
21 itemsPerPage: 10,
22 totalItems: null
23 }
24 sort: SortField = '-createdAt'
25 defaultSort: SortField = '-createdAt'
26 loadOnInit = true
27 pageHeight: number
28 videoWidth = 215
29 videoHeight = 230
30 videoPages: Video[][]
31
32 protected abstract notificationsService: NotificationsService
33 protected abstract authService: AuthService
34 protected abstract router: Router
35 protected abstract route: ActivatedRoute
36 protected abstract currentRoute: string
37 abstract titlePage: string
38
39 protected loadedPages: { [ id: number ]: Video[] } = {}
40 protected otherRouteParams = {}
41
42 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}>
43
44 get user () {
45 return this.authService.getUser()
46 }
47
48 ngOnInit () {
49 // Subscribe to route changes
50 const routeParams = this.route.snapshot.params
51 this.loadRouteParams(routeParams)
52
53 fromEvent(window, 'resize')
54 .debounceTime(500)
55 .subscribe(() => this.calcPageSizes())
56
57 this.calcPageSizes()
58 if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage)
59 }
60
61 onNearOfTop () {
62 this.previousPage()
63 }
64
65 onNearOfBottom () {
66 if (this.hasMoreVideos()) {
67 this.nextPage()
68 }
69 }
70
71 onPageChanged (page: number) {
72 this.pagination.currentPage = page
73 this.setNewRouteParams()
74 }
75
76 reloadVideos () {
77 this.loadedPages = {}
78 this.loadMoreVideos(this.pagination.currentPage)
79 }
80
81 loadMoreVideos (page: number) {
82 if (this.loadedPages[page] !== undefined) return
83
84 const observable = this.getVideosObservable(page)
85
86 observable.subscribe(
87 ({ videos, totalVideos }) => {
88 // Paging is too high, return to the first one
89 if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) {
90 this.pagination.currentPage = 1
91 this.setNewRouteParams()
92 return this.reloadVideos()
93 }
94
95 this.loadedPages[page] = videos
96 this.buildVideoPages()
97 this.pagination.totalItems = totalVideos
98
99 // Initialize infinite scroller now we loaded the first page
100 if (Object.keys(this.loadedPages).length === 1) {
101 // Wait elements creation
102 setTimeout(() => this.infiniteScroller.initialize(), 500)
103 }
104 },
105 error => this.notificationsService.error('Error', error.message)
106 )
107 }
108
109 protected hasMoreVideos () {
110 // No results
111 if (this.pagination.totalItems === 0) return false
112
113 // Not loaded yet
114 if (!this.pagination.totalItems) return true
115
116 const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage
117 return maxPage > this.maxPageLoaded()
118 }
119
120 protected previousPage () {
121 const min = this.minPageLoaded()
122
123 if (min > 1) {
124 this.loadMoreVideos(min - 1)
125 }
126 }
127
128 protected nextPage () {
129 this.loadMoreVideos(this.maxPageLoaded() + 1)
130 }
131
132 protected buildRouteParams () {
133 // There is always a sort and a current page
134 const params = {
135 sort: this.sort,
136 page: this.pagination.currentPage
137 }
138
139 return Object.assign(params, this.otherRouteParams)
140 }
141
142 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
143 this.sort = routeParams['sort'] as SortField || this.defaultSort
144
145 if (routeParams['page'] !== undefined) {
146 this.pagination.currentPage = parseInt(routeParams['page'], 10)
147 } else {
148 this.pagination.currentPage = 1
149 }
150 }
151
152 protected setNewRouteParams () {
153 const routeParams = this.buildRouteParams()
154 this.router.navigate([ this.currentRoute, routeParams ])
155 }
156
157 protected buildVideoPages () {
158 this.videoPages = Object.values(this.loadedPages)
159 }
160
161 private minPageLoaded () {
162 return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
163 }
164
165 private maxPageLoaded () {
166 return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10)))
167 }
168
169 private calcPageSizes () {
170 if (isInMobileView()) {
171 this.pagination.itemsPerPage = 5
172
173 // Video takes all the width
174 this.videoWidth = -1
175 this.pageHeight = this.pagination.itemsPerPage * this.videoHeight
176 } else {
177 const videosWidth = this.videosElement.nativeElement.offsetWidth
178 this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE
179 this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE
180 }
181
182 // Rebuild pages because maybe we modified the number of items per page
183 let videos: Video[] = []
184 Object.values(this.loadedPages)
185 .forEach(videosPage => videos = videos.concat(videosPage))
186 this.loadedPages = {}
187
188 for (let i = 1; (i * this.pagination.itemsPerPage) <= videos.length; i++) {
189 this.loadedPages[i] = videos.slice((i - 1) * this.pagination.itemsPerPage, this.pagination.itemsPerPage * i)
190 }
191
192 this.buildVideoPages()
193
194 console.log('Re calculated pages after a resize!')
195 }
196 }