Lazy load dropdown info on click
[oweals/peertube.git] / client / src / app / shared / video / video-actions-dropdown.component.ts
1 import { AfterViewInit, Component, EventEmitter, Input, OnChanges, Output, ViewChild } from '@angular/core'
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'
16
17 export 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 })
31 export class VideoActionsDropdownComponent implements OnChanges {
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   }
49   @Input() placement = 'left'
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 () {
82     this.buildActions()
83   }
84
85   isUserLoggedIn () {
86     return this.authService.isLoggedIn()
87   }
88
89   loadDropdownInformation () {
90     if (!this.isUserLoggedIn() || this.loaded === true) return
91
92     this.loaded = true
93
94     if (this.displayOptions.playlist) this.playlistAdd.load()
95   }
96
97   /* Show modals */
98
99   showDownloadModal () {
100     this.videoDownloadModal.show(this.video as VideoDetails)
101   }
102
103   showReportModal () {
104     this.videoReportModal.show()
105   }
106
107   showBlacklistModal () {
108     this.videoBlacklistModal.show()
109   }
110
111   /* Actions checker */
112
113   isVideoUpdatable () {
114     return this.video.isUpdatableBy(this.user)
115   }
116
117   isVideoRemovable () {
118     return this.video.isRemovableBy(this.user)
119   }
120
121   isVideoBlacklistable () {
122     return this.video.isBlackistableBy(this.user)
123   }
124
125   isVideoUnblacklistable () {
126     return this.video.isUnblacklistableBy(this.user)
127   }
128
129   isVideoDownloadable () {
130     return this.video && this.video instanceof VideoDetails && this.video.downloadEnabled
131   }
132
133   /* Action handlers */
134
135   async unblacklistVideo () {
136     const confirmMessage = this.i18n(
137       'Do you really want to remove this video from the blacklist? It will be available again in the videos list.'
138     )
139
140     const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist'))
141     if (res === false) return
142
143     this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe(
144       () => {
145         this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name }))
146
147         this.video.blacklisted = false
148         this.video.blacklistedReason = null
149
150         this.videoUnblacklisted.emit()
151       },
152
153       err => this.notifier.error(err.message)
154     )
155   }
156
157   async removeVideo () {
158     const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
159     if (res === false) return
160
161     this.videoService.removeVideo(this.video.id)
162         .subscribe(
163           () => {
164             this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }))
165
166             this.videoRemoved.emit()
167           },
168
169           error => this.notifier.error(error.message)
170         )
171   }
172
173   onVideoBlacklisted () {
174     this.videoBlacklisted.emit()
175   }
176
177   getPlaylistDropdownPlacement () {
178     if (this.screenService.isInSmallView()) {
179       return 'bottom-right'
180     }
181
182     return 'bottom-left bottom-right'
183   }
184
185   private buildActions () {
186     this.videoActions = []
187
188     if (this.authService.isLoggedIn()) {
189       this.videoActions.push([
190         {
191           label: this.i18n('Save to playlist'),
192           handler: () => this.playlistDropdown.toggle(),
193           isDisplayed: () => this.displayOptions.playlist,
194           iconName: 'playlist-add'
195         }
196       ])
197
198       this.videoActions.push([
199         {
200           label: this.i18n('Download'),
201           handler: () => this.showDownloadModal(),
202           isDisplayed: () => this.displayOptions.download && this.isVideoDownloadable(),
203           iconName: 'download'
204         },
205         {
206           label: this.i18n('Update'),
207           linkBuilder: ({ video }) => [ '/videos/update', video.uuid ],
208           iconName: 'edit',
209           isDisplayed: () => this.displayOptions.update && this.isVideoUpdatable()
210         },
211         {
212           label: this.i18n('Blacklist'),
213           handler: () => this.showBlacklistModal(),
214           iconName: 'no',
215           isDisplayed: () => this.displayOptions.blacklist && this.isVideoBlacklistable()
216         },
217         {
218           label: this.i18n('Unblacklist'),
219           handler: () => this.unblacklistVideo(),
220           iconName: 'undo',
221           isDisplayed: () => this.displayOptions.blacklist && this.isVideoUnblacklistable()
222         },
223         {
224           label: this.i18n('Delete'),
225           handler: () => this.removeVideo(),
226           isDisplayed: () => this.displayOptions.delete && this.isVideoRemovable(),
227           iconName: 'delete'
228         }
229       ])
230
231       this.videoActions.push([
232         {
233           label: this.i18n('Report'),
234           handler: () => this.showReportModal(),
235           isDisplayed: () => this.displayOptions.report,
236           iconName: 'alert'
237         }
238       ])
239     }
240   }
241 }