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