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 { MiniatureDisplayOptions, 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 displayOptions: MiniatureDisplayOptions = {
47 protected abstract notifier: Notifier
48 protected abstract authService: AuthService
49 protected abstract route: ActivatedRoute
50 protected abstract serverService: ServerService
51 protected abstract screenService: ScreenService
52 protected abstract router: Router
53 abstract titlePage: string
55 private resizeSubscription: Subscription
56 private angularState: number
58 abstract getVideosObservable (page: number): Observable<{ videos: Video[], totalVideos: number }>
60 abstract generateSyndicationList (): void
63 return this.authService.getUser()
67 // Subscribe to route changes
68 const routeParams = this.route.snapshot.queryParams
69 this.loadRouteParams(routeParams)
71 this.resizeSubscription = fromEvent(window, 'resize')
72 .pipe(debounceTime(500))
73 .subscribe(() => this.calcPageSizes())
76 if (this.loadOnInit === true) this.loadMoreVideos()
80 if (this.resizeSubscription) this.resizeSubscription.unsubscribe()
91 videoById (index: number, video: Video) {
96 if (this.disabled) return
99 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
101 this.pagination.currentPage += 1
103 this.setScrollRouteParams()
105 this.loadMoreVideos()
109 const observable = this.getVideosObservable(this.pagination.currentPage)
111 observable.subscribe(
112 ({ videos, totalVideos }) => {
113 this.pagination.totalItems = totalVideos
114 this.videos = this.videos.concat(videos)
119 error => this.notifier.error(error.message)
124 this.pagination.currentPage = 1
126 this.loadMoreVideos()
129 toggleModerationDisplay () {
130 throw new Error('toggleModerationDisplay is not implemented')
133 removeVideoFromArray (video: Video) {
134 this.videos = this.videos.filter(v => v.id !== video.id)
137 // On videos hook for children that want to do something
138 protected onMoreVideos () { /* empty */ }
140 protected loadRouteParams (routeParams: { [ key: string ]: any }) {
141 this.sort = routeParams[ 'sort' ] as VideoSortField || this.defaultSort
142 this.categoryOneOf = routeParams[ 'categoryOneOf' ]
143 this.angularState = routeParams[ 'a-state' ]
146 private calcPageSizes () {
147 if (this.screenService.isInMobileView()) {
148 this.pagination.itemsPerPage = 5
152 private setScrollRouteParams () {
154 if (this.angularState) return
156 this.angularState = 42
158 const queryParams = {
159 'a-state': this.angularState,
160 categoryOneOf: this.categoryOneOf
163 let path = this.router.url
164 if (!path || path === '/') path = this.serverService.getConfig().instance.defaultClientRoute
166 this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })