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'
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 implements OnInit {
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
42 private serverConfig: ServerConfig
45 private authService: AuthService,
46 private serverService: ServerService,
47 private notifier: Notifier,
48 private confirmService: ConfirmService,
49 private route: ActivatedRoute,
51 private videoService: VideoService,
52 private videoPlaylistService: VideoPlaylistService,
53 private cdr: ChangeDetectorRef
57 this.serverConfig = this.serverService.getTmpConfig()
58 this.serverService.getConfig()
59 .subscribe(config => {
60 this.serverConfig = config
61 this.cdr.detectChanges()
65 isUnavailable (e: VideoPlaylistElement) {
66 return e.type === VideoPlaylistElementType.UNAVAILABLE
69 isPrivate (e: VideoPlaylistElement) {
70 return e.type === VideoPlaylistElementType.PRIVATE
73 isDeleted (e: VideoPlaylistElement) {
74 return e.type === VideoPlaylistElementType.DELETED
78 if (!this.playlist) return null
80 return [ '/videos/watch/playlist', this.playlist.uuid ]
84 if (!this.playlistElement || !this.playlistElement.video) return {}
87 videoId: this.playlistElement.video.uuid,
88 start: this.playlistElement.startTimestamp,
89 stop: this.playlistElement.stopTimestamp
93 isVideoBlur (video: Video) {
94 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
97 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
98 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id)
101 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
103 this.elementRemoved.emit(playlistElement)
106 err => this.notifier.error(err.message)
109 this.moreDropdown.close()
112 updateTimestamps (playlistElement: VideoPlaylistElement) {
113 const body: VideoPlaylistElementUpdate = {}
115 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
116 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
118 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body)
121 this.notifier.success(this.i18n('Timestamps updated'))
123 playlistElement.startTimestamp = body.startTimestamp
124 playlistElement.stopTimestamp = body.stopTimestamp
126 this.cdr.detectChanges()
129 err => this.notifier.error(err.message)
132 this.moreDropdown.close()
135 formatTimestamp (playlistElement: VideoPlaylistElement) {
136 const start = playlistElement.startTimestamp
137 const stop = playlistElement.stopTimestamp
139 const startFormatted = secondsToTime(start, true, ':')
140 const stopFormatted = secondsToTime(stop, true, ':')
142 if (start === null && stop === null) return ''
144 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
145 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
147 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
150 onDropdownOpenChange () {
151 this.displayTimestampOptions = false
154 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
155 event.preventDefault()
157 this.displayTimestampOptions = !this.displayTimestampOptions
159 if (this.displayTimestampOptions === true) {
160 this.timestampOptions = {
161 startTimestampEnabled: false,
162 stopTimestampEnabled: false,
164 stopTimestamp: playlistElement.video.duration
167 if (playlistElement.startTimestamp) {
168 this.timestampOptions.startTimestampEnabled = true
169 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
172 if (playlistElement.stopTimestamp) {
173 this.timestampOptions.stopTimestampEnabled = true
174 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
178 // FIXME: why do we have to use setTimeout here?
180 this.cdr.detectChanges()