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'
15 export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableForReuseHook {
16 pagination: ComponentPagination = {
21 sort: VideoSortField = '-publishedAt'
23 categoryOneOf?: number
24 defaultSort: VideoSortField = '-publishedAt'
26 syndicationItems: Syndication[] = []
30 ownerDisplayType: OwnerDisplayType = 'account'
31 displayModerationBlock = false
33 displayVideoActions = true
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
45 private resizeSubscription: Subscription
46 private angularState: number
48 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
50 abstract generateSyndicationList (): void
53 return this.authService.getUser()
57 // Subscribe to route changes
58 const routeParams = this.route.snapshot.queryParams
59 this.loadRouteParams(routeParams)
61 this.resizeSubscription = fromEvent(window, 'resize')
62 .pipe(debounceTime(500))
63 .subscribe(() => this.calcPageSizes())
66 if (this.loadOnInit === true) this.loadMoreVideos()
70 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
81 videoById (index: number, video: Video) {
86 if (this.disabled) return
89 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
91 this.pagination.currentPage += 1
93 this.setScrollRouteParams()
99 const observable = this.getVideosObservable(this.pagination.currentPage)
101 observable.subscribe(
102 ({ videos, totalVideos }) => {
103 this.pagination.totalItems = totalVideos
104 this.videos = this.videos.concat(videos)
109 error => this.notifier.error(error.message)
114 this.pagination.currentPage = 1
116 this.loadMoreVideos()
119 toggleModerationDisplay () {
120 throw new Error('toggleModerationDisplay is not implemented')
123 removeVideoFromArray (video: Video) {
124 this.videos = this.videos.filter(v => v.id !== video.id)
127 // On videos hook for children that want to do something
128 protected onMoreVideos () { /* empty */ }
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' ]
136 private calcPageSizes () {
137 if (this.screenService.isInMobileView()) {
138 this.pagination.itemsPerPage = 5
142 private setScrollRouteParams () {
144 if (this.angularState) return
146 this.angularState = 42
148 const queryParams = {
149 'a-state': this.angularState,
150 categoryOneOf: this.categoryOneOf
153 let path = this.router.url
154 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
156 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })