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