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