]>
Commit | Line | Data |
---|---|---|
ad453580 | 1 | import { Subject, Subscription } from 'rxjs' |
bfbd9128 | 2 | import { CdkDragDrop } from '@angular/cdk/drag-drop' |
82f443de C |
3 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
4 | import { ActivatedRoute, Router } from '@angular/router' | |
5 | import { ComponentPagination, ConfirmService, Notifier, ScreenService } from '@app/core' | |
6 | import { DropdownAction } from '@app/shared/shared-main' | |
7 | import { VideoShareComponent } from '@app/shared/shared-share-modal' | |
67ed6552 | 8 | import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
82f443de | 9 | import { 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 | 15 | export 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 C |
59 | this.paramsSub = this.route.params.subscribe(routeParams => { |
60 | this.videoPlaylistId = routeParams[ 'videoPlaylistId' ] | |
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 C |
87 | this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter) |
88 | .subscribe( | |
89 | () => { | |
90 | this.reorderClientPositions() | |
91 | }, | |
92 | ||
93 | 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) | |
132 | .subscribe( | |
133 | () => { | |
17119e4a | 134 | this.router.navigate([ '/my-library', 'video-playlists' ]) |
66357162 | 135 | this.notifier.success($localize`Playlist ${videoPlaylist.displayName} deleted.`) |
82f443de C |
136 | }, |
137 | ||
138 | error => this.notifier.error(error.message) | |
139 | ) | |
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} | |
148 | * | |
149 | * @returns {null|number} Null for no delay, or a number in milliseconds. | |
150 | */ | |
151 | getDragStartDelay (): null | number { | |
152 | if (this.screenService.isInTouchScreen()) { | |
153 | return 500 | |
154 | } | |
155 | ||
156 | return null | |
157 | } | |
158 | ||
f0a39880 | 159 | private loadElements () { |
bfbd9128 | 160 | this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination) |
93cae479 | 161 | .subscribe(({ total, data }) => { |
bfbd9128 | 162 | this.playlistElements = this.playlistElements.concat(data) |
93cae479 | 163 | this.pagination.totalItems = total |
ad453580 C |
164 | |
165 | this.onDataSubject.next(data) | |
f0a39880 C |
166 | }) |
167 | } | |
c5a1ae50 C |
168 | |
169 | private loadPlaylistInfo () { | |
170 | this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId) | |
171 | .subscribe(playlist => { | |
172 | this.playlist = playlist | |
173 | }) | |
174 | } | |
15e9d5ca | 175 | |
65af03a2 C |
176 | private reorderClientPositions (first?: VideoPlaylistElement) { |
177 | if (this.playlistElements.length === 0) return | |
178 | ||
179 | const oldFirst = first || this.findFirst() | |
15e9d5ca C |
180 | let i = 1 |
181 | ||
bfbd9128 C |
182 | for (const element of this.playlistElements) { |
183 | element.position = i | |
15e9d5ca C |
184 | i++ |
185 | } | |
65af03a2 C |
186 | |
187 | // Reload playlist thumbnail if the first element changed | |
188 | const newFirst = this.findFirst() | |
189 | if (oldFirst && newFirst && oldFirst.id !== newFirst.id) { | |
190 | this.playlist.refreshThumbnail() | |
191 | } | |
192 | } | |
193 | ||
194 | private findFirst () { | |
195 | return this.playlistElements.find(e => e.position === 1) | |
15e9d5ca | 196 | } |
f0a39880 | 197 | } |