]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-video-miniature/videos-selection.component.ts
Add video filters to common video pages
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / videos-selection.component.ts
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'
7
8 export type SelectionType = { [ id: number ]: boolean }
9
10 @Component({
11 selector: 'my-videos-selection',
12 templateUrl: './videos-selection.component.html',
13 styleUrls: [ './videos-selection.component.scss' ]
14 })
15 export class VideosSelectionComponent implements AfterContentInit {
16 @Input() user: User
17 @Input() pagination: ComponentPagination
18
19 @Input() titlePage: string
20
21 @Input() miniatureDisplayOptions: MiniatureDisplayOptions
22
23 @Input() noResultMessage = $localize`No results.`
24 @Input() enableSelection = true
25
26 @Input() disabled = false
27
28 @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>>
29
30 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>>
31
32 @Output() selectionChange = new EventEmitter<SelectionType>()
33 @Output() videosModelChange = new EventEmitter<Video[]>()
34
35 _selection: SelectionType = {}
36
37 rowButtonsTemplate: TemplateRef<any>
38 globalButtonsTemplate: TemplateRef<any>
39
40 videos: Video[] = []
41 sort: VideoSortField = '-publishedAt'
42
43 onDataSubject = new Subject<any[]>()
44
45 hasDoneFirstQuery = false
46
47 private lastQueryLength: number
48
49 constructor (
50 private notifier: Notifier
51 ) { }
52
53 @Input() get selection () {
54 return this._selection
55 }
56
57 set selection (selection: SelectionType) {
58 this._selection = selection
59 this.selectionChange.emit(this._selection)
60 }
61
62 @Input() get videosModel () {
63 return this.videos
64 }
65
66 set videosModel (videos: Video[]) {
67 this.videos = videos
68 this.videosModelChange.emit(this.videos)
69 }
70
71 ngAfterContentInit () {
72 {
73 const t = this.templates.find(t => t.name === 'rowButtons')
74 if (t) this.rowButtonsTemplate = t.template
75 }
76
77 {
78 const t = this.templates.find(t => t.name === 'globalButtons')
79 if (t) this.globalButtonsTemplate = t.template
80 }
81
82 this.loadMoreVideos()
83 }
84
85 getVideosObservable (page: number) {
86 return this.getVideosObservableFunction(page, this.sort)
87 }
88
89 abortSelectionMode () {
90 this._selection = {}
91 }
92
93 isInSelectionMode () {
94 return Object.keys(this._selection).some(k => this._selection[k] === true)
95 }
96
97 videoById (index: number, video: Video) {
98 return video.id
99 }
100
101 onNearOfBottom () {
102 if (this.disabled) return
103
104 // No more results
105 if (this.lastQueryLength !== undefined && this.lastQueryLength < this.pagination.itemsPerPage) return
106
107 this.pagination.currentPage += 1
108
109 this.loadMoreVideos()
110 }
111
112 loadMoreVideos (reset = false) {
113 this.getVideosObservable(this.pagination.currentPage)
114 .subscribe({
115 next: ({ data }) => {
116 this.hasDoneFirstQuery = true
117 this.lastQueryLength = data.length
118
119 if (reset) this.videos = []
120 this.videos = this.videos.concat(data)
121 this.videosModel = this.videos
122
123 this.onDataSubject.next(data)
124 },
125
126 error: err => {
127 const message = $localize`Cannot load more videos. Try again later.`
128
129 console.error(message, { err })
130 this.notifier.error(message)
131 }
132 })
133 }
134
135 reloadVideos () {
136 this.pagination.currentPage = 1
137 this.loadMoreVideos(true)
138 }
139
140 removeVideoFromArray (video: Video) {
141 this.videos = this.videos.filter(v => v.id !== video.id)
142 }
143 }