]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video-playlist/video-playlist-element-miniature.component.ts
Fix broken playlist api
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-playlist / video-playlist-element-miniature.component.ts
1 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, Output, ViewChild } from '@angular/core'
2 import { Video } from '@app/shared/video/video.model'
3 import { 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'
13
14 @Component({
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
19 })
20 export class VideoPlaylistElementMiniatureComponent {
21 @ViewChild('moreDropdown', { static: false }) moreDropdown: NgbDropdown
22
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
31 @Output() elementRemoved = new EventEmitter<VideoPlaylistElement>()
32
33 displayTimestampOptions = false
34
35 timestampOptions: {
36 startTimestampEnabled: boolean
37 startTimestamp: number
38 stopTimestampEnabled: boolean
39 stopTimestamp: number
40 } = {} as any
41
42 constructor (
43 private authService: AuthService,
44 private serverService: ServerService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private route: ActivatedRoute,
48 private i18n: I18n,
49 private videoService: VideoService,
50 private videoPlaylistService: VideoPlaylistService,
51 private cdr: ChangeDetectorRef
52 ) {}
53
54 isUnavailable (e: VideoPlaylistElement) {
55 return e.type === VideoPlaylistElementType.UNAVAILABLE
56 }
57
58 isPrivate (e: VideoPlaylistElement) {
59 return e.type === VideoPlaylistElementType.PRIVATE
60 }
61
62 isDeleted (e: VideoPlaylistElement) {
63 return e.type === VideoPlaylistElementType.DELETED
64 }
65
66 buildRouterLink () {
67 if (!this.playlist) return null
68
69 return [ '/videos/watch/playlist', this.playlist.uuid ]
70 }
71
72 buildRouterQuery () {
73 if (!this.playlistElement || !this.playlistElement.video) return {}
74
75 return {
76 videoId: this.playlistElement.video.uuid,
77 start: this.playlistElement.startTimestamp,
78 stop: this.playlistElement.stopTimestamp
79 }
80 }
81
82 isVideoBlur (video: Video) {
83 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
84 }
85
86 removeFromPlaylist (playlistElement: VideoPlaylistElement) {
87 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, playlistElement.id)
88 .subscribe(
89 () => {
90 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
91
92 this.elementRemoved.emit(playlistElement)
93 },
94
95 err => this.notifier.error(err.message)
96 )
97
98 this.moreDropdown.close()
99 }
100
101 updateTimestamps (playlistElement: VideoPlaylistElement) {
102 const body: VideoPlaylistElementUpdate = {}
103
104 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
105 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
106
107 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, playlistElement.id, body)
108 .subscribe(
109 () => {
110 this.notifier.success(this.i18n('Timestamps updated'))
111
112 playlistElement.startTimestamp = body.startTimestamp
113 playlistElement.stopTimestamp = body.stopTimestamp
114
115 this.cdr.detectChanges()
116 },
117
118 err => this.notifier.error(err.message)
119 )
120
121 this.moreDropdown.close()
122 }
123
124 formatTimestamp (playlistElement: VideoPlaylistElement) {
125 const start = playlistElement.startTimestamp
126 const stop = playlistElement.stopTimestamp
127
128 const startFormatted = secondsToTime(start, true, ':')
129 const stopFormatted = secondsToTime(stop, true, ':')
130
131 if (start === null && stop === null) return ''
132
133 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
134 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
135
136 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
137 }
138
139 onDropdownOpenChange () {
140 this.displayTimestampOptions = false
141 }
142
143 toggleDisplayTimestampsOptions (event: Event, playlistElement: VideoPlaylistElement) {
144 event.preventDefault()
145
146 this.displayTimestampOptions = !this.displayTimestampOptions
147
148 if (this.displayTimestampOptions === true) {
149 this.timestampOptions = {
150 startTimestampEnabled: false,
151 stopTimestampEnabled: false,
152 startTimestamp: 0,
153 stopTimestamp: playlistElement.video.duration
154 }
155
156 if (playlistElement.startTimestamp) {
157 this.timestampOptions.startTimestampEnabled = true
158 this.timestampOptions.startTimestamp = playlistElement.startTimestamp
159 }
160
161 if (playlistElement.stopTimestamp) {
162 this.timestampOptions.stopTimestampEnabled = true
163 this.timestampOptions.stopTimestamp = playlistElement.stopTimestamp
164 }
165 }
166
167 // FIXME: why do we have to use setTimeout here?
168 setTimeout(() => {
169 this.cdr.detectChanges()
170 })
171 }
172 }