]>
Commit | Line | Data |
---|---|---|
9af61e84 | 1 | import { ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core' |
fd45e8f4 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
2a2c19df | 3 | import { Location } from '@angular/common' |
0cd4344f C |
4 | import { isInMobileView } from '@app/shared/misc/utils' |
5 | import { InfiniteScrollerDirective } from '@app/shared/video/infinite-scroller.directive' | |
fd45e8f4 | 6 | import { NotificationsService } from 'angular2-notifications' |
caae7a06 | 7 | import 'rxjs/add/operator/debounceTime' |
9bf9d2a5 | 8 | import { Observable } from 'rxjs/Observable' |
6194c1b4 | 9 | import { fromEvent } from 'rxjs/observable/fromEvent' |
9af61e84 | 10 | import { Subscription } from 'rxjs/Subscription' |
b2731bff | 11 | import { AuthService } from '../../core/auth' |
4635f59d | 12 | import { ComponentPagination } from '../rest/component-pagination.model' |
7b87d2d5 | 13 | import { VideoSortField } from './sort-field.type' |
202f6b6c | 14 | import { Video } from './video.model' |
fd45e8f4 | 15 | |
9af61e84 | 16 | export abstract class AbstractVideoList implements OnInit, OnDestroy { |
75236b98 | 17 | private static LINES_PER_PAGE = 4 |
0cd4344f | 18 | |
1ff8d7d6 | 19 | @ViewChild('videosElement') videosElement: ElementRef |
0cd4344f C |
20 | @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective |
21 | ||
4635f59d | 22 | pagination: ComponentPagination = { |
fd45e8f4 | 23 | currentPage: 1, |
0cd4344f | 24 | itemsPerPage: 10, |
fd45e8f4 C |
25 | totalItems: null |
26 | } | |
7b87d2d5 C |
27 | sort: VideoSortField = '-createdAt' |
28 | defaultSort: VideoSortField = '-createdAt' | |
cc1561f9 | 29 | syndicationItems = [] |
244e76a5 | 30 | |
f3aaa9a9 | 31 | loadOnInit = 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 | |
2a2c19df | 44 | protected abstract location: Location |
2bbb3412 | 45 | protected abstract currentRoute: string |
9bf9d2a5 | 46 | abstract titlePage: string |
c88593f7 | 47 | |
0cd4344f C |
48 | protected loadedPages: { [ id: number ]: Video[] } = {} |
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') |
6194c1b4 C |
66 | .debounceTime(500) |
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 | |
fd45e8f4 | 99 | |
0cd4344f | 100 | const observable = this.getVideosObservable(page) |
fd45e8f4 C |
101 | |
102 | observable.subscribe( | |
103 | ({ videos, totalVideos }) => { | |
a2b817d3 | 104 | // Paging is too high, return to the first one |
f595d394 | 105 | if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) { |
a2b817d3 C |
106 | this.pagination.currentPage = 1 |
107 | this.setNewRouteParams() | |
108 | return this.reloadVideos() | |
109 | } | |
110 | ||
0cd4344f C |
111 | this.loadedPages[page] = videos |
112 | this.buildVideoPages() | |
fd45e8f4 | 113 | this.pagination.totalItems = totalVideos |
2bbb3412 | 114 | |
0cd4344f C |
115 | // Initialize infinite scroller now we loaded the first page |
116 | if (Object.keys(this.loadedPages).length === 1) { | |
117 | // Wait elements creation | |
118 | setTimeout(() => this.infiniteScroller.initialize(), 500) | |
2bbb3412 | 119 | } |
fd45e8f4 | 120 | }, |
c5911fd3 | 121 | error => this.notificationsService.error('Error', error.message) |
fd45e8f4 C |
122 | ) |
123 | } | |
124 | ||
2bbb3412 | 125 | protected hasMoreVideos () { |
f595d394 C |
126 | // No results |
127 | if (this.pagination.totalItems === 0) return false | |
128 | ||
129 | // Not loaded yet | |
2bbb3412 C |
130 | if (!this.pagination.totalItems) return true |
131 | ||
202f6b6c | 132 | const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage |
6a6d92b1 | 133 | return maxPage > this.maxPageLoaded() |
2bbb3412 C |
134 | } |
135 | ||
136 | protected previousPage () { | |
0cd4344f | 137 | const min = this.minPageLoaded() |
2bbb3412 | 138 | |
0cd4344f C |
139 | if (min > 1) { |
140 | this.loadMoreVideos(min - 1) | |
141 | } | |
2bbb3412 C |
142 | } |
143 | ||
144 | protected nextPage () { | |
0cd4344f | 145 | this.loadMoreVideos(this.maxPageLoaded() + 1) |
fd45e8f4 C |
146 | } |
147 | ||
fd45e8f4 C |
148 | protected buildRouteParams () { |
149 | // There is always a sort and a current page | |
150 | const params = { | |
151 | sort: this.sort, | |
152 | page: this.pagination.currentPage | |
153 | } | |
154 | ||
0cd4344f | 155 | return Object.assign(params, this.otherRouteParams) |
fd45e8f4 C |
156 | } |
157 | ||
158 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { | |
7b87d2d5 | 159 | this.sort = routeParams['sort'] as VideoSortField || this.defaultSort |
fd45e8f4 C |
160 | |
161 | if (routeParams['page'] !== undefined) { | |
162 | this.pagination.currentPage = parseInt(routeParams['page'], 10) | |
163 | } else { | |
164 | this.pagination.currentPage = 1 | |
165 | } | |
166 | } | |
167 | ||
2bbb3412 | 168 | protected setNewRouteParams () { |
2a2c19df C |
169 | const paramsObject = this.buildRouteParams() |
170 | ||
171 | const queryParams = Object.keys(paramsObject).map(p => p + '=' + paramsObject[p]).join('&') | |
172 | this.location.replaceState(this.currentRoute, queryParams) | |
fd45e8f4 | 173 | } |
0cd4344f C |
174 | |
175 | protected buildVideoPages () { | |
176 | this.videoPages = Object.values(this.loadedPages) | |
177 | } | |
178 | ||
a86887a4 C |
179 | protected buildVideoHeight () { |
180 | // Same ratios than base width/height | |
181 | return this.videosElement.nativeElement.offsetWidth * (this.baseVideoHeight / this.baseVideoWidth) | |
182 | } | |
183 | ||
0cd4344f C |
184 | private minPageLoaded () { |
185 | return Math.min(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) | |
186 | } | |
187 | ||
188 | private maxPageLoaded () { | |
189 | return Math.max(...Object.keys(this.loadedPages).map(e => parseInt(e, 10))) | |
190 | } | |
6194c1b4 C |
191 | |
192 | private calcPageSizes () { | |
9af61e84 | 193 | if (isInMobileView() || this.baseVideoWidth === -1) { |
6194c1b4 C |
194 | this.pagination.itemsPerPage = 5 |
195 | ||
196 | // Video takes all the width | |
197 | this.videoWidth = -1 | |
a86887a4 | 198 | this.videoHeight = this.buildVideoHeight() |
6194c1b4 C |
199 | this.pageHeight = this.pagination.itemsPerPage * this.videoHeight |
200 | } else { | |
9af61e84 C |
201 | this.videoWidth = this.baseVideoWidth |
202 | this.videoHeight = this.baseVideoHeight | |
caae7a06 | 203 | |
6194c1b4 C |
204 | const videosWidth = this.videosElement.nativeElement.offsetWidth |
205 | this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE | |
206 | this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE | |
207 | } | |
208 | ||
209 | // Rebuild pages because maybe we modified the number of items per page | |
caae7a06 | 210 | const videos = [].concat(...this.videoPages) |
6194c1b4 C |
211 | this.loadedPages = {} |
212 | ||
caae7a06 C |
213 | let i = 1 |
214 | // Don't include the last page if it not complete | |
215 | while (videos.length >= this.pagination.itemsPerPage && i < 10000) { // 10000 -> Hard limit in case of infinite loop | |
216 | this.loadedPages[i] = videos.splice(0, this.pagination.itemsPerPage) | |
217 | i++ | |
6194c1b4 C |
218 | } |
219 | ||
9af61e84 C |
220 | // Re fetch the last page |
221 | if (videos.length !== 0) { | |
222 | this.loadMoreVideos(i) | |
223 | } else { | |
224 | this.buildVideoPages() | |
225 | } | |
6194c1b4 | 226 | |
caae7a06 | 227 | console.log('Rebuilt pages with %s elements per page.', this.pagination.itemsPerPage) |
6194c1b4 | 228 | } |
fd45e8f4 | 229 | } |