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