1
0
Fork 0
peertube/client/src/app/shared/video-playlist/video-playlist-element-mini...

187 lines
6.2 KiB
TypeScript
Raw Normal View History

2019-12-18 14:31:54 +00:00
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2019-03-13 13:18:58 +00:00
import { Video } from '@app/shared/video/video.model'
2019-12-18 14:31:54 +00:00
import { ServerConfig, VideoPlaylistElementType, VideoPlaylistElementUpdate } from '@shared/models'
2019-03-13 13:18:58 +00:00
import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core'
import { ActivatedRoute } from '@angular/router'
import { I18n } from '@ngx-translate/i18n-polyfill'
import { VideoService } from '@app/shared/video/video.service'
import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
import { secondsToTime } from '../../../assets/player/utils'
2019-07-31 13:57:32 +00:00
import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
2019-03-13 13:18:58 +00:00
@Component({
selector: 'my-video-playlist-element-miniature',
styleUrls: [ './video-playlist-element-miniature.component.scss' ],
2019-03-14 13:05:36 +00:00
templateUrl: './video-playlist-element-miniature.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
2019-03-13 13:18:58 +00:00
})
2019-12-18 14:31:54 +00:00
export class VideoPlaylistElementMiniatureComponent implements OnInit {
2020-02-07 09:00:34 +00:00
@ViewChild('moreDropdown') moreDropdown: NgbDropdown
2019-03-13 13:18:58 +00:00
@Input() playlist: VideoPlaylist
2019-07-31 13:57:32 +00:00
@Input() playlistElement: VideoPlaylistElement
2019-03-13 13:18:58 +00:00
@Input() owned = false
@Input() playing = false
@Input() rowLink = false
@Input() accountLink = true
2019-07-31 13:57:32 +00:00
@Input() position: number // Keep this property because we're in the OnPush change detection strategy
2019-03-13 13:18:58 +00:00
2019-07-31 13:57:32 +00:00
@Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
2019-03-13 13:18:58 +00:00
displayTimestampOptions = false
timestampOptions: {
startTimestampEnabled: boolean
startTimestamp: number
stopTimestampEnabled: boolean
stopTimestamp: number
} = {} as any
2019-12-18 14:31:54 +00:00
private serverConfig: ServerConfig
2019-03-13 13:18:58 +00:00
constructor (
private authService: AuthService,
private serverService: ServerService,
private notifier: Notifier,
private confirmService: ConfirmService,
private route: ActivatedRoute,
private i18n: I18n,
private videoService: VideoService,
2019-03-14 13:05:36 +00:00
private videoPlaylistService: VideoPlaylistService,
private cdr: ChangeDetectorRef
2019-03-13 13:18:58 +00:00
) {}
2019-12-18 14:31:54 +00:00
ngOnInit (): void {
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
.subscribe(config => {
this.serverConfig = config
this.cdr.detectChanges()
})
}
2019-07-31 13:57:32 +00:00
isUnavailable (e: VideoPlaylistElement) {
return e.type === VideoPlaylistElementType.UNAVAILABLE
}
isPrivate (e: VideoPlaylistElement) {
return e.type === VideoPlaylistElementType.PRIVATE
}
isDeleted (e: VideoPlaylistElement) {
return e.type === VideoPlaylistElementType.DELETED
}
2019-03-13 13:18:58 +00:00
buildRouterLink () {
if (!this.playlist) return null
return [ '/videos/watch/playlist', this.playlist.uuid ]
}
buildRouterQuery () {
2019-07-31 13:57:32 +00:00
if (!this.playlistElement || !this.playlistElement.video) return {}
2019-03-13 13:18:58 +00:00
return {
2019-07-31 13:57:32 +00:00
videoId: this.playlistElement.video.uuid,
start: this.playlistElement.startTimestamp,
stop: this.playlistElement.stopTimestamp,
resume: true
2019-03-13 13:18:58 +00:00
}
}
isVideoBlur (video: Video) {
2019-12-18 14:31:54 +00:00
return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
2019-03-13 13:18:58 +00:00
}
2019-07-31 13:57:32 +00:00
removeFromPlaylist (playlistElement: VideoPlaylistElement) {
2020-01-06 12:06:13 +00:00
const videoId = this.playlistElement.video ? this.playlistElement.video.id : undefined
this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id, videoId)
2019-03-13 13:18:58 +00:00
.subscribe(
() => {
this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
2019-07-31 13:57:32 +00:00
this.elementRemoved.emit(playlistElement)
2019-03-13 13:18:58 +00:00
},
err => this.notifier.error(err.message)
)
this.moreDropdown.close()
}
2019-07-31 13:57:32 +00:00
updateTimestamps (playlistElement: VideoPlaylistElement) {
2019-03-13 13:18:58 +00:00
const body: VideoPlaylistElementUpdate = {}
body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
2020-01-06 12:06:13 +00:00
this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body, this.playlistElement.video.id)
2019-03-13 13:18:58 +00:00
.subscribe(
() => {
this.notifier.success(this.i18n('Timestamps updated'))
2019-07-31 13:57:32 +00:00
playlistElement.startTimestamp = body.startTimestamp
playlistElement.stopTimestamp = body.stopTimestamp
2019-03-14 13:05:36 +00:00
this.cdr.detectChanges()
2019-03-13 13:18:58 +00:00
},
err => this.notifier.error(err.message)
)
this.moreDropdown.close()
}
2019-07-31 13:57:32 +00:00
formatTimestamp (playlistElement: VideoPlaylistElement) {
const start = playlistElement.startTimestamp
const stop = playlistElement.stopTimestamp
2019-03-13 13:18:58 +00:00
const startFormatted = secondsToTime(start, true, ':')
const stopFormatted = secondsToTime(stop, true, ':')
if (start === null && stop === null) return ''
if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
}
onDropdownOpenChange () {
this.displayTimestampOptions = false
}
2019-07-31 13:57:32 +00:00
toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
2019-03-13 13:18:58 +00:00
event.preventDefault()
this.displayTimestampOptions = !this.displayTimestampOptions
if (this.displayTimestampOptions === true) {
this.timestampOptions = {
startTimestampEnabled: false,
stopTimestampEnabled: false,
startTimestamp: 0,
2019-07-31 13:57:32 +00:00
stopTimestamp: playlistElement.video.duration
2019-03-13 13:18:58 +00:00
}
2019-07-31 13:57:32 +00:00
if (playlistElement.startTimestamp) {
2019-03-13 13:18:58 +00:00
this.timestampOptions.startTimestampEnabled = true
2019-07-31 13:57:32 +00:00
this.timestampOptions.startTimestamp = playlistElement.startTimestamp
2019-03-13 13:18:58 +00:00
}
2019-07-31 13:57:32 +00:00
if (playlistElement.stopTimestamp) {
2019-03-13 13:18:58 +00:00
this.timestampOptions.stopTimestampEnabled = true
2019-07-31 13:57:32 +00:00
this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
2019-03-13 13:18:58 +00:00
}
}
2019-03-14 13:05:36 +00:00
// FIXME: why do we have to use setTimeout here?
setTimeout(() => {
this.cdr.detectChanges()
})
2019-03-13 13:18:58 +00:00
}
}