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