1 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { AuthService, Notifier, ServerService } from '@app/core'
3 import { Video } from '@app/shared/shared-main'
4 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
5 import { ServerConfig, VideoPlaylistElementType, VideoPlaylistElementUpdate } from '@shared/models'
6 import { secondsToTime } from '../../../assets/player/utils'
7 import { VideoPlaylistElement } from './video-playlist-element.model'
8 import { VideoPlaylist } from './video-playlist.model'
9 import { VideoPlaylistService } from './video-playlist.service'
12 selector: 'my-video-playlist-element-miniature',
13 styleUrls: [ './video-playlist-element-miniature.component.scss' ],
14 templateUrl: './video-playlist-element-miniature.component.html',
15 changeDetection: ChangeDetectionStrategy.OnPush
17 export class VideoPlaylistElementMiniatureComponent implements OnInit {
18 @ViewChild('moreDropdown') moreDropdown: NgbDropdown
20 @Input() playlist: VideoPlaylist
21 @Input() playlistElement: VideoPlaylistElement
22 @Input() owned = false
23 @Input() playing = false
24 @Input() rowLink = false
25 @Input() accountLink = true
26 @Input() position: number // Keep this property because we're in the OnPush change detection strategy
27 @Input() touchScreenEditButton = false
29 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
31 displayTimestampOptions = false
34 startTimestampEnabled: boolean
35 startTimestamp: number
36 stopTimestampEnabled: boolean
40 private serverConfig: ServerConfig
43 private authService: AuthService,
44 private serverService: ServerService,
45 private notifier: Notifier,
46 private videoPlaylistService: VideoPlaylistService,
47 private cdr: ChangeDetectorRef
51 this.serverConfig = this.serverService.getTmpConfig()
52 this.serverService.getConfig()
53 .subscribe(config => {
54 this.serverConfig = config
55 this.cdr.detectChanges()
59 isUnavailable (e: VideoPlaylistElement) {
60 return e.type === VideoPlaylistElementType.UNAVAILABLE
63 isPrivate (e: VideoPlaylistElement) {
64 return e.type === VideoPlaylistElementType.PRIVATE
67 isDeleted (e: VideoPlaylistElement) {
68 return e.type === VideoPlaylistElementType.DELETED
72 if (!this.playlist) return null
74 return [ '/videos/watch/playlist', this.playlist.uuid ]
78 if (!this.playlistElement || !this.playlistElement.video) return {}
81 playlistPosition: this.playlistElement.position,
82 start: this.playlistElement.startTimestamp,
83 stop: this.playlistElement.stopTimestamp,
88 isVideoBlur (video: Video) {
89 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
92 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
93 const videoId = this.playlistElement.video ? this.playlistElement.video.id : undefined
95 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id, videoId)
98 this.notifier.success($localize`Video removed from ${this.playlist.displayName}`)
99 this.elementRemoved.emit(playlistElement)
102 err => this.notifier.error(err.message)
105 this.moreDropdown.close()
108 updateTimestamps (playlistElement: VideoPlaylistElement) {
109 const body: VideoPlaylistElementUpdate = {}
111 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
112 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
114 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body, this.playlistElement.video.id)
117 this.notifier.success($localize`Timestamps updated`)
119 playlistElement.startTimestamp = body.startTimestamp
120 playlistElement.stopTimestamp = body.stopTimestamp
122 this.cdr.detectChanges()
125 err => this.notifier.error(err.message)
128 this.moreDropdown.close()
131 formatTimestamp (playlistElement: VideoPlaylistElement) {
132 const start = playlistElement.startTimestamp
133 const stop = playlistElement.stopTimestamp
135 const startFormatted = secondsToTime(start, true, ':')
136 const stopFormatted = secondsToTime(stop, true, ':')
138 if (start === null && stop === null) return ''
140 if (start !== null && stop === null) return $localize`Starts at ` + startFormatted
141 if (start === null && stop !== null) return $localize`Stops at ` + stopFormatted
143 return $localize`Starts at ` + startFormatted + $localize` and stops at ` + stopFormatted
146 onDropdownOpenChange () {
147 this.displayTimestampOptions = false
150 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
151 event.preventDefault()
153 this.displayTimestampOptions = !this.displayTimestampOptions
155 if (this.displayTimestampOptions === true) {
156 this.timestampOptions = {
157 startTimestampEnabled: false,
158 stopTimestampEnabled: false,
160 stopTimestamp: playlistElement.video.duration
163 if (playlistElement.startTimestamp) {
164 this.timestampOptions.startTimestampEnabled = true
165 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
168 if (playlistElement.stopTimestamp) {
169 this.timestampOptions.stopTimestampEnabled = true
170 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
174 // FIXME: why do we have to use setTimeout here?
176 this.cdr.detectChanges()