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 { ResultList, VideoSortField } from '@shared/models'
5 import { PeerTubeTemplateDirective, Video } from '../shared-main'
6 import { MiniatureDisplayOptions } from './video-miniature.component'
8 export type SelectionType = { [ id: number ]: boolean }
11 selector: 'my-videos-selection',
12 templateUrl: './videos-selection.component.html',
13 styleUrls: [ './videos-selection.component.scss' ]
15 export class VideosSelectionComponent implements AfterContentInit {
17 @Input() pagination: ComponentPagination
19 @Input() titlePage: string
21 @Input() miniatureDisplayOptions: MiniatureDisplayOptions
23 @Input() noResultMessage = $localize`No results.`
24 @Input() enableSelection = true
26 @Input() disabled = false
28 @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>>
30 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>>
32 @Output() selectionChange = new EventEmitter<SelectionType>()
33 @Output() videosModelChange = new EventEmitter<Video[]>()
35 _selection: SelectionType = {}
37 rowButtonsTemplate: TemplateRef<any>
38 globalButtonsTemplate: TemplateRef<any>
41 sort: VideoSortField = '-publishedAt'
43 onDataSubject = new Subject<any[]>()
45 hasDoneFirstQuery = false
47 private lastQueryLength: number
50 private notifier: Notifier
53 @Input() get selection () {
54 return this._selection
57 set selection (selection: SelectionType) {
58 this._selection = selection
59 this.selectionChange.emit(this._selection)
62 @Input() get videosModel () {
66 set videosModel (videos: Video[]) {
68 this.videosModelChange.emit(this.videos)
71 ngAfterContentInit () {
73 const t = this.templates.find(t => t.name === 'rowButtons')
74 if (t) this.rowButtonsTemplate = t.template
78 const t = this.templates.find(t => t.name === 'globalButtons')
79 if (t) this.globalButtonsTemplate = t.template
85 getVideosObservable (page: number) {
86 return this.getVideosObservableFunction(page, this.sort)
89 abortSelectionMode () {
93 isInSelectionMode () {
94 return Object.keys(this._selection).some(k => this._selection[k] === true)
97 videoById (index: number, video: Video) {
102 if (this.disabled) return
105 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
107 this.pagination.currentPage += 1
109 this.loadMoreVideos()
112 loadMoreVideos (reset = false) {
113 if (reset) this.hasDoneFirstQuery = false
115 this.getVideosObservable(this.pagination.currentPage)
117 next: ({ data }) => {
118 this.hasDoneFirstQuery = true
119 this.lastQueryLength = data.length
121 if (reset) this.videos = []
122 this.videos = this.videos.concat(data)
123 this.videosModel = this.videos
125 this.onDataSubject.next(data)
129 const message = $localize`Cannot load more videos. Try again later.`
131 console.error(message, { err })
132 this.notifier.error(message)
138 this.pagination.currentPage = 1
139 this.loadMoreVideos(true)
142 removeVideoFromArray (video: Video) {
143 this.videos = this.videos.filter(v => v.id !== video.id)