1 import { Component, OnDestroy, OnInit } 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 { Subject, Subscription } from 'rxjs'
7 import { ActivatedRoute } from '@angular/router'
8 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
9 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { ScreenService } from '@app/shared/misc/screen.service'
12 import { CdkDragDrop } from '@angular/cdk/drag-drop'
13 import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
16 selector: 'my-account-video-playlist-elements',
17 templateUrl: './my-account-video-playlist-elements.component.html',
18 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
20 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
21 playlistElements: VideoPlaylistElement[] = []
22 playlist: VideoPlaylist
24 pagination: ComponentPagination = {
30 onDataSubject = new Subject<any[]>()
32 private videoPlaylistId: string | number
33 private paramsSub: Subscription
36 private authService: AuthService,
37 private serverService: ServerService,
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private route: ActivatedRoute,
42 private screenService: ScreenService,
43 private videoPlaylistService: VideoPlaylistService
47 this.paramsSub = this.route.params.subscribe(routeParams => {
48 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
51 this.loadPlaylistInfo()
56 if (this.paramsSub) this.paramsSub.unsubscribe()
59 drop (event: CdkDragDrop<any>) {
60 const previousIndex = event.previousIndex
61 const newIndex = event.currentIndex
63 if (previousIndex === newIndex) return
65 const oldPosition = this.playlistElements[previousIndex].position
66 let insertAfter = this.playlistElements[newIndex].position
68 if (oldPosition > insertAfter) insertAfter--
70 const element = this.playlistElements[previousIndex]
72 this.playlistElements.splice(previousIndex, 1)
73 this.playlistElements.splice(newIndex, 0, element)
75 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
78 this.reorderClientPositions()
81 err => this.notifier.error(err.message)
85 onElementRemoved (element: VideoPlaylistElement) {
86 const oldFirst = this.findFirst()
88 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
89 this.reorderClientPositions(oldFirst)
94 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
96 this.pagination.currentPage += 1
100 trackByFn (index: number, elem: VideoPlaylistElement) {
105 * Returns null to not have drag and drop delay.
106 * In small views, where elements are about 100% wide,
107 * we add a delay to prevent unwanted drag&drop.
109 * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078}
111 * @returns {null|number} Null for no delay, or a number in milliseconds.
113 getDragStartDelay (): null | number {
114 if (this.screenService.isInTouchScreen()) {
121 private loadElements () {
122 this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
123 .subscribe(({ total, data }) => {
124 this.playlistElements = this.playlistElements.concat(data)
125 this.pagination.totalItems = total
127 this.onDataSubject.next(data)
131 private loadPlaylistInfo () {
132 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
133 .subscribe(playlist => {
134 this.playlist = playlist
138 private reorderClientPositions (first?: VideoPlaylistElement) {
139 if (this.playlistElements.length === 0) return
141 const oldFirst = first || this.findFirst()
144 for (const element of this.playlistElements) {
149 // Reload playlist thumbnail if the first element changed
150 const newFirst = this.findFirst()
151 if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
152 this.playlist.refreshThumbnail()
156 private findFirst () {
157 return this.playlistElements.find(e => e.position === 1)