]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { debounceTime } from 'rxjs/operators' |
9af61e84 | 2 | import { ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core' |
fd45e8f4 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
2a2c19df | 4 | import { Location } from '@angular/common' |
0cd4344f C |
5 | import { isInMobileView } from '@app/shared/misc/utils' |
6 | import { InfiniteScrollerDirective } from '@app/shared/video/infinite-scroller.directive' | |
fd45e8f4 | 7 | import { NotificationsService } from 'angular2-notifications' |
db400f44 | 8 | import { fromEvent, Observable, Subscription } from 'rxjs' |
b2731bff | 9 | import { AuthService } from '../../core/auth' |
4635f59d | 10 | import { ComponentPagination } from '../rest/component-pagination.model' |
7b87d2d5 | 11 | import { VideoSortField } from './sort-field.type' |
202f6b6c | 12 | import { Video } from './video.model' |
fd45e8f4 | 13 | |
9af61e84 | 14 | export abstract class AbstractVideoList implements OnInit, OnDestroy { |
75236b98 | 15 | private static LINES_PER_PAGE = 4 |
0cd4344f | 16 | |
1ff8d7d6 | 17 | @ViewChild('videosElement') videosElement: ElementRef |
0cd4344f C |
18 | @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective |
19 | ||
4635f59d | 20 | pagination: ComponentPagination = { |
fd45e8f4 | 21 | currentPage: 1, |
0cd4344f | 22 | itemsPerPage: 10, |
fd45e8f4 C |
23 | totalItems: null |
24 | } | |
136cce4d C |
25 | sort: VideoSortField = '-publishedAt' |
26 | defaultSort: VideoSortField = '-publishedAt' | |
cc1561f9 | 27 | syndicationItems = [] |
244e76a5 | 28 | |
f3aaa9a9 | 29 | loadOnInit = true |
0626e7af | 30 | marginContent = true |
0cd4344f | 31 | pageHeight: number |
caae7a06 C |
32 | videoWidth: number |
33 | videoHeight: number | |
34 | videoPages: Video[][] = [] | |
fd45e8f4 | 35 | |
9af61e84 C |
36 | protected baseVideoWidth = 215 |
37 | protected baseVideoHeight = 230 | |
38 | ||
b2731bff C |
39 | protected abstract notificationsService: NotificationsService |
40 | protected abstract authService: AuthService | |
41 | protected abstract router: Router | |
42 | protected abstract route: ActivatedRoute | |
2a2c19df | 43 | protected abstract location: Location |
2bbb3412 | 44 | protected abstract currentRoute: string |
9bf9d2a5 | 45 | abstract titlePage: string |
c88593f7 | 46 | |
0cd4344f | 47 | protected loadedPages: { [ id: number ]: Video[] } = {} |
5f73f5da | 48 | protected loadingPage: { [ id: number ]: boolean } = {} |
0cd4344f | 49 | protected otherRouteParams = {} |
2bbb3412 | 50 | |
9af61e84 C |
51 | private resizeSubscription: Subscription |
52 | ||
0cd4344f | 53 | abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}> |
244e76a5 | 54 | abstract generateSyndicationList () |
fd45e8f4 | 55 | |
b2731bff C |
56 | get user () { |
57 | return this.authService.getUser() | |
58 | } | |
59 | ||
fd45e8f4 C |
60 | ngOnInit () { |
61 | // Subscribe to route changes | |
5b5e333f | 62 | const routeParams = this.route.snapshot.queryParams |
2bbb3412 | 63 | this.loadRouteParams(routeParams) |
a2b817d3 | 64 | |
9af61e84 | 65 | this.resizeSubscription = fromEvent(window, 'resize') |
db400f44 | 66 | .pipe(debounceTime(500)) |
6194c1b4 | 67 | .subscribe(() => this.calcPageSizes()) |
3290f37c | 68 | |
6194c1b4 | 69 | this.calcPageSizes() |
0cd4344f | 70 | if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage) |
fd45e8f4 C |
71 | } |
72 | ||
9af61e84 C |
73 | ngOnDestroy () { |
74 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
75 | } | |
76 | ||
2bbb3412 | 77 | onNearOfTop () { |
0cd4344f | 78 | this.previousPage() |
2bbb3412 C |
79 | } |
80 | ||
81 | onNearOfBottom () { | |
82 | if (this.hasMoreVideos()) { | |
83 | this.nextPage() | |
84 | } | |
85 | } | |
86 | ||
0cd4344f C |
87 | onPageChanged (page: number) { |
88 | this.pagination.currentPage = page | |
89 | this.setNewRouteParams() | |
90 | } | |
91 | ||
f3aaa9a9 | 92 | reloadVideos () { |
f3aaa9a9 | 93 | this.loadedPages = {} |
0cd4344f | 94 | this.loadMoreVideos(this.pagination.currentPage) |
f3aaa9a9 C |
95 | } |
96 | ||
0cd4344f C |
97 | loadMoreVideos (page: number) { |
98 | if (this.loadedPages[page] !== undefined) return | |
5f73f5da | 99 | if (this.loadingPage[page] === true) return |
fd45e8f4 | 100 | |
5f73f5da | 101 | this.loadingPage[page] = true |
0cd4344f | 102 | const observable = this.getVideosObservable(page) |
fd45e8f4 C |
103 | |
104 | observable.subscribe( | |
105 | ({ videos, totalVideos }) => { | |
5f73f5da C |
106 | this.loadingPage[page] = false |
107 | ||
a2b817d3 | 108 | // Paging is too high, return to the first one |
f595d394 | 109 | if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) { |
a2b817d3 C |
110 | this.pagination.currentPage = 1 |
111 | this.setNewRouteParams() | |
112 | return this.reloadVideos() | |
113 | } | |
114 | ||
0cd4344f C |
115 | this.loadedPages[page] = videos |
116 | this.buildVideoPages() | |
fd45e8f4 | 117 | this.pagination.totalItems = totalVideos |
2bbb3412 | 118 | |
0cd4344f C |
119 | // Initialize infinite scroller now we loaded the first page |
120 | if (Object.keys(this.loadedPages).length === 1) { | |
121 | // Wait elements creation | |
122 | setTimeout(() => this.infiniteScroller.initialize(), 500) | |
2bbb3412 | 123 | } |
fd45e8f4 | 124 | }, |
5f73f5da C |
125 | error => { |
126 | this.loadingPage[page] = false | |
127 | this.notificationsService.error('Error', error.message) | |
128 | } | |
fd45e8f4 C |
129 | ) |
130 | } | |
131 | ||
2bbb3412 | 132 | protected hasMoreVideos () { |
f595d394 C |
133 | // No results |
134 | if (this.pagination.totalItems === 0) return false | |
135 | ||
136 | // Not loaded yet | |
2bbb3412 C |
137 | if (!this.pagination.totalItems) return true |
138 | ||
202f6b6c | 139 | const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage |
6a6d92b1 | 140 | return maxPage > this.maxPageLoaded() |
2bbb3412 C |
141 | } |
142 | ||
143 | protected previousPage () { | |
0cd4344f | 144 | const min = this.minPageLoaded() |
2bbb3412 | 145 | |
0cd4344f C |
146 | if (min > 1) { |
147 | this.loadMoreVideos(min - 1) | |
148 | } | |
2bbb3412 C |
149 | } |
150 | ||
151 | protected nextPage () { | |
0cd4344f | 152 | this.loadMoreVideos(this.maxPageLoaded() + 1) |
fd45e8f4 C |
153 | } |
154 | ||
fd45e8f4 C |
155 | protected buildRouteParams () { |
156 | // There is always a sort and a current page | |
157 | const params = { | |
158 | sort: this.sort, | |
159 | page: this.pagination.currentPage | |
160 | } | |
161 | ||
0cd4344f | 162 | return Object.assign(params, this.otherRouteParams) |
fd45e8f4 C |
163 | } |
164 | ||
165 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { | |
7b87d2d5 | 166 | this.sort = routeParams['sort'] as VideoSortField || this.defaultSort |
fd45e8f4 C |
167 | |
168 | if (routeParams['page'] !== undefined) { | |
169 | this.pagination.currentPage = parseInt(routeParams['page'], 10) | |
170 | } else { | |
171 | this.pagination.currentPage = 1 | |
172 | } | |
173 | } | |
174 | ||
2bbb3412 | 175 | protected setNewRouteParams () { |
2a2c19df C |
176 | const paramsObject = this.buildRouteParams() |
177 | ||
178 | const queryParams = Object.keys(paramsObject).map(p => p + '=' + paramsObject[p]).join('&') | |
179 | this.location.replaceState(this.currentRoute, queryParams) | |
fd45e8f4 | 180 | } |
0cd4344f C |
181 | |
182 | protected buildVideoPages () { | |
183 | this.videoPages = Object.values(this.loadedPages) | |
184 | } | |
185 | ||
a86887a4 C |
186 | protected buildVideoHeight () { |
187 | // Same ratios than base width/height | |
188 | return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth) | |
189 | } | |
190 | ||
0cd4344f C |
191 | private minPageLoaded () { |
192 | return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) | |
193 | } | |
194 | ||
195 | private maxPageLoaded () { | |
196 | return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) | |
197 | } | |
6194c1b4 C |
198 | |
199 | private calcPageSizes () { | |
9af61e84 | 200 | if (isInMobileView() || this.baseVideoWidth === -1) { |
6194c1b4 C |
201 | this.pagination.itemsPerPage = 5 |
202 | ||
203 | // Video takes all the width | |
204 | this.videoWidth = -1 | |
a86887a4 | 205 | this.videoHeight = this.buildVideoHeight() |
6194c1b4 C |
206 | this.pageHeight = this.pagination.itemsPerPage * this.videoHeight |
207 | } else { | |
9af61e84 C |
208 | this.videoWidth = this.baseVideoWidth |
209 | this.videoHeight = this.baseVideoHeight | |
caae7a06 | 210 | |
6194c1b4 C |
211 | const videosWidth = this.videosElement.nativeElement.offsetWidth |
212 | this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE | |
213 | this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE | |
214 | } | |
215 | ||
216 | // Rebuild pages because maybe we modified the number of items per page | |
caae7a06 | 217 | const videos = [].concat(...this.videoPages) |
6194c1b4 C |
218 | this.loadedPages = {} |
219 | ||
caae7a06 C |
220 | let i = 1 |
221 | // Don't include the last page if it not complete | |
222 | while (videos.length >= this.pagination.itemsPerPage && i < 10000) { // 10000 -> Hard limit in case of infinite loop | |
223 | this.loadedPages[i] = videos.splice(0, this.pagination.itemsPerPage) | |
224 | i++ | |
6194c1b4 C |
225 | } |
226 | ||
9af61e84 C |
227 | // Re fetch the last page |
228 | if (videos.length !== 0) { | |
229 | this.loadMoreVideos(i) | |
230 | } else { | |
231 | this.buildVideoPages() | |
232 | } | |
6194c1b4 | 233 | |
caae7a06 | 234 | console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage) |
6194c1b4 | 235 | } |
fd45e8f4 | 236 | } |