]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-video-playlists/my-video-playlist-elements.component.ts
Migrate client to eslint
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-video-playlists / my-video-playlist-elements.component.ts
CommitLineData
ad453580 1import { Subject, Subscription } from 'rxjs'
bfbd9128 2import { CdkDragDrop } from '@angular/cdk/drag-drop'
82f443de
C
3import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4import { ActivatedRoute, Router } from '@angular/router'
5import { ComponentPagination, ConfirmService, Notifier, ScreenService } from '@app/core'
6import { DropdownAction } from '@app/shared/shared-main'
7import { VideoShareComponent } from '@app/shared/shared-share-modal'
67ed6552 8import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
82f443de 9import { VideoPlaylistType } from '@shared/models'
f0a39880
C
10
11@Component({
17119e4a
C
12 templateUrl: './my-video-playlist-elements.component.html',
13 styleUrls: [ './my-video-playlist-elements.component.scss' ]
f0a39880 14})
17119e4a 15export class MyVideoPlaylistElementsComponent implements OnInit, OnDestroy {
82f443de
C
16 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
17
bfbd9128 18 playlistElements: VideoPlaylistElement[] = []
c5a1ae50 19 playlist: VideoPlaylist
f0a39880 20
82f443de
C
21 playlistActions: DropdownAction<VideoPlaylist>[][] = []
22
f0a39880
C
23 pagination: ComponentPagination = {
24 currentPage: 1,
ad453580 25 itemsPerPage: 10,
f0a39880
C
26 totalItems: null
27 }
28
ad453580
C
29 onDataSubject = new Subject<any[]>()
30
f0a39880
C
31 private videoPlaylistId: string | number
32 private paramsSub: Subscription
33
34 constructor (
f0a39880 35 private notifier: Notifier,
82f443de
C
36 private router: Router,
37 private confirmService: ConfirmService,
f0a39880 38 private route: ActivatedRoute,
0c695c5c 39 private screenService: ScreenService,
c5a1ae50 40 private videoPlaylistService: VideoPlaylistService
f0a39880
C
41 ) {}
42
43 ngOnInit () {
82f443de
C
44 this.playlistActions = [
45 [
46 {
66357162 47 label: $localize`Update playlist`,
82f443de 48 iconName: 'edit',
17119e4a 49 linkBuilder: playlist => [ '/my-library', 'video-playlists', 'update', playlist.uuid ]
82f443de
C
50 },
51 {
66357162 52 label: $localize`Delete playlist`,
82f443de
C
53 iconName: 'delete',
54 handler: playlist => this.deleteVideoPlaylist(playlist)
55 }
56 ]
57 ]
58
f0a39880 59 this.paramsSub = this.route.params.subscribe(routeParams => {
9df52d66 60 this.videoPlaylistId = routeParams['videoPlaylistId']
f0a39880 61 this.loadElements()
c5a1ae50
C
62
63 this.loadPlaylistInfo()
f0a39880
C
64 })
65 }
66
67 ngOnDestroy () {
68 if (this.paramsSub) this.paramsSub.unsubscribe()
69 }
70
15e9d5ca
C
71 drop (event: CdkDragDrop<any>) {
72 const previousIndex = event.previousIndex
73 const newIndex = event.currentIndex
74
75 if (previousIndex === newIndex) return
76
bfbd9128
C
77 const oldPosition = this.playlistElements[previousIndex].position
78 let insertAfter = this.playlistElements[newIndex].position
b767c4a7
C
79
80 if (oldPosition > insertAfter) insertAfter--
15e9d5ca 81
bfbd9128 82 const element = this.playlistElements[previousIndex]
15e9d5ca 83
bfbd9128
C
84 this.playlistElements.splice(previousIndex, 1)
85 this.playlistElements.splice(newIndex, 0, element)
15e9d5ca 86
65af03a2 87 this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter)
1378c0d3
C
88 .subscribe({
89 next: () => {
65af03a2
C
90 this.reorderClientPositions()
91 },
92
1378c0d3
C
93 error: err => this.notifier.error(err.message)
94 })
15e9d5ca
C
95 }
96
bfbd9128 97 onElementRemoved (element: VideoPlaylistElement) {
65af03a2
C
98 const oldFirst = this.findFirst()
99
bfbd9128 100 this.playlistElements = this.playlistElements.filter(v => v.id !== element.id)
65af03a2 101 this.reorderClientPositions(oldFirst)
c5a1ae50
C
102 }
103
f0a39880
C
104 onNearOfBottom () {
105 // Last page
106 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
107
108 this.pagination.currentPage += 1
109 this.loadElements()
110 }
111
bfbd9128 112 trackByFn (index: number, elem: VideoPlaylistElement) {
bce47964
C
113 return elem.id
114 }
115
82f443de
C
116 isRegularPlaylist (playlist: VideoPlaylist) {
117 return playlist?.type.id === VideoPlaylistType.REGULAR
118 }
119
120 showShareModal () {
121 this.videoShareModal.show()
122 }
123
124 async deleteVideoPlaylist (videoPlaylist: VideoPlaylist) {
125 const res = await this.confirmService.confirm(
66357162
C
126 $localize`Do you really want to delete ${videoPlaylist.displayName}?`,
127 $localize`Delete`
82f443de
C
128 )
129 if (res === false) return
130
131 this.videoPlaylistService.removeVideoPlaylist(videoPlaylist)
1378c0d3
C
132 .subscribe({
133 next: () => {
17119e4a 134 this.router.navigate([ '/my-library', 'video-playlists' ])
66357162 135 this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`)
82f443de
C
136 },
137
1378c0d3
C
138 error: err => this.notifier.error(err.message)
139 })
82f443de
C
140 }
141
0c695c5c
JM
142 /**
143 * Returns null to not have drag and drop delay.
144 * In small views, where elements are about 100% wide,
145 * we add a delay to prevent unwanted drag&drop.
146 *
147 * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078}
0c695c5c
JM
148 */
149 getDragStartDelay (): null | number {
150 if (this.screenService.isInTouchScreen()) {
151 return 500
152 }
153
154 return null
155 }
156
f0a39880 157 private loadElements () {
bfbd9128 158 this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination)
93cae479 159 .subscribe(({ total, data }) => {
bfbd9128 160 this.playlistElements = this.playlistElements.concat(data)
93cae479 161 this.pagination.totalItems = total
ad453580
C
162
163 this.onDataSubject.next(data)
f0a39880
C
164 })
165 }
c5a1ae50
C
166
167 private loadPlaylistInfo () {
168 this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId)
169 .subscribe(playlist => {
170 this.playlist = playlist
171 })
172 }
15e9d5ca 173
65af03a2
C
174 private reorderClientPositions (first?: VideoPlaylistElement) {
175 if (this.playlistElements.length === 0) return
176
177 const oldFirst = first || this.findFirst()
15e9d5ca
C
178 let i = 1
179
bfbd9128
C
180 for (const element of this.playlistElements) {
181 element.position = i
15e9d5ca
C
182 i++
183 }
65af03a2
C
184
185 // Reload playlist thumbnail if the first element changed
186 const newFirst = this.findFirst()
187 if (oldFirst && newFirst && oldFirst.id !== newFirst.id) {
37a44fc9 188 this.loadPlaylistInfo()
65af03a2
C
189 }
190 }
191
192 private findFirst () {
193 return this.playlistElements.find(e => e.position === 1)
15e9d5ca 194 }
f0a39880 195}