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 { 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 { CdkDragDrop } from '@angular/cdk/drag-drop'
12 import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
15 selector: 'my-account-video-playlist-elements',
16 templateUrl: './my-account-video-playlist-elements.component.html',
17 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
19 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
20 playlistElements: VideoPlaylistElement[] = []
21 playlist: VideoPlaylist
23 pagination: ComponentPagination = {
29 private videoPlaylistId: string | number
30 private paramsSub: Subscription
33 private authService: AuthService,
34 private serverService: ServerService,
35 private notifier: Notifier,
36 private confirmService: ConfirmService,
37 private route: ActivatedRoute,
39 private videoPlaylistService: VideoPlaylistService
43 this.paramsSub = this.route.params.subscribe(routeParams => {
44 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
47 this.loadPlaylistInfo()
52 if (this.paramsSub) this.paramsSub.unsubscribe()
55 drop (event: CdkDragDrop<any>) {
56 const previousIndex = event.previousIndex
57 const newIndex = event.currentIndex
59 if (previousIndex === newIndex) return
61 const oldPosition = this.playlistElements[previousIndex].position
62 let insertAfter = this.playlistElements[newIndex].position
64 if (oldPosition > insertAfter) insertAfter--
66 const element = this.playlistElements[previousIndex]
68 this.playlistElements.splice(previousIndex, 1)
69 this.playlistElements.splice(newIndex, 0, element)
71 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
74 this.reorderClientPositions()
77 err => this.notifier.error(err.message)
81 onElementRemoved (element: VideoPlaylistElement) {
82 const oldFirst = this.findFirst()
84 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
85 this.reorderClientPositions(oldFirst)
90 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
92 this.pagination.currentPage += 1
96 trackByFn (index: number, elem: VideoPlaylistElement) {
100 private loadElements () {
101 this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
102 .subscribe(({ total, data }) => {
103 this.playlistElements = this.playlistElements.concat(data)
104 this.pagination.totalItems = total
108 private loadPlaylistInfo () {
109 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
110 .subscribe(playlist => {
111 this.playlist = playlist
115 private reorderClientPositions (first?: VideoPlaylistElement) {
116 if (this.playlistElements.length === 0) return
118 const oldFirst = first || this.findFirst()
121 for (const element of this.playlistElements) {
126 // Reload playlist thumbnail if the first element changed
127 const newFirst = this.findFirst()
128 if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
129 this.playlist.refreshThumbnail()
133 private findFirst () {
134 return this.playlistElements.find(e => e.position === 1)