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