1 import { Subject, Subscription } from 'rxjs'
2 import { CdkDragDrop } from '@angular/cdk/drag-drop'
3 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { ComponentPagination, ConfirmService, HooksService, Notifier, ScreenService } from '@app/core'
6 import { DropdownAction } from '@app/shared/shared-main'
7 import { VideoShareComponent } from '@app/shared/shared-share-modal'
8 import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
9 import { VideoPlaylistType } from '@shared/models'
12 templateUrl: './my-video-playlist-elements.component.html',
13 styleUrls: [ './my-video-playlist-elements.component.scss' ]
15 export class MyVideoPlaylistElementsComponent implements OnInit, OnDestroy {
16 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
18 playlistElements: VideoPlaylistElement[] = []
19 playlist: VideoPlaylist
21 playlistActions: DropdownAction<VideoPlaylist>[][] = []
23 pagination: ComponentPagination = {
29 onDataSubject = new Subject<any[]>()
31 private videoPlaylistId: string | number
32 private paramsSub: Subscription
35 private hooks: HooksService,
36 private notifier: Notifier,
37 private router: Router,
38 private confirmService: ConfirmService,
39 private route: ActivatedRoute,
40 private screenService: ScreenService,
41 private videoPlaylistService: VideoPlaylistService
45 this.playlistActions = [
48 label: $localize`Update playlist`,
50 linkBuilder: playlist => [ '/my-library', 'video-playlists', 'update', playlist.uuid ]
53 label: $localize`Delete playlist`,
55 handler: playlist => this.deleteVideoPlaylist(playlist)
60 this.paramsSub = this.route.params.subscribe(routeParams => {
61 this.videoPlaylistId = routeParams['videoPlaylistId']
64 this.loadPlaylistInfo()
69 if (this.paramsSub) this.paramsSub.unsubscribe()
72 drop (event: CdkDragDrop<any>) {
73 const previousIndex = event.previousIndex
74 const newIndex = event.currentIndex
76 if (previousIndex === newIndex) return
78 const oldPosition = this.playlistElements[previousIndex].position
79 let insertAfter = this.playlistElements[newIndex].position
81 if (oldPosition > insertAfter) insertAfter--
83 const element = this.playlistElements[previousIndex]
85 this.playlistElements.splice(previousIndex, 1)
86 this.playlistElements.splice(newIndex, 0, element)
88 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
91 this.reorderClientPositions()
94 error: err => this.notifier.error(err.message)
98 onElementRemoved (element: VideoPlaylistElement) {
99 const oldFirst = this.findFirst()
101 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
102 this.reorderClientPositions(oldFirst)
107 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
109 this.pagination.currentPage += 1
113 trackByFn (index: number, elem: VideoPlaylistElement) {
117 isRegularPlaylist (playlist: VideoPlaylist) {
118 return playlist?.type.id === VideoPlaylistType.REGULAR
122 this.videoShareModal.show()
125 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
126 const res = await this.confirmService.confirm(
127 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
130 if (res === false) return
132 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
135 this.router.navigate([ '/my-library', 'video-playlists' ])
136 this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`)
139 error: err => this.notifier.error(err.message)
144 * Returns null to not have drag and drop delay.
145 * In small views, where elements are about 100% wide,
146 * we add a delay to prevent unwanted drag&drop.
148 * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078}
150 getDragStartDelay (): null | number {
151 if (this.screenService.isInTouchScreen()) {
158 private loadElements () {
159 this.hooks.wrapObsFun(
160 this.videoPlaylistService.getPlaylistVideos.bind(this.videoPlaylistService),
161 { videoPlaylistId: this.videoPlaylistId, componentPagination: this.pagination },
163 'filter:api.my-library.video-playlist-elements.list.params',
164 'filter:api.my-library.video-playlist-elements.list.result'
166 .subscribe(({ total, data }) => {
167 this.playlistElements = this.playlistElements.concat(data)
168 this.pagination.totalItems = total
170 this.onDataSubject.next(data)
174 private loadPlaylistInfo () {
175 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
176 .subscribe(playlist => {
177 this.playlist = playlist
181 private reorderClientPositions (first?: VideoPlaylistElement) {
182 if (this.playlistElements.length === 0) return
184 const oldFirst = first || this.findFirst()
187 for (const element of this.playlistElements) {
192 // Reload playlist thumbnail if the first element changed
193 const newFirst = this.findFirst()
194 if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
195 this.loadPlaylistInfo()
199 private findFirst () {
200 return this.playlistElements.find(e => e.position === 1)