1 import { Component, Input, OnInit, ChangeDetectorRef } from '@angular/core'
2 import { Video } from './video.model'
3 import { ScreenService } from '@app/shared/misc/screen.service'
4 import { AuthService, ThemeService } from '@app/core'
5 import { VideoPlaylistService } from '../video-playlist/video-playlist.service'
6 import { VideoPlaylistElementCreate } from '../../../../../shared'
9 selector: 'my-video-thumbnail',
10 styleUrls: [ './video-thumbnail.component.scss' ],
11 templateUrl: './video-thumbnail.component.html'
13 export class VideoThumbnailComponent {
16 @Input() routerLink: any[]
17 @Input() queryParams: any[]
19 addToWatchLaterText = 'Add to watch later'
20 addedToWatchLaterText = 'Added to watch later'
21 addedToWatchLater: boolean
23 watchLaterPlaylist: any
26 private screenService: ScreenService,
27 private authService: AuthService,
28 private videoPlaylistService: VideoPlaylistService,
29 private cd: ChangeDetectorRef
33 if (this.addedToWatchLater !== undefined) return
34 if (!this.isUserLoggedIn()) return
36 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
39 for (const playlist of this.authService.getUser().specialPlaylists) {
40 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
41 this.addedToWatchLater = !!existingPlaylist
43 if (existingPlaylist) {
44 this.watchLaterPlaylist = {
45 playlistId: existingPlaylist.playlistId,
46 playlistElementId: existingPlaylist.playlistElementId
49 this.watchLaterPlaylist = {
50 playlistId: playlist.id
54 this.cd.markForCheck()
61 if (!this.video) return ''
63 if (this.screenService.isInMobileView()) {
64 return this.video.previewUrl
67 return this.video.thumbnailUrl
70 getProgressPercent () {
71 if (!this.video.userHistory) return 0
73 const currentTime = this.video.userHistory.currentTime
75 return (currentTime / this.video.duration) * 100
78 getVideoRouterLink () {
79 if (this.routerLink) return this.routerLink
81 return [ '/videos/watch', this.video.uuid ]
85 return this.authService.isLoggedIn()
89 if (this.addedToWatchLater === undefined) return
90 this.addedToWatchLater = true
92 this.videoPlaylistService.addVideoInPlaylist(
93 this.watchLaterPlaylist.playlistId,
94 { videoId: this.video.id } as VideoPlaylistElementCreate
97 this.addedToWatchLater = true
98 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
103 removeFromWatchLater () {
104 if (this.addedToWatchLater === undefined) return
105 this.addedToWatchLater = false
107 this.videoPlaylistService.removeVideoFromPlaylist(
108 this.watchLaterPlaylist.playlistId,
109 this.watchLaterPlaylist.playlistElementId
112 this.addedToWatchLater = false