1 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, Output, ViewChild } from '@angular/core'
2 import { Video } from '@app/shared/video/video.model'
3 import { 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'
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
20 export class VideoPlaylistElementMiniatureComponent {
21 @ViewChild('moreDropdown', { static: false }) moreDropdown: NgbDropdown
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
31 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
33 displayTimestampOptions = false
36 startTimestampEnabled: boolean
37 startTimestamp: number
38 stopTimestampEnabled: boolean
43 private authService: AuthService,
44 private serverService: ServerService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private route: ActivatedRoute,
49 private videoService: VideoService,
50 private videoPlaylistService: VideoPlaylistService,
51 private cdr: ChangeDetectorRef
54 isUnavailable (e: VideoPlaylistElement) {
55 return e.type === VideoPlaylistElementType.UNAVAILABLE
58 isPrivate (e: VideoPlaylistElement) {
59 return e.type === VideoPlaylistElementType.PRIVATE
62 isDeleted (e: VideoPlaylistElement) {
63 return e.type === VideoPlaylistElementType.DELETED
67 if (!this.playlist) return null
69 return [ '/videos/watch/playlist', this.playlist.uuid ]
73 if (!this.playlistElement || !this.playlistElement.video) return {}
76 videoId: this.playlistElement.video.uuid,
77 start: this.playlistElement.startTimestamp,
78 stop: this.playlistElement.stopTimestamp
82 isVideoBlur (video: Video) {
83 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
86 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
87 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id)
90 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
92 this.elementRemoved.emit(playlistElement)
95 err => this.notifier.error(err.message)
98 this.moreDropdown.close()
101 updateTimestamps (playlistElement: VideoPlaylistElement) {
102 const body: VideoPlaylistElementUpdate = {}
104 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
105 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
107 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body)
110 this.notifier.success(this.i18n('Timestamps updated'))
112 playlistElement.startTimestamp = body.startTimestamp
113 playlistElement.stopTimestamp = body.stopTimestamp
115 this.cdr.detectChanges()
118 err => this.notifier.error(err.message)
121 this.moreDropdown.close()
124 formatTimestamp (playlistElement: VideoPlaylistElement) {
125 const start = playlistElement.startTimestamp
126 const stop = playlistElement.stopTimestamp
128 const startFormatted = secondsToTime(start, true, ':')
129 const stopFormatted = secondsToTime(stop, true, ':')
131 if (start === null && stop === null) return ''
133 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
134 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
136 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
139 onDropdownOpenChange () {
140 this.displayTimestampOptions = false
143 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
144 event.preventDefault()
146 this.displayTimestampOptions = !this.displayTimestampOptions
148 if (this.displayTimestampOptions === true) {
149 this.timestampOptions = {
150 startTimestampEnabled: false,
151 stopTimestampEnabled: false,
153 stopTimestamp: playlistElement.video.duration
156 if (playlistElement.startTimestamp) {
157 this.timestampOptions.startTimestampEnabled = true
158 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
161 if (playlistElement.stopTimestamp) {
162 this.timestampOptions.stopTimestampEnabled = true
163 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
167 // FIXME: why do we have to use setTimeout here?
169 this.cdr.detectChanges()