]>
Commit | Line | Data |
---|---|---|
72675ebe | 1 | import { AfterViewInit, Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core' |
3a0fb65c C |
2 | import { I18n } from '@ngx-translate/i18n-polyfill' |
3 | import { DropdownAction, DropdownButtonSize, DropdownDirection } from '@app/shared/buttons/action-dropdown.component' | |
4 | import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core' | |
5 | import { BlocklistService } from '@app/shared/blocklist' | |
6 | import { Video } from '@app/shared/video/video.model' | |
7 | import { VideoService } from '@app/shared/video/video.service' | |
8 | import { VideoDetails } from '@app/shared/video/video-details.model' | |
9 | import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' | |
10 | import { VideoAddToPlaylistComponent } from '@app/shared/video-playlist/video-add-to-playlist.component' | |
11 | import { VideoDownloadComponent } from '@app/shared/video/modals/video-download.component' | |
12 | import { VideoReportComponent } from '@app/shared/video/modals/video-report.component' | |
13 | import { VideoBlacklistComponent } from '@app/shared/video/modals/video-blacklist.component' | |
14 | import { VideoBlacklistService } from '@app/shared/video-blacklist' | |
15 | import { ScreenService } from '@app/shared/misc/screen.service' | |
8ba9c205 | 16 | import { VideoCaption } from '@shared/models' |
3a0fb65c C |
17 | |
18 | export type VideoActionsDisplayType = { | |
19 | playlist?: boolean | |
20 | download?: boolean | |
21 | update?: boolean | |
22 | blacklist?: boolean | |
23 | delete?: boolean | |
24 | report?: boolean | |
25 | } | |
26 | ||
27 | @Component({ | |
28 | selector: 'my-video-actions-dropdown', | |
29 | templateUrl: './video-actions-dropdown.component.html', | |
30 | styleUrls: [ './video-actions-dropdown.component.scss' ] | |
31 | }) | |
4da22f64 | 32 | export class VideoActionsDropdownComponent implements OnChanges { |
f36da21e C |
33 | @ViewChild('playlistDropdown', { static: false }) playlistDropdown: NgbDropdown |
34 | @ViewChild('playlistAdd', { static: false }) playlistAdd: VideoAddToPlaylistComponent | |
3a0fb65c | 35 | |
f36da21e C |
36 | @ViewChild('videoDownloadModal', { static: false }) videoDownloadModal: VideoDownloadComponent |
37 | @ViewChild('videoReportModal', { static: false }) videoReportModal: VideoReportComponent | |
38 | @ViewChild('videoBlacklistModal', { static: false }) videoBlacklistModal: VideoBlacklistComponent | |
3a0fb65c C |
39 | |
40 | @Input() video: Video | VideoDetails | |
8ba9c205 | 41 | @Input() videoCaptions: VideoCaption[] = [] |
3a0fb65c C |
42 | |
43 | @Input() displayOptions: VideoActionsDisplayType = { | |
44 | playlist: false, | |
45 | download: true, | |
46 | update: true, | |
47 | blacklist: true, | |
48 | delete: true, | |
49 | report: true | |
50 | } | |
8dfceec4 | 51 | @Input() placement = 'left' |
3a0fb65c C |
52 | |
53 | @Input() label: string | |
54 | ||
55 | @Input() buttonStyled = false | |
56 | @Input() buttonSize: DropdownButtonSize = 'normal' | |
57 | @Input() buttonDirection: DropdownDirection = 'vertical' | |
58 | ||
59 | @Output() videoRemoved = new EventEmitter() | |
60 | @Output() videoUnblacklisted = new EventEmitter() | |
61 | @Output() videoBlacklisted = new EventEmitter() | |
689a4f69 | 62 | @Output() modalOpened = new EventEmitter() |
3a0fb65c C |
63 | |
64 | videoActions: DropdownAction<{ video: Video }>[][] = [] | |
65 | ||
66 | private loaded = false | |
67 | ||
68 | constructor ( | |
69 | private authService: AuthService, | |
70 | private notifier: Notifier, | |
71 | private confirmService: ConfirmService, | |
72 | private videoBlacklistService: VideoBlacklistService, | |
73 | private serverService: ServerService, | |
74 | private screenService: ScreenService, | |
75 | private videoService: VideoService, | |
76 | private blocklistService: BlocklistService, | |
77 | private i18n: I18n | |
78 | ) { } | |
79 | ||
80 | get user () { | |
81 | return this.authService.getUser() | |
82 | } | |
83 | ||
84 | ngOnChanges () { | |
1c8ddbfa C |
85 | if (this.loaded) { |
86 | this.loaded = false | |
78d60e63 | 87 | this.playlistAdd.reload() |
1c8ddbfa C |
88 | } |
89 | ||
3a0fb65c C |
90 | this.buildActions() |
91 | } | |
92 | ||
93 | isUserLoggedIn () { | |
94 | return this.authService.isLoggedIn() | |
95 | } | |
96 | ||
97 | loadDropdownInformation () { | |
98 | if (!this.isUserLoggedIn() || this.loaded === true) return | |
99 | ||
100 | this.loaded = true | |
101 | ||
102 | if (this.displayOptions.playlist) this.playlistAdd.load() | |
103 | } | |
104 | ||
105 | /* Show modals */ | |
106 | ||
107 | showDownloadModal () { | |
689a4f69 C |
108 | this.modalOpened.emit() |
109 | ||
8ba9c205 | 110 | this.videoDownloadModal.show(this.video as VideoDetails, this.videoCaptions) |
3a0fb65c C |
111 | } |
112 | ||
113 | showReportModal () { | |
689a4f69 C |
114 | this.modalOpened.emit() |
115 | ||
3a0fb65c C |
116 | this.videoReportModal.show() |
117 | } | |
118 | ||
119 | showBlacklistModal () { | |
689a4f69 C |
120 | this.modalOpened.emit() |
121 | ||
3a0fb65c C |
122 | this.videoBlacklistModal.show() |
123 | } | |
124 | ||
125 | /* Actions checker */ | |
126 | ||
127 | isVideoUpdatable () { | |
128 | return this.video.isUpdatableBy(this.user) | |
129 | } | |
130 | ||
131 | isVideoRemovable () { | |
132 | return this.video.isRemovableBy(this.user) | |
133 | } | |
134 | ||
135 | isVideoBlacklistable () { | |
136 | return this.video.isBlackistableBy(this.user) | |
137 | } | |
138 | ||
139 | isVideoUnblacklistable () { | |
140 | return this.video.isUnblacklistableBy(this.user) | |
141 | } | |
142 | ||
72675ebe C |
143 | isVideoDownloadable () { |
144 | return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled | |
145 | } | |
146 | ||
3a0fb65c C |
147 | /* Action handlers */ |
148 | ||
149 | async unblacklistVideo () { | |
150 | const confirmMessage = this.i18n( | |
151 | 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.' | |
152 | ) | |
153 | ||
154 | const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist')) | |
155 | if (res === false) return | |
156 | ||
157 | this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe( | |
158 | () => { | |
159 | this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name })) | |
160 | ||
161 | this.video.blacklisted = false | |
162 | this.video.blacklistedReason = null | |
163 | ||
164 | this.videoUnblacklisted.emit() | |
165 | }, | |
166 | ||
167 | err => this.notifier.error(err.message) | |
168 | ) | |
169 | } | |
170 | ||
171 | async removeVideo () { | |
689a4f69 C |
172 | this.modalOpened.emit() |
173 | ||
3a0fb65c C |
174 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete')) |
175 | if (res === false) return | |
176 | ||
177 | this.videoService.removeVideo(this.video.id) | |
178 | .subscribe( | |
179 | () => { | |
180 | this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name })) | |
181 | ||
182 | this.videoRemoved.emit() | |
183 | }, | |
184 | ||
185 | error => this.notifier.error(error.message) | |
186 | ) | |
187 | } | |
188 | ||
189 | onVideoBlacklisted () { | |
190 | this.videoBlacklisted.emit() | |
191 | } | |
192 | ||
193 | getPlaylistDropdownPlacement () { | |
194 | if (this.screenService.isInSmallView()) { | |
195 | return 'bottom-right' | |
196 | } | |
197 | ||
198 | return 'bottom-left bottom-right' | |
199 | } | |
200 | ||
201 | private buildActions () { | |
f238aec5 C |
202 | this.videoActions = [ |
203 | [ | |
3a0fb65c C |
204 | { |
205 | label: this.i18n('Save to playlist'), | |
206 | handler: () => this.playlistDropdown.toggle(), | |
f238aec5 | 207 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.playlist, |
3a0fb65c C |
208 | iconName: 'playlist-add' |
209 | } | |
f238aec5 C |
210 | ], |
211 | [ | |
3a0fb65c C |
212 | { |
213 | label: this.i18n('Download'), | |
214 | handler: () => this.showDownloadModal(), | |
72675ebe | 215 | isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(), |
3a0fb65c C |
216 | iconName: 'download' |
217 | }, | |
218 | { | |
219 | label: this.i18n('Update'), | |
220 | linkBuilder: ({ video }) => [ '/videos/update', video.uuid ], | |
221 | iconName: 'edit', | |
f238aec5 | 222 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.update && this.isVideoUpdatable() |
3a0fb65c C |
223 | }, |
224 | { | |
225 | label: this.i18n('Blacklist'), | |
226 | handler: () => this.showBlacklistModal(), | |
227 | iconName: 'no', | |
f238aec5 | 228 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoBlacklistable() |
3a0fb65c C |
229 | }, |
230 | { | |
231 | label: this.i18n('Unblacklist'), | |
232 | handler: () => this.unblacklistVideo(), | |
233 | iconName: 'undo', | |
f238aec5 | 234 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.blacklist && this.isVideoUnblacklistable() |
3a0fb65c C |
235 | }, |
236 | { | |
237 | label: this.i18n('Delete'), | |
238 | handler: () => this.removeVideo(), | |
f238aec5 | 239 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.delete && this.isVideoRemovable(), |
3a0fb65c C |
240 | iconName: 'delete' |
241 | } | |
f238aec5 C |
242 | ], |
243 | [ | |
3a0fb65c C |
244 | { |
245 | label: this.i18n('Report'), | |
246 | handler: () => this.showReportModal(), | |
f238aec5 | 247 | isDisplayed: () => this.authService.isLoggedIn() && this.displayOptions.report, |
3a0fb65c C |
248 | iconName: 'alert' |
249 | } | |
f238aec5 C |
250 | ] |
251 | ] | |
3a0fb65c C |
252 | } |
253 | } |