]>
Commit | Line | Data |
---|---|---|
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 | if (reset) this.hasDoneFirstQuery = false | |
114 | ||
115 | this.getVideosObservable(this.pagination.currentPage) | |
116 | .subscribe({ | |
117 | next: ({ data }) => { | |
118 | this.hasDoneFirstQuery = true | |
119 | this.lastQueryLength = data.length | |
120 | ||
121 | if (reset) this.videos = [] | |
122 | this.videos = this.videos.concat(data) | |
123 | this.videosModel = this.videos | |
124 | ||
125 | this.onDataSubject.next(data) | |
126 | }, | |
127 | ||
128 | error: err => { | |
129 | const message = $localize`Cannot load more videos. Try again later.` | |
130 | ||
131 | console.error(message, { err }) | |
132 | this.notifier.error(message) | |
133 | } | |
134 | }) | |
135 | } | |
136 | ||
137 | reloadVideos () { | |
138 | this.pagination.currentPage = 1 | |
139 | this.loadMoreVideos(true) | |
140 | } | |
141 | ||
142 | removeVideoFromArray (video: Video) { | |
143 | this.videos = this.videos.filter(v => v.id !== video.id) | |
144 | } | |
145 | } |