]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob
6434b9e50fa89e39e14d73bab1e81f5a21b4e960
[github/Chocobozzz/PeerTube.git] /
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'
13
14 @Component({
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' ]
18 })
19 export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
20 playlistElements: VideoPlaylistElement[] = []
21 playlist: VideoPlaylist
22
23 pagination: ComponentPagination = {
24 currentPage: 1,
25 itemsPerPage: 30,
26 totalItems: null
27 }
28
29 private videoPlaylistId: string | number
30 private paramsSub: Subscription
31
32 constructor (
33 private authService: AuthService,
34 private serverService: ServerService,
35 private notifier: Notifier,
36 private confirmService: ConfirmService,
37 private route: ActivatedRoute,
38 private i18n: I18n,
39 private videoPlaylistService: VideoPlaylistService
40 ) {}
41
42 ngOnInit () {
43 this.paramsSub = this.route.params.subscribe(routeParams => {
44 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
45 this.loadElements()
46
47 this.loadPlaylistInfo()
48 })
49 }
50
51 ngOnDestroy () {
52 if (this.paramsSub) this.paramsSub.unsubscribe()
53 }
54
55 drop (event: CdkDragDrop<any>) {
56 const previousIndex = event.previousIndex
57 const newIndex = event.currentIndex
58
59 if (previousIndex === newIndex) return
60
61 const oldPosition = this.playlistElements[previousIndex].position
62 let insertAfter = this.playlistElements[newIndex].position
63
64 if (oldPosition > insertAfter) insertAfter--
65
66 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
67 .subscribe(
68 () => { /* nothing to do */ },
69
70 err => this.notifier.error(err.message)
71 )
72
73 const element = this.playlistElements[previousIndex]
74
75 this.playlistElements.splice(previousIndex, 1)
76 this.playlistElements.splice(newIndex, 0, element)
77
78 this.reorderClientPositions()
79 }
80
81 onElementRemoved (element: VideoPlaylistElement) {
82 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
83 this.reorderClientPositions()
84 }
85
86 onNearOfBottom () {
87 // Last page
88 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
89
90 this.pagination.currentPage += 1
91 this.loadElements()
92 }
93
94 trackByFn (index: number, elem: VideoPlaylistElement) {
95 return elem.id
96 }
97
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
103 })
104 }
105
106 private loadPlaylistInfo () {
107 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
108 .subscribe(playlist => {
109 this.playlist = playlist
110 })
111 }
112
113 private reorderClientPositions () {
114 let i = 1
115
116 for (const element of this.playlistElements) {
117 element.position = i
118 i++
119 }
120 }
121 }