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