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