]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-playlist/video-playlist-element-miniature.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-playlist / video-playlist-element-miniature.component.ts
CommitLineData
ba430d75 1import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
67ed6552
C
2import { AuthService, Notifier, ServerService } from '@app/core'
3import { Video } from '@app/shared/shared-main'
e2f01c47 4import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
67ed6552 5import { ServerConfig, VideoPlaylistElementType, VideoPlaylistElementUpdate } from '@shared/models'
e2f01c47 6import { secondsToTime } from '../../../assets/player/utils'
67ed6552
C
7import { VideoPlaylistElement } from './video-playlist-element.model'
8import { VideoPlaylist } from './video-playlist.model'
9import { VideoPlaylistService } from './video-playlist.service'
e2f01c47
C
10
11@Component({
12 selector: 'my-video-playlist-element-miniature',
13 styleUrls: [ './video-playlist-element-miniature.component.scss' ],
bce47964
C
14 templateUrl: './video-playlist-element-miniature.component.html',
15 changeDetection: ChangeDetectionStrategy.OnPush
e2f01c47 16})
ba430d75 17export class VideoPlaylistElementMiniatureComponent implements OnInit {
2f5d2ec5 18 @ViewChild('moreDropdown') moreDropdown: NgbDropdown
e2f01c47
C
19
20 @Input() playlist: VideoPlaylist
bfbd9128 21 @Input() playlistElement: VideoPlaylistElement
e2f01c47
C
22 @Input() owned = false
23 @Input() playing = false
24 @Input() rowLink = false
25 @Input() accountLink = true
bfbd9128 26 @Input() position: number // Keep this property because we're in the OnPush change detection strategy
bedf0e60 27 @Input() touchScreenEditButton = false
e2f01c47 28
bfbd9128 29 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
e2f01c47
C
30
31 displayTimestampOptions = false
32
33 timestampOptions: {
34 startTimestampEnabled: boolean
35 startTimestamp: number
36 stopTimestampEnabled: boolean
37 stopTimestamp: number
38 } = {} as any
39
ba430d75
C
40 private serverConfig: ServerConfig
41
e2f01c47
C
42 constructor (
43 private authService: AuthService,
44 private serverService: ServerService,
45 private notifier: Notifier,
bce47964
C
46 private videoPlaylistService: VideoPlaylistService,
47 private cdr: ChangeDetectorRef
e2f01c47
C
48 ) {}
49
ba430d75
C
50 ngOnInit (): void {
51 this.serverConfig = this.serverService.getTmpConfig()
52 this.serverService.getConfig()
53 .subscribe(config => {
54 this.serverConfig = config
55 this.cdr.detectChanges()
56 })
57 }
58
bfbd9128
C
59 isUnavailable (e: VideoPlaylistElement) {
60 return e.type === VideoPlaylistElementType.UNAVAILABLE
61 }
62
63 isPrivate (e: VideoPlaylistElement) {
64 return e.type === VideoPlaylistElementType.PRIVATE
65 }
66
67 isDeleted (e: VideoPlaylistElement) {
68 return e.type === VideoPlaylistElementType.DELETED
69 }
70
e2f01c47
C
71 buildRouterLink () {
72 if (!this.playlist) return null
73
74 return [ '/videos/watch/playlist', this.playlist.uuid ]
75 }
76
77 buildRouterQuery () {
bfbd9128 78 if (!this.playlistElement || !this.playlistElement.video) return {}
e2f01c47
C
79
80 return {
bfbd9128
C
81 videoId: this.playlistElement.video.uuid,
82 start: this.playlistElement.startTimestamp,
96f6278f
RK
83 stop: this.playlistElement.stopTimestamp,
84 resume: true
e2f01c47
C
85 }
86 }
87
88 isVideoBlur (video: Video) {
ba430d75 89 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverConfig)
e2f01c47
C
90 }
91
bfbd9128 92 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
51b34a11
C
93 const videoId = this.playlistElement.video ? this.playlistElement.video.id : undefined
94
95 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id, videoId)
e2f01c47
C
96 .subscribe(
97 () => {
66357162 98 this.notifier.success($localize`Video removed from ${this.playlist.displayName}`)
bfbd9128 99 this.elementRemoved.emit(playlistElement)
e2f01c47
C
100 },
101
102 err => this.notifier.error(err.message)
103 )
104
105 this.moreDropdown.close()
106 }
107
bfbd9128 108 updateTimestamps (playlistElement: VideoPlaylistElement) {
e2f01c47
C
109 const body: VideoPlaylistElementUpdate = {}
110
111 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
112 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
113
51b34a11 114 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body, this.playlistElement.video.id)
e2f01c47
C
115 .subscribe(
116 () => {
66357162 117 this.notifier.success($localize`Timestamps updated`)
e2f01c47 118
bfbd9128
C
119 playlistElement.startTimestamp = body.startTimestamp
120 playlistElement.stopTimestamp = body.stopTimestamp
bce47964
C
121
122 this.cdr.detectChanges()
e2f01c47
C
123 },
124
125 err => this.notifier.error(err.message)
126 )
127
128 this.moreDropdown.close()
129 }
130
bfbd9128
C
131 formatTimestamp (playlistElement: VideoPlaylistElement) {
132 const start = playlistElement.startTimestamp
133 const stop = playlistElement.stopTimestamp
e2f01c47
C
134
135 const startFormatted = secondsToTime(start, true, ':')
136 const stopFormatted = secondsToTime(stop, true, ':')
137
138 if (start === null && stop === null) return ''
139
66357162
C
140 if (start !== null && stop === null) return $localize`Starts at ` + startFormatted
141 if (start === null && stop !== null) return $localize`Stops at ` + stopFormatted
e2f01c47 142
66357162 143 return $localize`Starts at ` + startFormatted + $localize` and stops at ` + stopFormatted
e2f01c47
C
144 }
145
146 onDropdownOpenChange () {
147 this.displayTimestampOptions = false
148 }
149
bfbd9128 150 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
e2f01c47
C
151 event.preventDefault()
152
153 this.displayTimestampOptions = !this.displayTimestampOptions
154
155 if (this.displayTimestampOptions === true) {
156 this.timestampOptions = {
157 startTimestampEnabled: false,
158 stopTimestampEnabled: false,
159 startTimestamp: 0,
bfbd9128 160 stopTimestamp: playlistElement.video.duration
e2f01c47
C
161 }
162
bfbd9128 163 if (playlistElement.startTimestamp) {
e2f01c47 164 this.timestampOptions.startTimestampEnabled = true
bfbd9128 165 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
e2f01c47
C
166 }
167
bfbd9128 168 if (playlistElement.stopTimestamp) {
e2f01c47 169 this.timestampOptions.stopTimestampEnabled = true
bfbd9128 170 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
e2f01c47
C
171 }
172 }
bce47964
C
173
174 // FIXME: why do we have to use setTimeout here?
175 setTimeout(() => {
176 this.cdr.detectChanges()
177 })
e2f01c47
C
178 }
179}