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, VideosExistInPlaylists, 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 {
17 @Input() videosContainedInPlaylists: VideosExistInPlaylists
19 @Input() pagination: ComponentPagination
21 @Input() titlePage: string
23 @Input() miniatureDisplayOptions: MiniatureDisplayOptions
25 @Input() noResultMessage = $localize`No results.`
26 @Input() enableSelection = true
28 @Input() disabled = false
30 @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>>
32 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>>
34 @Output() selectionChange = new EventEmitter<SelectionType>()
35 @Output() videosModelChange = new EventEmitter<Video[]>()
37 _selection: SelectionType = {}
39 rowButtonsTemplate: TemplateRef<any>
40 globalButtonsTemplate: TemplateRef<any>
43 sort: VideoSortField = '-publishedAt'
45 onDataSubject = new Subject<any[]>()
47 hasDoneFirstQuery = false
49 private lastQueryLength: number
52 private notifier: Notifier
55 @Input() get selection () {
56 return this._selection
59 set selection (selection: SelectionType) {
60 this._selection = selection
61 this.selectionChange.emit(this._selection)
64 @Input() get videosModel () {
68 set videosModel (videos: Video[]) {
70 this.videosModelChange.emit(this.videos)
73 ngAfterContentInit () {
75 const t = this.templates.find(t => t.name === 'rowButtons')
76 if (t) this.rowButtonsTemplate = t.template
80 const t = this.templates.find(t => t.name === 'globalButtons')
81 if (t) this.globalButtonsTemplate = t.template
87 getVideosObservable (page: number) {
88 return this.getVideosObservableFunction(page, this.sort)
91 abortSelectionMode () {
95 isInSelectionMode () {
96 return Object.keys(this._selection).some(k => this._selection[k] === true)
99 videoById (index: number, video: Video) {
104 if (this.disabled) return
107 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
109 this.pagination.currentPage += 1
111 this.loadMoreVideos()
114 loadMoreVideos (reset = false) {
115 if (reset) this.hasDoneFirstQuery = false
117 this.getVideosObservable(this.pagination.currentPage)
119 next: ({ data }) => {
120 this.hasDoneFirstQuery = true
121 this.lastQueryLength = data.length
123 if (reset) this.videos = []
124 this.videos = this.videos.concat(data)
125 this.videosModel = this.videos
127 this.onDataSubject.next(data)
131 const message = $localize`Cannot load more videos. Try again later.`
133 logger.error(message, err)
134 this.notifier.error(message)
140 this.pagination.currentPage = 1
141 this.loadMoreVideos(true)
144 removeVideoFromArray (video: Video) {
145 this.videos = this.videos.filter(v => v.id !== video.id)