]>
Commit | Line | Data |
---|---|---|
ac81d1a0 | 1 | import { ElementRef, 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' |
9bf9d2a5 | 6 | import { Observable } from 'rxjs/Observable' |
b2731bff | 7 | import { AuthService } from '../../core/auth' |
4635f59d | 8 | import { ComponentPagination } from '../rest/component-pagination.model' |
202f6b6c | 9 | import { SortField } from './sort-field.type' |
202f6b6c | 10 | import { Video } from './video.model' |
fd45e8f4 | 11 | |
f3aaa9a9 | 12 | export abstract class AbstractVideoList implements OnInit { |
0cd4344f C |
13 | private static LINES_PER_PAGE = 3 |
14 | ||
15 | @ViewChild('videoElement') videosElement: ElementRef | |
16 | @ViewChild(InfiniteScrollerDirective) infiniteScroller: InfiniteScrollerDirective | |
17 | ||
4635f59d | 18 | pagination: ComponentPagination = { |
fd45e8f4 | 19 | currentPage: 1, |
0cd4344f | 20 | itemsPerPage: 10, |
fd45e8f4 C |
21 | totalItems: null |
22 | } | |
9bf9d2a5 | 23 | sort: SortField = '-createdAt' |
f3aaa9a9 | 24 | defaultSort: SortField = '-createdAt' |
f3aaa9a9 | 25 | loadOnInit = true |
0cd4344f C |
26 | pageHeight: number |
27 | videoWidth = 215 | |
28 | videoHeight = 230 | |
29 | videoPages: Video[][] | |
fd45e8f4 | 30 | |
b2731bff C |
31 | protected abstract notificationsService: NotificationsService |
32 | protected abstract authService: AuthService | |
33 | protected abstract router: Router | |
34 | protected abstract route: ActivatedRoute | |
2bbb3412 | 35 | protected abstract currentRoute: string |
9bf9d2a5 | 36 | abstract titlePage: string |
c88593f7 | 37 | |
0cd4344f C |
38 | protected loadedPages: { [ id: number ]: Video[] } = {} |
39 | protected otherRouteParams = {} | |
2bbb3412 | 40 | |
0cd4344f | 41 | abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number}> |
fd45e8f4 | 42 | |
b2731bff C |
43 | get user () { |
44 | return this.authService.getUser() | |
45 | } | |
46 | ||
fd45e8f4 C |
47 | ngOnInit () { |
48 | // Subscribe to route changes | |
2bbb3412 C |
49 | const routeParams = this.route.snapshot.params |
50 | this.loadRouteParams(routeParams) | |
a2b817d3 | 51 | |
3290f37c C |
52 | if (isInMobileView()) { |
53 | this.pagination.itemsPerPage = 5 | |
0cd4344f C |
54 | this.videoWidth = -1 |
55 | } | |
56 | ||
57 | if (this.videoWidth !== -1) { | |
58 | const videosWidth = this.videosElement.nativeElement.offsetWidth | |
59 | this.pagination.itemsPerPage = Math.floor(videosWidth / this.videoWidth) * AbstractVideoList.LINES_PER_PAGE | |
60 | } | |
61 | ||
62 | // Video takes all the width | |
63 | if (this.videoWidth === -1) { | |
64 | this.pageHeight = this.pagination.itemsPerPage * this.videoHeight | |
65 | } else { | |
66 | this.pageHeight = this.videoHeight * AbstractVideoList.LINES_PER_PAGE | |
3290f37c C |
67 | } |
68 | ||
0cd4344f | 69 | if (this.loadOnInit === true) this.loadMoreVideos(this.pagination.currentPage) |
fd45e8f4 C |
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 |
2bbb3412 C |
128 | return maxPage > this.pagination.currentPage |
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 | } | |
fd45e8f4 | 179 | } |