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') 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
30 @Input() touchScreenEditButton = false
32 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
34 displayTimestampOptions = false
37 startTimestampEnabled: boolean
38 startTimestamp: number
39 stopTimestampEnabled: boolean
43 private serverConfig: ServerConfig
46 private authService: AuthService,
47 private serverService: ServerService,
48 private notifier: Notifier,
49 private confirmService: ConfirmService,
50 private route: ActivatedRoute,
52 private videoService: VideoService,
53 private videoPlaylistService: VideoPlaylistService,
54 private cdr: ChangeDetectorRef
58 this.serverConfig = this.serverService.getTmpConfig()
59 this.serverService.getConfig()
60 .subscribe(config => {
61 this.serverConfig = config
62 this.cdr.detectChanges()
66 isUnavailable (e: VideoPlaylistElement) {
67 return e.type === VideoPlaylistElementType.UNAVAILABLE
70 isPrivate (e: VideoPlaylistElement) {
71 return e.type === VideoPlaylistElementType.PRIVATE
74 isDeleted (e: VideoPlaylistElement) {
75 return e.type === VideoPlaylistElementType.DELETED
79 if (!this.playlist) return null
81 return [ '/videos/watch/playlist', this.playlist.uuid ]
85 if (!this.playlistElement || !this.playlistElement.video) return {}
88 videoId: this.playlistElement.video.uuid,
89 start: this.playlistElement.startTimestamp,
90 stop: this.playlistElement.stopTimestamp,
95 isVideoBlur (video: Video) {
96 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
99 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
100 const videoId = this.playlistElement.video ? this.playlistElement.video.id : undefined
102 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id, videoId)
105 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
107 this.elementRemoved.emit(playlistElement)
110 err => this.notifier.error(err.message)
113 this.moreDropdown.close()
116 updateTimestamps (playlistElement: VideoPlaylistElement) {
117 const body: VideoPlaylistElementUpdate = {}
119 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
120 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
122 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body, this.playlistElement.video.id)
125 this.notifier.success(this.i18n('Timestamps updated'))
127 playlistElement.startTimestamp = body.startTimestamp
128 playlistElement.stopTimestamp = body.stopTimestamp
130 this.cdr.detectChanges()
133 err => this.notifier.error(err.message)
136 this.moreDropdown.close()
139 formatTimestamp (playlistElement: VideoPlaylistElement) {
140 const start = playlistElement.startTimestamp
141 const stop = playlistElement.stopTimestamp
143 const startFormatted = secondsToTime(start, true, ':')
144 const stopFormatted = secondsToTime(stop, true, ':')
146 if (start === null && stop === null) return ''
148 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
149 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
151 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
154 onDropdownOpenChange () {
155 this.displayTimestampOptions = false
158 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
159 event.preventDefault()
161 this.displayTimestampOptions = !this.displayTimestampOptions
163 if (this.displayTimestampOptions === true) {
164 this.timestampOptions = {
165 startTimestampEnabled: false,
166 stopTimestampEnabled: false,
168 stopTimestamp: playlistElement.video.duration
171 if (playlistElement.startTimestamp) {
172 this.timestampOptions.startTimestampEnabled = true
173 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
176 if (playlistElement.stopTimestamp) {
177 this.timestampOptions.stopTimestampEnabled = true
178 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
182 // FIXME: why do we have to use setTimeout here?
184 this.cdr.detectChanges()