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