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