1 import { Observable, Subject } from 'rxjs'
2 import { AfterContentInit, Component, ContentChildren, EventEmitter, Input, Output, QueryList, TemplateRef } from '@angular/core'
3 import { ComponentPagination, Notifier, User } from '@app/core'
4 import { logger } from '@root-helpers/logger'
5 import { ResultList, VideoSortField } from '@shared/models'
6 import { PeerTubeTemplateDirective, Video } from '../shared-main'
7 import { MiniatureDisplayOptions } from './video-miniature.component'
9 export type SelectionType = { [ id: number ]: boolean }
12 selector: 'my-videos-selection',
13 templateUrl: './videos-selection.component.html',
14 styleUrls: [ './videos-selection.component.scss' ]
16 export class VideosSelectionComponent implements AfterContentInit {
18 @Input() pagination: ComponentPagination
20 @Input() titlePage: string
22 @Input() miniatureDisplayOptions: MiniatureDisplayOptions
24 @Input() noResultMessage = $localize`No results.`
25 @Input() enableSelection = true
27 @Input() disabled = false
29 @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>>
31 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>>
33 @Output() selectionChange = new EventEmitter<SelectionType>()
34 @Output() videosModelChange = new EventEmitter<Video[]>()
36 _selection: SelectionType = {}
38 rowButtonsTemplate: TemplateRef<any>
39 globalButtonsTemplate: TemplateRef<any>
42 sort: VideoSortField = '-publishedAt'
44 onDataSubject = new Subject<any[]>()
46 hasDoneFirstQuery = false
48 private lastQueryLength: number
51 private notifier: Notifier
54 @Input() get selection () {
55 return this._selection
58 set selection (selection: SelectionType) {
59 this._selection = selection
60 this.selectionChange.emit(this._selection)
63 @Input() get videosModel () {
67 set videosModel (videos: Video[]) {
69 this.videosModelChange.emit(this.videos)
72 ngAfterContentInit () {
74 const t = this.templates.find(t => t.name === 'rowButtons')
75 if (t) this.rowButtonsTemplate = t.template
79 const t = this.templates.find(t => t.name === 'globalButtons')
80 if (t) this.globalButtonsTemplate = t.template
86 getVideosObservable (page: number) {
87 return this.getVideosObservableFunction(page, this.sort)
90 abortSelectionMode () {
94 isInSelectionMode () {
95 return Object.keys(this._selection).some(k => this._selection[k] === true)
98 videoById (index: number, video: Video) {
103 if (this.disabled) return
106 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
108 this.pagination.currentPage += 1
110 this.loadMoreVideos()
113 loadMoreVideos (reset = false) {
114 if (reset) this.hasDoneFirstQuery = false
116 this.getVideosObservable(this.pagination.currentPage)
118 next: ({ data }) => {
119 this.hasDoneFirstQuery = true
120 this.lastQueryLength = data.length
122 if (reset) this.videos = []
123 this.videos = this.videos.concat(data)
124 this.videosModel = this.videos
126 this.onDataSubject.next(data)
130 const message = $localize`Cannot load more videos. Try again later.`
132 logger.error(message, err)
133 this.notifier.error(message)
139 this.pagination.currentPage = 1
140 this.loadMoreVideos(true)
143 removeVideoFromArray (video: Video) {
144 this.videos = this.videos.filter(v => v.id !== video.id)