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