]>
Commit | Line | Data |
---|---|---|
dd24f1bb C |
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' | |
67ed6552 C |
4 | import { ResultList, VideoSortField } from '@shared/models' |
5 | import { PeerTubeTemplateDirective, Video } from '../shared-main' | |
733dbc53 | 6 | import { MiniatureDisplayOptions } from './video-miniature.component' |
693263e9 C |
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 | }) | |
dd24f1bb | 15 | export class VideosSelectionComponent implements AfterContentInit { |
241609f1 | 16 | @Input() user: User |
8c6781e9 | 17 | @Input() pagination: ComponentPagination |
dd24f1bb | 18 | |
693263e9 | 19 | @Input() titlePage: string |
dd24f1bb | 20 | |
693263e9 | 21 | @Input() miniatureDisplayOptions: MiniatureDisplayOptions |
dd24f1bb | 22 | |
2e46eb97 C |
23 | @Input() noResultMessage = $localize`No results.` |
24 | @Input() enableSelection = true | |
dd24f1bb C |
25 | |
26 | @Input() disabled = false | |
c4a6f790 | 27 | |
93cae479 | 28 | @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>> |
c4a6f790 | 29 | |
421d935d | 30 | @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>> |
693263e9 C |
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 | ||
dd24f1bb C |
40 | videos: Video[] = [] |
41 | sort: VideoSortField = '-publishedAt' | |
42 | ||
43 | onDataSubject = new Subject<any[]>() | |
44 | ||
45 | hasDoneFirstQuery = false | |
46 | ||
47 | private lastQueryLength: number | |
48 | ||
693263e9 | 49 | constructor ( |
dd24f1bb C |
50 | private notifier: Notifier |
51 | ) { } | |
693263e9 | 52 | |
693263e9 C |
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 | ||
8c6781e9 C |
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 | } | |
8c6781e9 | 81 | |
dd24f1bb | 82 | this.loadMoreVideos() |
693263e9 C |
83 | } |
84 | ||
85 | getVideosObservable (page: number) { | |
86 | return this.getVideosObservableFunction(page, this.sort) | |
87 | } | |
88 | ||
89 | abortSelectionMode () { | |
90 | this._selection = {} | |
91 | } | |
92 | ||
93 | isInSelectionMode () { | |
9df52d66 | 94 | return Object.keys(this._selection).some(k => this._selection[k] === true) |
693263e9 C |
95 | } |
96 | ||
dd24f1bb C |
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) | |
693263e9 C |
138 | } |
139 | ||
dd24f1bb C |
140 | removeVideoFromArray (video: Video) { |
141 | this.videos = this.videos.filter(v => v.id !== video.id) | |
693263e9 C |
142 | } |
143 | } |