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