]> 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 reordering playlist
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-video-playlists / my-account-video-playlist-elements.component.ts
CommitLineData
e2f01c47 1import { Component, OnDestroy, OnInit } from '@angular/core'
c5a1ae50 2import { Notifier, ServerService } from '@app/core'
f0a39880
C
3import { AuthService } from '../../core/auth'
4import { ConfirmService } from '../../core/confirm'
5import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6import { Video } from '@app/shared/video/video.model'
15e9d5ca 7import { Subject, Subscription } from 'rxjs'
f0a39880
C
8import { ActivatedRoute } from '@angular/router'
9import { VideoService } from '@app/shared/video/video.service'
c5a1ae50
C
10import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
11import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
12import { I18n } from '@ngx-translate/i18n-polyfill'
15e9d5ca
C
13import { CdkDragDrop, CdkDragMove } from '@angular/cdk/drag-drop'
14import { throttleTime } from 'rxjs/operators'
f0a39880
C
15
16@Component({
17 selector: 'my-account-video-playlist-elements',
18 templateUrl: './my-account-video-playlist-elements.component.html',
19 styleUrls: [ './my-account-video-playlist-elements.component.scss' ]
20})
21export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy {
22 videos: Video[] = []
c5a1ae50 23 playlist: VideoPlaylist
f0a39880
C
24
25 pagination: ComponentPagination = {
26 currentPage: 1,
bce47964 27 itemsPerPage: 30,
f0a39880
C
28 totalItems: null
29 }
30
31 private videoPlaylistId: string | number
32 private paramsSub: Subscription
15e9d5ca 33 private dragMoveSubject = new Subject<number>()
f0a39880
C
34
35 constructor (
36 private authService: AuthService,
c5a1ae50 37 private serverService: ServerService,
f0a39880
C
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private route: ActivatedRoute,
c5a1ae50
C
41 private i18n: I18n,
42 private videoService: VideoService,
43 private videoPlaylistService: VideoPlaylistService
f0a39880
C
44 ) {}
45
46 ngOnInit () {
47 this.paramsSub = this.route.params.subscribe(routeParams => {
48 this.videoPlaylistId = routeParams[ 'videoPlaylistId' ]
49 this.loadElements()
c5a1ae50
C
50
51 this.loadPlaylistInfo()
f0a39880 52 })
15e9d5ca
C
53
54 this.dragMoveSubject.asObservable()
55 .pipe(throttleTime(200))
56 .subscribe(y => this.checkScroll(y))
f0a39880
C
57 }
58
59 ngOnDestroy () {
60 if (this.paramsSub) this.paramsSub.unsubscribe()
61 }
62
15e9d5ca
C
63 drop (event: CdkDragDrop<any>) {
64 const previousIndex = event.previousIndex
65 const newIndex = event.currentIndex
66
67 if (previousIndex === newIndex) return
68
69 const oldPosition = this.videos[previousIndex].playlistElement.position
b767c4a7
C
70 let insertAfter = this.videos[newIndex].playlistElement.position
71
72 if (oldPosition > insertAfter) insertAfter--
15e9d5ca
C
73
74 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
75 .subscribe(
76 () => { /* nothing to do */ },
77
78 err => this.notifier.error(err.message)
79 )
80
81 const video = this.videos[previousIndex]
82
83 this.videos.splice(previousIndex, 1)
84 this.videos.splice(newIndex, 0, video)
85
86 this.reorderClientPositions()
87 }
88
89 onDragMove (event: CdkDragMove<any>) {
90 this.dragMoveSubject.next(event.pointerPosition.y)
91 }
92
93 checkScroll (pointerY: number) {
94 // FIXME: Uncomment when https://github.com/angular/material2/issues/14098 is fixed
95 // FIXME: Remove when https://github.com/angular/material2/issues/13588 is implemented
96 // if (pointerY < 150) {
97 // window.scrollBy({
98 // left: 0,
99 // top: -20,
100 // behavior: 'smooth'
101 // })
102 //
103 // return
104 // }
105 //
106 // if (window.innerHeight - pointerY <= 50) {
107 // window.scrollBy({
108 // left: 0,
109 // top: 20,
110 // behavior: 'smooth'
111 // })
112 // }
113 }
114
e2f01c47
C
115 onElementRemoved (video: Video) {
116 this.videos = this.videos.filter(v => v.id !== video.id)
117 this.reorderClientPositions()
c5a1ae50
C
118 }
119
f0a39880
C
120 onNearOfBottom () {
121 // Last page
122 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
123
124 this.pagination.currentPage += 1
125 this.loadElements()
126 }
127
bce47964
C
128 trackByFn (index: number, elem: Video) {
129 return elem.id
130 }
131
f0a39880
C
132 private loadElements () {
133 this.videoService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
134 .subscribe(({ totalVideos, videos }) => {
135 this.videos = this.videos.concat(videos)
136 this.pagination.totalItems = totalVideos
137 })
138 }
c5a1ae50
C
139
140 private loadPlaylistInfo () {
141 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
142 .subscribe(playlist => {
143 this.playlist = playlist
144 })
145 }
15e9d5ca
C
146
147 private reorderClientPositions () {
148 let i = 1
149
150 for (const video of this.videos) {
151 video.playlistElement.position = i
152 i++
153 }
154 }
f0a39880 155}