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 { Subject, 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'
16 import { CdkDragDrop, CdkDragMove } from '@angular/cdk/drag-drop'
17 import { throttleTime } from 'rxjs/operators'
20 selector: 'my-account-video-playlist-elements',
21 templateUrl: './my-account-video-playlist-elements.component.html',
22 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
24 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
25 @ViewChild('moreDropdown') moreDropdown: NgbDropdown
28 playlist: VideoPlaylist
30 pagination: ComponentPagination = {
36 displayTimestampOptions = false
39 startTimestampEnabled: boolean
40 startTimestamp: number
41 stopTimestampEnabled: boolean
45 private videoPlaylistId: string | number
46 private paramsSub: Subscription
47 private dragMoveSubject = new Subject<number>()
50 private authService: AuthService,
51 private serverService: ServerService,
52 private notifier: Notifier,
53 private confirmService: ConfirmService,
54 private route: ActivatedRoute,
56 private videoService: VideoService,
57 private videoPlaylistService: VideoPlaylistService
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
65 this.loadPlaylistInfo()
68 this.dragMoveSubject.asObservable()
69 .pipe(throttleTime(200))
70 .subscribe(y => this.checkScroll(y))
74 if (this.paramsSub) this.paramsSub.unsubscribe()
77 drop (event: CdkDragDrop<any>) {
78 const previousIndex = event.previousIndex
79 const newIndex = event.currentIndex
81 if (previousIndex === newIndex) return
83 const oldPosition = this.videos[previousIndex].playlistElement.position
84 const insertAfter = newIndex === 0 ? 0 : this.videos[newIndex].playlistElement.position
86 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
88 () => { /* nothing to do */ },
90 err => this.notifier.error(err.message)
93 const video = this.videos[previousIndex]
95 this.videos.splice(previousIndex, 1)
96 this.videos.splice(newIndex, 0, video)
98 this.reorderClientPositions()
101 onDragMove (event: CdkDragMove<any>) {
102 this.dragMoveSubject.next(event.pointerPosition.y)
105 checkScroll (pointerY: number) {
106 // FIXME: Uncomment when https://github.com/angular/material2/issues/14098 is fixed
107 // FIXME: Remove when https://github.com/angular/material2/issues/13588 is implemented
108 // if (pointerY < 150) {
112 // behavior: 'smooth'
118 // if (window.innerHeight - pointerY <= 50) {
122 // behavior: 'smooth'
127 isVideoBlur (video: Video) {
128 return video.isVideoNSFWForUser(this.authService.getUser(), this.serverService.getConfig())
131 removeFromPlaylist (video: Video) {
132 this.videoPlaylistService.removeVideoFromPlaylist(this.playlist.id, video.id)
135 this.notifier.success(this.i18n('Video removed from {{name}}', { name: this.playlist.displayName }))
137 this.videos = this.videos.filter(v => v.id !== video.id)
138 this.reorderClientPositions()
141 err => this.notifier.error(err.message)
144 this.moreDropdown.close()
147 updateTimestamps (video: Video) {
148 const body: VideoPlaylistElementUpdate = {}
150 body.startTimestamp = this.timestampOptions.startTimestampEnabled ? this.timestampOptions.startTimestamp : null
151 body.stopTimestamp = this.timestampOptions.stopTimestampEnabled ? this.timestampOptions.stopTimestamp : null
153 this.videoPlaylistService.updateVideoOfPlaylist(this.playlist.id, video.id, body)
156 this.notifier.success(this.i18n('Timestamps updated'))
158 video.playlistElement.startTimestamp = body.startTimestamp
159 video.playlistElement.stopTimestamp = body.stopTimestamp
162 err => this.notifier.error(err.message)
165 this.moreDropdown.close()
170 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
172 this.pagination.currentPage += 1
176 formatTimestamp (video: Video) {
177 const start = video.playlistElement.startTimestamp
178 const stop = video.playlistElement.stopTimestamp
180 const startFormatted = secondsToTime(start, true, ':')
181 const stopFormatted = secondsToTime(stop, true, ':')
183 if (start === null && stop === null) return ''
185 if (start !== null && stop === null) return this.i18n('Starts at ') + startFormatted
186 if (start === null && stop !== null) return this.i18n('Stops at ') + stopFormatted
188 return this.i18n('Starts at ') + startFormatted + this.i18n(' and stops at ') + stopFormatted
191 onDropdownOpenChange () {
192 this.displayTimestampOptions = false
195 toggleDisplayTimestampsOptions (event: Event, video: Video) {
196 event.preventDefault()
198 this.displayTimestampOptions = !this.displayTimestampOptions
200 if (this.displayTimestampOptions === true) {
201 this.timestampOptions = {
202 startTimestampEnabled: false,
203 stopTimestampEnabled: false,
205 stopTimestamp: video.duration
208 if (video.playlistElement.startTimestamp) {
209 this.timestampOptions.startTimestampEnabled = true
210 this.timestampOptions.startTimestamp = video.playlistElement.startTimestamp
213 if (video.playlistElement.stopTimestamp) {
214 this.timestampOptions.stopTimestampEnabled = true
215 this.timestampOptions.stopTimestamp = video.playlistElement.stopTimestamp
220 private loadElements () {
221 this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
222 .subscribe(({ totalVideos, videos }) => {
223 this.videos = this.videos.concat(videos)
224 this.pagination.totalItems = totalVideos
228 private loadPlaylistInfo () {
229 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
230 .subscribe(playlist => {
231 this.playlist = playlist
235 private reorderClientPositions () {
238 for (const video of this.videos) {
239 video.playlistElement.position = i