]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/abstract-video-list.ts
Fix privacy label display
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / abstract-video-list.ts
CommitLineData
db400f44 1import { debounceTime } from 'rxjs/operators'
489290b8 2import { OnDestroy, OnInit } from '@angular/core'
fd45e8f4 3import { ActivatedRoute, Router } from '@angular/router'
db400f44 4import { fromEvent, Observable, Subscription } from 'rxjs'
b2731bff 5import { AuthService } from '../../core/auth'
4635f59d 6import { ComponentPagination } from '../rest/component-pagination.model'
7b87d2d5 7import { VideoSortField } from './sort-field.type'
202f6b6c 8import { Video } from './video.model'
bbe0f064 9import { ScreenService } from '@app/shared/misc/screen.service'
abf325b4 10import { MiniatureDisplayOptions, OwnerDisplayType } from '@app/shared/video/video-miniature.component'
c199c427 11import { Syndication } from '@app/shared/video/syndication.model'
489290b8
C
12import { Notifier, ServerService } from '@app/core'
13import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
0cd4344f 14
489290b8 15export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
4635f59d 16 pagination: ComponentPagination = {
fd45e8f4 17 currentPage: 1,
489290b8 18 itemsPerPage: 25,
fd45e8f4
C
19 totalItems: null
20 }
136cce4d 21 sort: VideoSortField = '-publishedAt'
489290b8 22
d59cba29 23 categoryOneOf?: number
136cce4d 24 defaultSort: VideoSortField = '-publishedAt'
489290b8 25
c199c427 26 syndicationItems: Syndication[] = []
244e76a5 27
f3aaa9a9 28 loadOnInit = true
489290b8 29 videos: Video[] = []
22a16e36 30 ownerDisplayType: OwnerDisplayType = 'account'
017c3dca 31 displayModerationBlock = false
9b4b15f9 32 titleTooltip: string
3a0fb65c 33 displayVideoActions = true
fd45e8f4 34
489290b8 35 disabled = false
9af61e84 36
abf325b4
C
37 displayOptions: MiniatureDisplayOptions = {
38 date: true,
39 views: true,
40 by: true,
41 privacyLabel: true,
42 privacyText: false,
43 state: false,
44 blacklistInfo: false
45 }
46
f8b2c1b4 47 protected abstract notifier: Notifier
b2731bff 48 protected abstract authService: AuthService
b2731bff 49 protected abstract route: ActivatedRoute
489290b8 50 protected abstract serverService: ServerService
bbe0f064 51 protected abstract screenService: ScreenService
489290b8 52 protected abstract router: Router
9bf9d2a5 53 abstract titlePage: string
c88593f7 54
9af61e84 55 private resizeSubscription: Subscription
489290b8
C
56 private angularState: number
57
58 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
9af61e84 59
c199c427 60 abstract generateSyndicationList (): void
fd45e8f4 61
b2731bff
C
62 get user () {
63 return this.authService.getUser()
64 }
65
fd45e8f4
C
66 ngOnInit () {
67 // Subscribe to route changes
5b5e333f 68 const routeParams = this.route.snapshot.queryParams
2bbb3412 69 this.loadRouteParams(routeParams)
a2b817d3 70
9af61e84 71 this.resizeSubscription = fromEvent(window, 'resize')
db400f44 72 .pipe(debounceTime(500))
6194c1b4 73 .subscribe(() => this.calcPageSizes())
3290f37c 74
6194c1b4 75 this.calcPageSizes()
489290b8 76 if (this.loadOnInit === true) this.loadMoreVideos()
fd45e8f4
C
77 }
78
9af61e84
C
79 ngOnDestroy () {
80 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
81 }
82
489290b8
C
83 disableForReuse () {
84 this.disabled = true
89724816
C
85 }
86
489290b8
C
87 enabledForReuse () {
88 this.disabled = false
89724816
C
89 }
90
489290b8
C
91 videoById (index: number, video: Video) {
92 return video.id
2bbb3412
C
93 }
94
95 onNearOfBottom () {
489290b8 96 if (this.disabled) return
2bbb3412 97
489290b8
C
98 // Last page
99 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
0cd4344f 100
489290b8 101 this.pagination.currentPage += 1
a8ecc6f6 102
489290b8 103 this.setScrollRouteParams()
a8ecc6f6 104
489290b8
C
105 this.loadMoreVideos()
106 }
fd45e8f4 107
489290b8
C
108 loadMoreVideos () {
109 const observable = this.getVideosObservable(this.pagination.currentPage)
fd45e8f4
C
110
111 observable.subscribe(
112 ({ videos, totalVideos }) => {
fd45e8f4 113 this.pagination.totalItems = totalVideos
489290b8 114 this.videos = this.videos.concat(videos)
693263e9
C
115
116 this.onMoreVideos()
fd45e8f4 117 },
017c3dca 118
489290b8
C
119 error => this.notifier.error(error.message)
120 )
2bbb3412
C
121 }
122
489290b8
C
123 reloadVideos () {
124 this.pagination.currentPage = 1
125 this.videos = []
126 this.loadMoreVideos()
fd45e8f4
C
127 }
128
489290b8
C
129 toggleModerationDisplay () {
130 throw new Error('toggleModerationDisplay is not implemented')
fd45e8f4
C
131 }
132
3a0fb65c
C
133 removeVideoFromArray (video: Video) {
134 this.videos = this.videos.filter(v => v.id !== video.id)
135 }
136
693263e9
C
137 // On videos hook for children that want to do something
138 protected onMoreVideos () { /* empty */ }
139
fd45e8f4 140 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
489290b8
C
141 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
142 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
143 this.angularState = routeParams[ 'a-state' ]
0cd4344f 144 }
6194c1b4
C
145
146 private calcPageSizes () {
489290b8 147 if (this.screenService.isInMobileView()) {
6194c1b4 148 this.pagination.itemsPerPage = 5
6194c1b4 149 }
489290b8 150 }
6194c1b4 151
489290b8
C
152 private setScrollRouteParams () {
153 // Already set
154 if (this.angularState) return
6194c1b4 155
489290b8 156 this.angularState = 42
6194c1b4 157
489290b8
C
158 const queryParams = {
159 'a-state': this.angularState,
160 categoryOneOf: this.categoryOneOf
9af61e84 161 }
6194c1b4 162
489290b8
C
163 let path = this.router.url
164 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
165
166 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
6194c1b4 167 }
fd45e8f4 168}