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