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