]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { debounceTime } from 'rxjs/operators' |
489290b8 | 2 | import { OnDestroy, OnInit } from '@angular/core' |
fd45e8f4 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
db400f44 | 4 | import { fromEvent, Observable, Subscription } from 'rxjs' |
b2731bff | 5 | import { AuthService } from '../../core/auth' |
4635f59d | 6 | import { ComponentPagination } from '../rest/component-pagination.model' |
7b87d2d5 | 7 | import { VideoSortField } from './sort-field.type' |
202f6b6c | 8 | import { Video } from './video.model' |
bbe0f064 | 9 | import { ScreenService } from '@app/shared/misc/screen.service' |
abf325b4 | 10 | import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component' |
c199c427 | 11 | import { Syndication } from '@app/shared/video/syndication.model' |
489290b8 C |
12 | import { Notifier, ServerService } from '@app/core' |
13 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' | |
34c7f429 C |
14 | import { I18n } from '@ngx-translate/i18n-polyfill' |
15 | import { isThisMonth, isThisWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date' | |
16 | ||
17 | enum GroupDate { | |
18 | UNKNOWN = 0, | |
19 | TODAY = 1, | |
20 | YESTERDAY = 2, | |
21 | THIS_WEEK = 3, | |
22 | THIS_MONTH = 4, | |
23 | OLDER = 5 | |
24 | } | |
0cd4344f | 25 | |
489290b8 | 26 | export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook { |
4635f59d | 27 | pagination: ComponentPagination = { |
fd45e8f4 | 28 | currentPage: 1, |
489290b8 | 29 | itemsPerPage: 25, |
fd45e8f4 C |
30 | totalItems: null |
31 | } | |
136cce4d | 32 | sort: VideoSortField = '-publishedAt' |
489290b8 | 33 | |
d59cba29 | 34 | categoryOneOf?: number |
136cce4d | 35 | defaultSort: VideoSortField = '-publishedAt' |
489290b8 | 36 | |
c199c427 | 37 | syndicationItems: Syndication[] = [] |
244e76a5 | 38 | |
f3aaa9a9 | 39 | loadOnInit = true |
489290b8 | 40 | videos: Video[] = [] |
22a16e36 | 41 | ownerDisplayType: OwnerDisplayType = 'account' |
017c3dca | 42 | displayModerationBlock = false |
9b4b15f9 | 43 | titleTooltip: string |
3a0fb65c | 44 | displayVideoActions = true |
34c7f429 | 45 | groupByDate = false |
fd45e8f4 | 46 | |
489290b8 | 47 | disabled = false |
9af61e84 | 48 | |
abf325b4 C |
49 | displayOptions: MiniatureDisplayOptions = { |
50 | date: true, | |
51 | views: true, | |
52 | by: true, | |
53 | privacyLabel: true, | |
54 | privacyText: false, | |
55 | state: false, | |
56 | blacklistInfo: false | |
57 | } | |
58 | ||
f8b2c1b4 | 59 | protected abstract notifier: Notifier |
b2731bff | 60 | protected abstract authService: AuthService |
b2731bff | 61 | protected abstract route: ActivatedRoute |
489290b8 | 62 | protected abstract serverService: ServerService |
bbe0f064 | 63 | protected abstract screenService: ScreenService |
489290b8 | 64 | protected abstract router: Router |
34c7f429 | 65 | protected abstract i18n: I18n |
9bf9d2a5 | 66 | abstract titlePage: string |
c88593f7 | 67 | |
9af61e84 | 68 | private resizeSubscription: Subscription |
489290b8 C |
69 | private angularState: number |
70 | ||
34c7f429 C |
71 | private groupedDateLabels: { [id in GroupDate]: string } |
72 | private groupedDates: { [id: number]: GroupDate } = {} | |
73 | ||
489290b8 | 74 | abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }> |
9af61e84 | 75 | |
c199c427 | 76 | abstract generateSyndicationList (): void |
fd45e8f4 | 77 | |
b2731bff C |
78 | get user () { |
79 | return this.authService.getUser() | |
80 | } | |
81 | ||
fd45e8f4 | 82 | ngOnInit () { |
34c7f429 C |
83 | this.groupedDateLabels = { |
84 | [GroupDate.UNKNOWN]: null, | |
85 | [GroupDate.TODAY]: this.i18n('Today'), | |
86 | [GroupDate.YESTERDAY]: this.i18n('Yesterday'), | |
87 | [GroupDate.THIS_WEEK]: this.i18n('This week'), | |
88 | [GroupDate.THIS_MONTH]: this.i18n('This month'), | |
89 | [GroupDate.OLDER]: this.i18n('Older') | |
90 | } | |
91 | ||
fd45e8f4 | 92 | // Subscribe to route changes |
5b5e333f | 93 | const routeParams = this.route.snapshot.queryParams |
2bbb3412 | 94 | this.loadRouteParams(routeParams) |
a2b817d3 | 95 | |
9af61e84 | 96 | this.resizeSubscription = fromEvent(window, 'resize') |
db400f44 | 97 | .pipe(debounceTime(500)) |
6194c1b4 | 98 | .subscribe(() => this.calcPageSizes()) |
3290f37c | 99 | |
6194c1b4 | 100 | this.calcPageSizes() |
489290b8 | 101 | if (this.loadOnInit === true) this.loadMoreVideos() |
fd45e8f4 C |
102 | } |
103 | ||
9af61e84 C |
104 | ngOnDestroy () { |
105 | if (this.resizeSubscription) this.resizeSubscription.unsubscribe() | |
106 | } | |
107 | ||
489290b8 C |
108 | disableForReuse () { |
109 | this.disabled = true | |
89724816 C |
110 | } |
111 | ||
489290b8 C |
112 | enabledForReuse () { |
113 | this.disabled = false | |
89724816 C |
114 | } |
115 | ||
489290b8 C |
116 | videoById (index: number, video: Video) { |
117 | return video.id | |
2bbb3412 C |
118 | } |
119 | ||
120 | onNearOfBottom () { | |
489290b8 | 121 | if (this.disabled) return |
2bbb3412 | 122 | |
489290b8 C |
123 | // Last page |
124 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
0cd4344f | 125 | |
489290b8 | 126 | this.pagination.currentPage += 1 |
a8ecc6f6 | 127 | |
489290b8 | 128 | this.setScrollRouteParams() |
a8ecc6f6 | 129 | |
489290b8 C |
130 | this.loadMoreVideos() |
131 | } | |
fd45e8f4 | 132 | |
489290b8 C |
133 | loadMoreVideos () { |
134 | const observable = this.getVideosObservable(this.pagination.currentPage) | |
fd45e8f4 C |
135 | |
136 | observable.subscribe( | |
137 | ({ videos, totalVideos }) => { | |
fd45e8f4 | 138 | this.pagination.totalItems = totalVideos |
489290b8 | 139 | this.videos = this.videos.concat(videos) |
693263e9 | 140 | |
34c7f429 C |
141 | if (this.groupByDate) this.buildGroupedDateLabels() |
142 | ||
693263e9 | 143 | this.onMoreVideos() |
fd45e8f4 | 144 | }, |
017c3dca | 145 | |
489290b8 C |
146 | error => this.notifier.error(error.message) |
147 | ) | |
2bbb3412 C |
148 | } |
149 | ||
489290b8 C |
150 | reloadVideos () { |
151 | this.pagination.currentPage = 1 | |
152 | this.videos = [] | |
153 | this.loadMoreVideos() | |
fd45e8f4 C |
154 | } |
155 | ||
489290b8 C |
156 | toggleModerationDisplay () { |
157 | throw new Error('toggleModerationDisplay is not implemented') | |
fd45e8f4 C |
158 | } |
159 | ||
3a0fb65c C |
160 | removeVideoFromArray (video: Video) { |
161 | this.videos = this.videos.filter(v => v.id !== video.id) | |
162 | } | |
163 | ||
34c7f429 C |
164 | buildGroupedDateLabels () { |
165 | let currentGroupedDate: GroupDate = GroupDate.UNKNOWN | |
166 | ||
167 | for (const video of this.videos) { | |
168 | const publishedDate = video.publishedAt | |
169 | ||
170 | if (currentGroupedDate < GroupDate.TODAY && isToday(publishedDate)) { | |
171 | currentGroupedDate = GroupDate.TODAY | |
172 | this.groupedDates[ video.id ] = currentGroupedDate | |
173 | continue | |
174 | } | |
175 | ||
176 | if (currentGroupedDate < GroupDate.YESTERDAY && isYesterday(publishedDate)) { | |
177 | currentGroupedDate = GroupDate.YESTERDAY | |
178 | this.groupedDates[ video.id ] = currentGroupedDate | |
179 | continue | |
180 | } | |
181 | ||
182 | if (currentGroupedDate < GroupDate.THIS_WEEK && isThisWeek(publishedDate)) { | |
183 | currentGroupedDate = GroupDate.THIS_WEEK | |
184 | this.groupedDates[ video.id ] = currentGroupedDate | |
185 | continue | |
186 | } | |
187 | ||
188 | if (currentGroupedDate < GroupDate.THIS_MONTH && isThisMonth(publishedDate)) { | |
189 | currentGroupedDate = GroupDate.THIS_MONTH | |
190 | this.groupedDates[ video.id ] = currentGroupedDate | |
191 | continue | |
192 | } | |
193 | ||
194 | if (currentGroupedDate < GroupDate.OLDER) { | |
195 | currentGroupedDate = GroupDate.OLDER | |
196 | this.groupedDates[ video.id ] = currentGroupedDate | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
201 | getCurrentGroupedDateLabel (video: Video) { | |
202 | if (this.groupByDate === false) return undefined | |
203 | ||
204 | return this.groupedDateLabels[this.groupedDates[video.id]] | |
205 | } | |
206 | ||
693263e9 C |
207 | // On videos hook for children that want to do something |
208 | protected onMoreVideos () { /* empty */ } | |
209 | ||
fd45e8f4 | 210 | protected loadRouteParams (routeParams: { [ key: string ]: any }) { |
489290b8 C |
211 | this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort |
212 | this.categoryOneOf = routeParams[ 'categoryOneOf' ] | |
213 | this.angularState = routeParams[ 'a-state' ] | |
0cd4344f | 214 | } |
6194c1b4 C |
215 | |
216 | private calcPageSizes () { | |
489290b8 | 217 | if (this.screenService.isInMobileView()) { |
6194c1b4 | 218 | this.pagination.itemsPerPage = 5 |
6194c1b4 | 219 | } |
489290b8 | 220 | } |
6194c1b4 | 221 | |
489290b8 C |
222 | private setScrollRouteParams () { |
223 | // Already set | |
224 | if (this.angularState) return | |
6194c1b4 | 225 | |
489290b8 | 226 | this.angularState = 42 |
6194c1b4 | 227 | |
489290b8 C |
228 | const queryParams = { |
229 | 'a-state': this.angularState, | |
230 | categoryOneOf: this.categoryOneOf | |
9af61e84 | 231 | } |
6194c1b4 | 232 | |
489290b8 C |
233 | let path = this.router.url |
234 | if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute | |
235 | ||
236 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | |
6194c1b4 | 237 | } |
fd45e8f4 | 238 | } |