1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { Notifier, ServerService } from '@app/core'
3 import { AuthService } from '../../core/auth'
4 import { ConfirmService } from '../../core/confirm'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { Video } from '@app/shared/video/video.model'
7 import { Subscription } from 'rxjs'
8 import { ActivatedRoute } from '@angular/router'
9 import { VideoService } from '@app/shared/video/video.service'
10 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
11 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
13 import { secondsToTime } from '../../../assets/player/utils'
14 import { VideoPlaylistElementUpdate } from '@shared/models'
15 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
18 selector: 'my-account-video-playlist-elements',
19 templateUrl: './my-account-video-playlist-elements.component.html',
20 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
22 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
23 @ViewChild('moreDropdown') moreDropdown: NgbDropdown
26 playlist: VideoPlaylist
28 pagination: ComponentPagination = {
34 displayTimestampOptions = false
37 startTimestampEnabled: boolean
38 startTimestamp: number
39 stopTimestampEnabled: boolean
43 private videoPlaylistId: string | number
44 private paramsSub: Subscription
47 private authService: AuthService,
48 private serverService: ServerService,
49 private notifier: Notifier,
50 private confirmService: ConfirmService,
51 private route: ActivatedRoute,
53 private videoService: VideoService,
54 private videoPlaylistService: VideoPlaylistService
58 this.paramsSub = this.route.params.subscribe(routeParams => {
59 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
62 this.loadPlaylistInfo()
67 if (this.paramsSub) this.paramsSub.unsubscribe()
70 isVideoBlur (video: Video) {
71 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
74 removeFromPlaylist (video: Video) {
75 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, video.id)
78 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
80 this.videos = this.videos.filter(v => v.id !== video.id)
83 err => this.notifier.error(err.message)
86 this.moreDropdown.close()
89 updateTimestamps (video: Video) {
90 const body: VideoPlaylistElementUpdate = {}
92 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
93 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
95 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, video.id, body)
98 this.notifier.success(this.i18n('Timestamps updated'))
100 video.playlistElement.startTimestamp = body.startTimestamp
101 video.playlistElement.stopTimestamp = body.stopTimestamp
104 err => this.notifier.error(err.message)
107 this.moreDropdown.close()
112 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
114 this.pagination.currentPage += 1
118 formatTimestamp (video: Video) {
119 const start = video.playlistElement.startTimestamp
120 const stop = video.playlistElement.stopTimestamp
122 const startFormatted = secondsToTime(start, true, ':')
123 const stopFormatted = secondsToTime(stop, true, ':')
125 if (start === null && stop === null) return ''
127 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
128 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
130 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
133 onDropdownOpenChange () {
134 this.displayTimestampOptions = false
137 toggleDisplayTimestampsOptions (event: Event, video: Video) {
138 event.preventDefault()
140 this.displayTimestampOptions = !this.displayTimestampOptions
142 if (this.displayTimestampOptions === true) {
143 this.timestampOptions = {
144 startTimestampEnabled: false,
145 stopTimestampEnabled: false,
147 stopTimestamp: video.duration
150 if (video.playlistElement.startTimestamp) {
151 this.timestampOptions.startTimestampEnabled = true
152 this.timestampOptions.startTimestamp = video.playlistElement.startTimestamp
155 if (video.playlistElement.stopTimestamp) {
156 this.timestampOptions.stopTimestampEnabled = true
157 this.timestampOptions.stopTimestamp = video.playlistElement.stopTimestamp
162 private loadElements () {
163 this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
164 .subscribe(({ totalVideos, videos }) => {
165 this.videos = this.videos.concat(videos)
166 this.pagination.totalItems = totalVideos
170 private loadPlaylistInfo () {
171 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
172 .subscribe(playlist => {
173 this.playlist = playlist