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