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