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