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 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
68 () => { /* nothing to do */ },
70 err => this.notifier.error(err.message)
73 const element = this.playlistElements[previousIndex]
75 this.playlistElements.splice(previousIndex, 1)
76 this.playlistElements.splice(newIndex, 0, element)
78 this.reorderClientPositions()
81 onElementRemoved (element: VideoPlaylistElement) {
82 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
83 this.reorderClientPositions()
88 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
90 this.pagination.currentPage += 1
94 trackByFn (index: number, elem: VideoPlaylistElement) {
98 private loadElements () {
99 this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
100 .subscribe(({ total, data }) => {
101 this.playlistElements = this.playlistElements.concat(data)
102 this.pagination.totalItems = total
106 private loadPlaylistInfo () {
107 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
108 .subscribe(playlist => {
109 this.playlist = playlist
113 private reorderClientPositions () {
116 for (const element of this.playlistElements) {