]>
Commit | Line | Data |
---|---|---|
1 | import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { Video } from '@app/shared/video/video.model' | |
3 | import { ServerConfig, VideoPlaylistElementType, VideoPlaylistElementUpdate } from '@shared/models' | |
4 | import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core' | |
5 | import { ActivatedRoute } from '@angular/router' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { VideoService } from '@app/shared/video/video.service' | |
8 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
9 | import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' | |
10 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
11 | import { secondsToTime } from '../../../assets/player/utils' | |
12 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
13 | ||
14 | @Component({ | |
15 | selector: 'my-video-playlist-element-miniature', | |
16 | styleUrls: [ './video-playlist-element-miniature.component.scss' ], | |
17 | templateUrl: './video-playlist-element-miniature.component.html', | |
18 | changeDetection: ChangeDetectionStrategy.OnPush | |
19 | }) | |
20 | export class VideoPlaylistElementMiniatureComponent implements OnInit { | |
21 | @ViewChild('moreDropdown') moreDropdown: NgbDropdown | |
22 | ||
23 | @Input() playlist: VideoPlaylist | |
24 | @Input() playlistElement: VideoPlaylistElement | |
25 | @Input() owned = false | |
26 | @Input() playing = false | |
27 | @Input() rowLink = false | |
28 | @Input() accountLink = true | |
29 | @Input() position: number // Keep this property because we're in the OnPush change detection strategy | |
30 | ||
31 | @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>() | |
32 | ||
33 | displayTimestampOptions = false | |
34 | ||
35 | timestampOptions: { | |
36 | startTimestampEnabled: boolean | |
37 | startTimestamp: number | |
38 | stopTimestampEnabled: boolean | |
39 | stopTimestamp: number | |
40 | } = {} as any | |
41 | ||
42 | private serverConfig: ServerConfig | |
43 | ||
44 | constructor ( | |
45 | private authService: AuthService, | |
46 | private serverService: ServerService, | |
47 | private notifier: Notifier, | |
48 | private confirmService: ConfirmService, | |
49 | private route: ActivatedRoute, | |
50 | private i18n: I18n, | |
51 | private videoService: VideoService, | |
52 | private videoPlaylistService: VideoPlaylistService, | |
53 | private cdr: ChangeDetectorRef | |
54 | ) {} | |
55 | ||
56 | ngOnInit (): void { | |
57 | this.serverConfig = this.serverService.getTmpConfig() | |
58 | this.serverService.getConfig() | |
59 | .subscribe(config => { | |
60 | this.serverConfig = config | |
61 | this.cdr.detectChanges() | |
62 | }) | |
63 | } | |
64 | ||
65 | isUnavailable (e: VideoPlaylistElement) { | |
66 | return e.type === VideoPlaylistElementType.UNAVAILABLE | |
67 | } | |
68 | ||
69 | isPrivate (e: VideoPlaylistElement) { | |
70 | return e.type === VideoPlaylistElementType.PRIVATE | |
71 | } | |
72 | ||
73 | isDeleted (e: VideoPlaylistElement) { | |
74 | return e.type === VideoPlaylistElementType.DELETED | |
75 | } | |
76 | ||
77 | buildRouterLink () { | |
78 | if (!this.playlist) return null | |
79 | ||
80 | return [ '/videos/watch/playlist', this.playlist.uuid ] | |
81 | } | |
82 | ||
83 | buildRouterQuery () { | |
84 | if (!this.playlistElement || !this.playlistElement.video) return {} | |
85 | ||
86 | return { | |
87 | videoId: this.playlistElement.video.uuid, | |
88 | start: this.playlistElement.startTimestamp, | |
89 | stop: this.playlistElement.stopTimestamp, | |
90 | resume: true | |
91 | } | |
92 | } | |
93 | ||
94 | isVideoBlur (video: Video) { | |
95 | return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig) | |
96 | } | |
97 | ||
98 | removeFromPlaylist (playlistElement: VideoPlaylistElement) { | |
99 | const videoId = this.playlistElement.video ? this.playlistElement.video.id : undefined | |
100 | ||
101 | this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id, videoId) | |
102 | .subscribe( | |
103 | () => { | |
104 | this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName })) | |
105 | ||
106 | this.elementRemoved.emit(playlistElement) | |
107 | }, | |
108 | ||
109 | err => this.notifier.error(err.message) | |
110 | ) | |
111 | ||
112 | this.moreDropdown.close() | |
113 | } | |
114 | ||
115 | updateTimestamps (playlistElement: VideoPlaylistElement) { | |
116 | const body: VideoPlaylistElementUpdate = {} | |
117 | ||
118 | body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null | |
119 | body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null | |
120 | ||
121 | this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body, this.playlistElement.video.id) | |
122 | .subscribe( | |
123 | () => { | |
124 | this.notifier.success(this.i18n('Timestamps updated')) | |
125 | ||
126 | playlistElement.startTimestamp = body.startTimestamp | |
127 | playlistElement.stopTimestamp = body.stopTimestamp | |
128 | ||
129 | this.cdr.detectChanges() | |
130 | }, | |
131 | ||
132 | err => this.notifier.error(err.message) | |
133 | ) | |
134 | ||
135 | this.moreDropdown.close() | |
136 | } | |
137 | ||
138 | formatTimestamp (playlistElement: VideoPlaylistElement) { | |
139 | const start = playlistElement.startTimestamp | |
140 | const stop = playlistElement.stopTimestamp | |
141 | ||
142 | const startFormatted = secondsToTime(start, true, ':') | |
143 | const stopFormatted = secondsToTime(stop, true, ':') | |
144 | ||
145 | if (start === null && stop === null) return '' | |
146 | ||
147 | if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted | |
148 | if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted | |
149 | ||
150 | return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted | |
151 | } | |
152 | ||
153 | onDropdownOpenChange () { | |
154 | this.displayTimestampOptions = false | |
155 | } | |
156 | ||
157 | toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) { | |
158 | event.preventDefault() | |
159 | ||
160 | this.displayTimestampOptions = !this.displayTimestampOptions | |
161 | ||
162 | if (this.displayTimestampOptions === true) { | |
163 | this.timestampOptions = { | |
164 | startTimestampEnabled: false, | |
165 | stopTimestampEnabled: false, | |
166 | startTimestamp: 0, | |
167 | stopTimestamp: playlistElement.video.duration | |
168 | } | |
169 | ||
170 | if (playlistElement.startTimestamp) { | |
171 | this.timestampOptions.startTimestampEnabled = true | |
172 | this.timestampOptions.startTimestamp = playlistElement.startTimestamp | |
173 | } | |
174 | ||
175 | if (playlistElement.stopTimestamp) { | |
176 | this.timestampOptions.stopTimestampEnabled = true | |
177 | this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp | |
178 | } | |
179 | } | |
180 | ||
181 | // FIXME: why do we have to use setTimeout here? | |
182 | setTimeout(() => { | |
183 | this.cdr.detectChanges() | |
184 | }) | |
185 | } | |
186 | } |