]>
Commit | Line | Data |
---|---|---|
1 | import { Subject, Subscription } from 'rxjs' | |
2 | import { CdkDragDrop } from '@angular/cdk/drag-drop' | |
3 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' | |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { ComponentPagination, ConfirmService, HooksService, Notifier, ScreenService } from '@app/core' | |
6 | import { DropdownAction } from '@app/shared/shared-main' | |
7 | import { VideoShareComponent } from '@app/shared/shared-share-modal' | |
8 | import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' | |
9 | import { VideoPlaylistType } from '@shared/models' | |
10 | ||
11 | @Component({ | |
12 | templateUrl: './my-video-playlist-elements.component.html', | |
13 | styleUrls: [ './my-video-playlist-elements.component.scss' ] | |
14 | }) | |
15 | export class MyVideoPlaylistElementsComponent implements OnInit, OnDestroy { | |
16 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent | |
17 | ||
18 | playlistElements: VideoPlaylistElement[] = [] | |
19 | playlist: VideoPlaylist | |
20 | ||
21 | playlistActions: DropdownAction<VideoPlaylist>[][] = [] | |
22 | ||
23 | pagination: ComponentPagination = { | |
24 | currentPage: 1, | |
25 | itemsPerPage: 10, | |
26 | totalItems: null | |
27 | } | |
28 | ||
29 | onDataSubject = new Subject<any[]>() | |
30 | ||
31 | private videoPlaylistId: string | number | |
32 | private paramsSub: Subscription | |
33 | ||
34 | constructor ( | |
35 | private hooks: HooksService, | |
36 | private notifier: Notifier, | |
37 | private router: Router, | |
38 | private confirmService: ConfirmService, | |
39 | private route: ActivatedRoute, | |
40 | private screenService: ScreenService, | |
41 | private videoPlaylistService: VideoPlaylistService | |
42 | ) {} | |
43 | ||
44 | ngOnInit () { | |
45 | this.playlistActions = [ | |
46 | [ | |
47 | { | |
48 | label: $localize`Update playlist`, | |
49 | iconName: 'edit', | |
50 | linkBuilder: playlist => [ '/my-library', 'video-playlists', 'update', playlist.uuid ] | |
51 | }, | |
52 | { | |
53 | label: $localize`Delete playlist`, | |
54 | iconName: 'delete', | |
55 | handler: playlist => this.deleteVideoPlaylist(playlist) | |
56 | } | |
57 | ] | |
58 | ] | |
59 | ||
60 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
61 | this.videoPlaylistId = routeParams['videoPlaylistId'] | |
62 | this.loadElements() | |
63 | ||
64 | this.loadPlaylistInfo() | |
65 | }) | |
66 | } | |
67 | ||
68 | ngOnDestroy () { | |
69 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
70 | } | |
71 | ||
72 | drop (event: CdkDragDrop<any>) { | |
73 | const previousIndex = event.previousIndex | |
74 | const newIndex = event.currentIndex | |
75 | ||
76 | if (previousIndex === newIndex) return | |
77 | ||
78 | const oldPosition = this.playlistElements[previousIndex].position | |
79 | let insertAfter = this.playlistElements[newIndex].position | |
80 | ||
81 | if (oldPosition > insertAfter) insertAfter-- | |
82 | ||
83 | const element = this.playlistElements[previousIndex] | |
84 | ||
85 | this.playlistElements.splice(previousIndex, 1) | |
86 | this.playlistElements.splice(newIndex, 0, element) | |
87 | ||
88 | this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter) | |
89 | .subscribe({ | |
90 | next: () => { | |
91 | this.reorderClientPositions() | |
92 | }, | |
93 | ||
94 | error: err => this.notifier.error(err.message) | |
95 | }) | |
96 | } | |
97 | ||
98 | onElementRemoved (element: VideoPlaylistElement) { | |
99 | const oldFirst = this.findFirst() | |
100 | ||
101 | this.playlistElements = this.playlistElements.filter(v => v.id !== element.id) | |
102 | this.reorderClientPositions(oldFirst) | |
103 | } | |
104 | ||
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 | ||
113 | trackByFn (index: number, elem: VideoPlaylistElement) { | |
114 | return elem.id | |
115 | } | |
116 | ||
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( | |
127 | $localize`Do you really want to delete ${videoPlaylist.displayName}?`, | |
128 | $localize`Delete` | |
129 | ) | |
130 | if (res === false) return | |
131 | ||
132 | this.videoPlaylistService.removeVideoPlaylist(videoPlaylist) | |
133 | .subscribe({ | |
134 | next: () => { | |
135 | this.router.navigate([ '/my-library', 'video-playlists' ]) | |
136 | this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`) | |
137 | }, | |
138 | ||
139 | error: err => this.notifier.error(err.message) | |
140 | }) | |
141 | } | |
142 | ||
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 | getDragStartDelay (): null | number { | |
151 | if (this.screenService.isInTouchScreen()) { | |
152 | return 500 | |
153 | } | |
154 | ||
155 | return null | |
156 | } | |
157 | ||
158 | private loadElements () { | |
159 | this.hooks.wrapObsFun( | |
160 | this.videoPlaylistService.getPlaylistVideos.bind(this.videoPlaylistService), | |
161 | { videoPlaylistId: this.videoPlaylistId, componentPagination: this.pagination }, | |
162 | 'my-library', | |
163 | 'filter:api.my-library.video-playlist-elements.list.params', | |
164 | 'filter:api.my-library.video-playlist-elements.list.result' | |
165 | ) | |
166 | .subscribe(({ total, data }) => { | |
167 | this.playlistElements = this.playlistElements.concat(data) | |
168 | this.pagination.totalItems = total | |
169 | ||
170 | this.onDataSubject.next(data) | |
171 | }) | |
172 | } | |
173 | ||
174 | private loadPlaylistInfo () { | |
175 | this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId) | |
176 | .subscribe(playlist => { | |
177 | this.playlist = playlist | |
178 | }) | |
179 | } | |
180 | ||
181 | private reorderClientPositions (first?: VideoPlaylistElement) { | |
182 | if (this.playlistElements.length === 0) return | |
183 | ||
184 | const oldFirst = first || this.findFirst() | |
185 | let i = 1 | |
186 | ||
187 | for (const element of this.playlistElements) { | |
188 | element.position = i | |
189 | i++ | |
190 | } | |
191 | ||
192 | // Reload playlist thumbnail if the first element changed | |
193 | const newFirst = this.findFirst() | |
194 | if (oldFirst && newFirst && oldFirst.id !== newFirst.id) { | |
195 | this.loadPlaylistInfo() | |
196 | } | |
197 | } | |
198 | ||
199 | private findFirst () { | |
200 | return this.playlistElements.find(e => e.position === 1) | |
201 | } | |
202 | } |