]>
Commit | Line | Data |
---|---|---|
f3aaa9a9 | 1 | import { OnInit } from '@angular/core' |
fd45e8f4 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
3290f37c | 3 | import { isInMobileView, isInSmallView } from '@app/shared/misc/utils' |
fd45e8f4 | 4 | import { NotificationsService } from 'angular2-notifications' |
9bf9d2a5 | 5 | import { Observable } from 'rxjs/Observable' |
b2731bff | 6 | import { AuthService } from '../../core/auth' |
4635f59d | 7 | import { ComponentPagination } from '../rest/component-pagination.model' |
202f6b6c | 8 | import { SortField } from './sort-field.type' |
202f6b6c | 9 | import { Video } from './video.model' |
fd45e8f4 | 10 | |
f3aaa9a9 | 11 | export abstract class AbstractVideoList implements OnInit { |
4635f59d | 12 | pagination: ComponentPagination = { |
fd45e8f4 | 13 | currentPage: 1, |
6f6cdef7 | 14 | itemsPerPage: 25, |
fd45e8f4 C |
15 | totalItems: null |
16 | } | |
9bf9d2a5 | 17 | sort: SortField = '-createdAt' |
f3aaa9a9 | 18 | defaultSort: SortField = '-createdAt' |
fd45e8f4 | 19 | videos: Video[] = [] |
f3aaa9a9 | 20 | loadOnInit = true |
fd45e8f4 | 21 | |
b2731bff C |
22 | protected abstract notificationsService: NotificationsService |
23 | protected abstract authService: AuthService | |
24 | protected abstract router: Router | |
25 | protected abstract route: ActivatedRoute | |
fd45e8f4 | 26 | |
2bbb3412 C |
27 | protected abstract currentRoute: string |
28 | ||
9bf9d2a5 | 29 | abstract titlePage: string |
c88593f7 C |
30 | |
31 | protected otherParams = {} | |
32 | ||
2bbb3412 C |
33 | private loadedPages: { [ id: number ]: boolean } = {} |
34 | ||
fd45e8f4 C |
35 | abstract getVideosObservable (): Observable<{ videos: Video[], totalVideos: number}> |
36 | ||
b2731bff C |
37 | get user () { |
38 | return this.authService.getUser() | |
39 | } | |
40 | ||
fd45e8f4 C |
41 | ngOnInit () { |
42 | // Subscribe to route changes | |
2bbb3412 C |
43 | const routeParams = this.route.snapshot.params |
44 | this.loadRouteParams(routeParams) | |
a2b817d3 | 45 | |
3290f37c C |
46 | if (isInMobileView()) { |
47 | this.pagination.itemsPerPage = 5 | |
48 | } | |
49 | ||
f3aaa9a9 | 50 | if (this.loadOnInit === true) this.loadMoreVideos('after') |
fd45e8f4 C |
51 | } |
52 | ||
2bbb3412 C |
53 | onNearOfTop () { |
54 | if (this.pagination.currentPage > 1) { | |
55 | this.previousPage() | |
56 | } | |
57 | } | |
58 | ||
59 | onNearOfBottom () { | |
60 | if (this.hasMoreVideos()) { | |
61 | this.nextPage() | |
62 | } | |
63 | } | |
64 | ||
f3aaa9a9 C |
65 | reloadVideos () { |
66 | this.videos = [] | |
67 | this.loadedPages = {} | |
68 | this.loadMoreVideos('before') | |
69 | } | |
70 | ||
2bbb3412 C |
71 | loadMoreVideos (where: 'before' | 'after') { |
72 | if (this.loadedPages[this.pagination.currentPage] === true) return | |
fd45e8f4 C |
73 | |
74 | const observable = this.getVideosObservable() | |
75 | ||
76 | observable.subscribe( | |
77 | ({ videos, totalVideos }) => { | |
a2b817d3 | 78 | // Paging is too high, return to the first one |
f595d394 | 79 | if (this.pagination.currentPage > 1 && totalVideos <= ((this.pagination.currentPage - 1) * this.pagination.itemsPerPage)) { |
a2b817d3 C |
80 | this.pagination.currentPage = 1 |
81 | this.setNewRouteParams() | |
82 | return this.reloadVideos() | |
83 | } | |
84 | ||
2bbb3412 | 85 | this.loadedPages[this.pagination.currentPage] = true |
fd45e8f4 | 86 | this.pagination.totalItems = totalVideos |
2bbb3412 C |
87 | |
88 | if (where === 'before') { | |
89 | this.videos = videos.concat(this.videos) | |
90 | } else { | |
91 | this.videos = this.videos.concat(videos) | |
92 | } | |
fd45e8f4 | 93 | }, |
c5911fd3 | 94 | error => this.notificationsService.error('Error', error.message) |
fd45e8f4 C |
95 | ) |
96 | } | |
97 | ||
2bbb3412 | 98 | protected hasMoreVideos () { |
f595d394 C |
99 | // No results |
100 | if (this.pagination.totalItems === 0) return false | |
101 | ||
102 | // Not loaded yet | |
2bbb3412 C |
103 | if (!this.pagination.totalItems) return true |
104 | ||
202f6b6c | 105 | const maxPage = this.pagination.totalItems / this.pagination.itemsPerPage |
2bbb3412 C |
106 | return maxPage > this.pagination.currentPage |
107 | } | |
108 | ||
109 | protected previousPage () { | |
110 | this.pagination.currentPage-- | |
111 | ||
112 | this.setNewRouteParams() | |
113 | this.loadMoreVideos('before') | |
114 | } | |
115 | ||
116 | protected nextPage () { | |
117 | this.pagination.currentPage++ | |
fd45e8f4 | 118 | |
2bbb3412 C |
119 | this.setNewRouteParams() |
120 | this.loadMoreVideos('after') | |
fd45e8f4 C |
121 | } |
122 | ||
fd45e8f4 C |
123 | protected buildRouteParams () { |
124 | // There is always a sort and a current page | |
125 | const params = { | |
126 | sort: this.sort, | |
127 | page: this.pagination.currentPage | |
128 | } | |
129 | ||
c88593f7 | 130 | return Object.assign(params, this.otherParams) |
fd45e8f4 C |
131 | } |
132 | ||
133 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { | |
f3aaa9a9 | 134 | this.sort = routeParams['sort'] as SortField || this.defaultSort |
fd45e8f4 C |
135 | |
136 | if (routeParams['page'] !== undefined) { | |
137 | this.pagination.currentPage = parseInt(routeParams['page'], 10) | |
138 | } else { | |
139 | this.pagination.currentPage = 1 | |
140 | } | |
141 | } | |
142 | ||
2bbb3412 | 143 | protected setNewRouteParams () { |
fd45e8f4 | 144 | const routeParams = this.buildRouteParams() |
2bbb3412 | 145 | this.router.navigate([ this.currentRoute, routeParams ]) |
fd45e8f4 C |
146 | } |
147 | } |