]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-account/my-account-video-playlists/my-account-video-playlist-elements.component.ts
fix reactive file upload button
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-elements.component.ts
CommitLineData
ad453580 1import { Subject, Subscription } from 'rxjs'
bfbd9128 2import { CdkDragDrop } from '@angular/cdk/drag-drop'
67ed6552
C
3import { Component, OnDestroy, OnInit } from '@angular/core'
4import { ActivatedRoute } from '@angular/router'
5import { ComponentPagination, Notifier, ScreenService } from '@app/core'
6import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
f0a39880
C
7
8@Component({
9 selector: 'my-account-video-playlist-elements',
10 templateUrl: './my-account-video-playlist-elements.component.html',
11 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
12})
13export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
bfbd9128 14 playlistElements: VideoPlaylistElement[] = []
c5a1ae50 15 playlist: VideoPlaylist
f0a39880
C
16
17 pagination: ComponentPagination = {
18 currentPage: 1,
ad453580 19 itemsPerPage: 10,
f0a39880
C
20 totalItems: null
21 }
22
ad453580
C
23 onDataSubject = new Subject<any[]>()
24
f0a39880
C
25 private videoPlaylistId: string | number
26 private paramsSub: Subscription
27
28 constructor (
f0a39880 29 private notifier: Notifier,
f0a39880 30 private route: ActivatedRoute,
0c695c5c 31 private screenService: ScreenService,
c5a1ae50 32 private videoPlaylistService: VideoPlaylistService
f0a39880
C
33 ) {}
34
35 ngOnInit () {
36 this.paramsSub = this.route.params.subscribe(routeParams => {
37 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
38 this.loadElements()
c5a1ae50
C
39
40 this.loadPlaylistInfo()
f0a39880
C
41 })
42 }
43
44 ngOnDestroy () {
45 if (this.paramsSub) this.paramsSub.unsubscribe()
46 }
47
15e9d5ca
C
48 drop (event: CdkDragDrop<any>) {
49 const previousIndex = event.previousIndex
50 const newIndex = event.currentIndex
51
52 if (previousIndex === newIndex) return
53
bfbd9128
C
54 const oldPosition = this.playlistElements[previousIndex].position
55 let insertAfter = this.playlistElements[newIndex].position
b767c4a7
C
56
57 if (oldPosition > insertAfter) insertAfter--
15e9d5ca 58
bfbd9128 59 const element = this.playlistElements[previousIndex]
15e9d5ca 60
bfbd9128
C
61 this.playlistElements.splice(previousIndex, 1)
62 this.playlistElements.splice(newIndex, 0, element)
15e9d5ca 63
65af03a2
C
64 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
65 .subscribe(
66 () => {
67 this.reorderClientPositions()
68 },
69
70 err => this.notifier.error(err.message)
71 )
15e9d5ca
C
72 }
73
bfbd9128 74 onElementRemoved (element: VideoPlaylistElement) {
65af03a2
C
75 const oldFirst = this.findFirst()
76
bfbd9128 77 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
65af03a2 78 this.reorderClientPositions(oldFirst)
c5a1ae50
C
79 }
80
f0a39880
C
81 onNearOfBottom () {
82 // Last page
83 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
84
85 this.pagination.currentPage += 1
86 this.loadElements()
87 }
88
bfbd9128 89 trackByFn (index: number, elem: VideoPlaylistElement) {
bce47964
C
90 return elem.id
91 }
92
0c695c5c
JM
93 /**
94 * Returns null to not have drag and drop delay.
95 * In small views, where elements are about 100% wide,
96 * we add a delay to prevent unwanted drag&drop.
97 *
98 * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078}
99 *
100 * @returns {null|number} Null for no delay, or a number in milliseconds.
101 */
102 getDragStartDelay (): null | number {
103 if (this.screenService.isInTouchScreen()) {
104 return 500
105 }
106
107 return null
108 }
109
f0a39880 110 private loadElements () {
bfbd9128 111 this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
93cae479 112 .subscribe(({ total, data }) => {
bfbd9128 113 this.playlistElements = this.playlistElements.concat(data)
93cae479 114 this.pagination.totalItems = total
ad453580
C
115
116 this.onDataSubject.next(data)
f0a39880
C
117 })
118 }
c5a1ae50
C
119
120 private loadPlaylistInfo () {
121 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
122 .subscribe(playlist => {
123 this.playlist = playlist
124 })
125 }
15e9d5ca 126
65af03a2
C
127 private reorderClientPositions (first?: VideoPlaylistElement) {
128 if (this.playlistElements.length === 0) return
129
130 const oldFirst = first || this.findFirst()
15e9d5ca
C
131 let i = 1
132
bfbd9128
C
133 for (const element of this.playlistElements) {
134 element.position = i
15e9d5ca
C
135 i++
136 }
65af03a2
C
137
138 // Reload playlist thumbnail if the first element changed
139 const newFirst = this.findFirst()
140 if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
141 this.playlist.refreshThumbnail()
142 }
143 }
144
145 private findFirst () {
146 return this.playlistElements.find(e => e.position === 1)
15e9d5ca 147 }
f0a39880 148}