]>
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 { logger } from '@root-helpers/logger' | |
5 | import { objectKeysTyped } from '@shared/core-utils' | |
6 | import { ResultList, VideosExistInPlaylists, VideoSortField } from '@shared/models' | |
7 | import { PeerTubeTemplateDirective, Video } from '../shared-main' | |
8 | import { MiniatureDisplayOptions } from './video-miniature.component' | |
9 | ||
10 | export 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 | }) | |
17 | export class VideosSelectionComponent implements AfterContentInit { | |
18 | @Input() videosContainedInPlaylists: VideosExistInPlaylists | |
19 | @Input() user: User | |
20 | @Input() pagination: ComponentPagination | |
21 | ||
22 | @Input() titlePage: string | |
23 | ||
24 | @Input() miniatureDisplayOptions: MiniatureDisplayOptions | |
25 | ||
26 | @Input() noResultMessage = $localize`No results.` | |
27 | @Input() enableSelection = true | |
28 | ||
29 | @Input() disabled = false | |
30 | ||
31 | @Input() getVideosObservableFunction: (page: number, sort?: VideoSortField) => Observable<ResultList<Video>> | |
32 | ||
33 | @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'rowButtons' | 'globalButtons'>> | |
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 | ||
43 | videos: Video[] = [] | |
44 | sort: VideoSortField = '-publishedAt' | |
45 | ||
46 | onDataSubject = new Subject<any[]>() | |
47 | ||
48 | hasDoneFirstQuery = false | |
49 | ||
50 | private lastQueryLength: number | |
51 | ||
52 | constructor ( | |
53 | private notifier: Notifier | |
54 | ) { } | |
55 | ||
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 | ||
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 | } | |
84 | ||
85 | this.loadMoreVideos() | |
86 | } | |
87 | ||
88 | getVideosObservable (page: number) { | |
89 | return this.getVideosObservableFunction(page, this.sort) | |
90 | } | |
91 | ||
92 | abortSelectionMode () { | |
93 | this._selection = {} | |
94 | } | |
95 | ||
96 | isInSelectionMode () { | |
97 | return objectKeysTyped(this._selection).some(k => this._selection[k] === true) | |
98 | } | |
99 | ||
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) { | |
116 | if (reset) this.hasDoneFirstQuery = false | |
117 | ||
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 | ||
134 | logger.error(message, err) | |
135 | this.notifier.error(message) | |
136 | } | |
137 | }) | |
138 | } | |
139 | ||
140 | reloadVideos () { | |
141 | this.pagination.currentPage = 1 | |
142 | this.loadMoreVideos(true) | |
143 | } | |
144 | ||
145 | removeVideoFromArray (video: Video) { | |
146 | this.videos = this.videos.filter(v => v.id !== video.id) | |
147 | } | |
148 | } |