]>
Commit | Line | Data |
---|---|---|
e2f01c47 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
c5a1ae50 | 2 | import { Notifier, ServerService } from '@app/core' |
f0a39880 C |
3 | import { AuthService } from '../../core/auth' |
4 | import { ConfirmService } from '../../core/confirm' | |
5 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
ad453580 | 6 | import { Subject, Subscription } from 'rxjs' |
f0a39880 | 7 | import { ActivatedRoute } from '@angular/router' |
c5a1ae50 C |
8 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' |
9 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' | |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
0c695c5c | 11 | import { ScreenService } from '@app/shared/misc/screen.service' |
bfbd9128 C |
12 | import { CdkDragDrop } from '@angular/cdk/drag-drop' |
13 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
f0a39880 C |
14 | |
15 | @Component({ | |
16 | selector: 'my-account-video-playlist-elements', | |
17 | templateUrl: './my-account-video-playlist-elements.component.html', | |
18 | styleUrls: [ './my-account-video-playlist-elements.component.scss' ] | |
19 | }) | |
20 | export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy { | |
bfbd9128 | 21 | playlistElements: VideoPlaylistElement[] = [] |
c5a1ae50 | 22 | playlist: VideoPlaylist |
f0a39880 C |
23 | |
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 ( | |
36 | private authService: AuthService, | |
c5a1ae50 | 37 | private serverService: ServerService, |
f0a39880 C |
38 | private notifier: Notifier, |
39 | private confirmService: ConfirmService, | |
40 | private route: ActivatedRoute, | |
c5a1ae50 | 41 | private i18n: I18n, |
0c695c5c | 42 | private screenService: ScreenService, |
c5a1ae50 | 43 | private videoPlaylistService: VideoPlaylistService |
f0a39880 C |
44 | ) {} |
45 | ||
46 | ngOnInit () { | |
47 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
48 | this.videoPlaylistId = routeParams[ 'videoPlaylistId' ] | |
49 | this.loadElements() | |
c5a1ae50 C |
50 | |
51 | this.loadPlaylistInfo() | |
f0a39880 C |
52 | }) |
53 | } | |
54 | ||
55 | ngOnDestroy () { | |
56 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
57 | } | |
58 | ||
15e9d5ca C |
59 | drop (event: CdkDragDrop<any>) { |
60 | const previousIndex = event.previousIndex | |
61 | const newIndex = event.currentIndex | |
62 | ||
63 | if (previousIndex === newIndex) return | |
64 | ||
bfbd9128 C |
65 | const oldPosition = this.playlistElements[previousIndex].position |
66 | let insertAfter = this.playlistElements[newIndex].position | |
b767c4a7 C |
67 | |
68 | if (oldPosition > insertAfter) insertAfter-- | |
15e9d5ca | 69 | |
bfbd9128 | 70 | const element = this.playlistElements[previousIndex] |
15e9d5ca | 71 | |
bfbd9128 C |
72 | this.playlistElements.splice(previousIndex, 1) |
73 | this.playlistElements.splice(newIndex, 0, element) | |
15e9d5ca | 74 | |
65af03a2 C |
75 | this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter) |
76 | .subscribe( | |
77 | () => { | |
78 | this.reorderClientPositions() | |
79 | }, | |
80 | ||
81 | err => this.notifier.error(err.message) | |
82 | ) | |
15e9d5ca C |
83 | } |
84 | ||
bfbd9128 | 85 | onElementRemoved (element: VideoPlaylistElement) { |
65af03a2 C |
86 | const oldFirst = this.findFirst() |
87 | ||
bfbd9128 | 88 | this.playlistElements = this.playlistElements.filter(v => v.id !== element.id) |
65af03a2 | 89 | this.reorderClientPositions(oldFirst) |
c5a1ae50 C |
90 | } |
91 | ||
f0a39880 C |
92 | onNearOfBottom () { |
93 | // Last page | |
94 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
95 | ||
96 | this.pagination.currentPage += 1 | |
97 | this.loadElements() | |
98 | } | |
99 | ||
bfbd9128 | 100 | trackByFn (index: number, elem: VideoPlaylistElement) { |
bce47964 C |
101 | return elem.id |
102 | } | |
103 | ||
0c695c5c JM |
104 | /** |
105 | * Returns null to not have drag and drop delay. | |
106 | * In small views, where elements are about 100% wide, | |
107 | * we add a delay to prevent unwanted drag&drop. | |
108 | * | |
109 | * @see {@link https://github.com/Chocobozzz/PeerTube/issues/2078} | |
110 | * | |
111 | * @returns {null|number} Null for no delay, or a number in milliseconds. | |
112 | */ | |
113 | getDragStartDelay (): null | number { | |
114 | if (this.screenService.isInTouchScreen()) { | |
115 | return 500 | |
116 | } | |
117 | ||
118 | return null | |
119 | } | |
120 | ||
f0a39880 | 121 | private loadElements () { |
bfbd9128 | 122 | this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination) |
93cae479 | 123 | .subscribe(({ total, data }) => { |
bfbd9128 | 124 | this.playlistElements = this.playlistElements.concat(data) |
93cae479 | 125 | this.pagination.totalItems = total |
ad453580 C |
126 | |
127 | this.onDataSubject.next(data) | |
f0a39880 C |
128 | }) |
129 | } | |
c5a1ae50 C |
130 | |
131 | private loadPlaylistInfo () { | |
132 | this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId) | |
133 | .subscribe(playlist => { | |
134 | this.playlist = playlist | |
135 | }) | |
136 | } | |
15e9d5ca | 137 | |
65af03a2 C |
138 | private reorderClientPositions (first?: VideoPlaylistElement) { |
139 | if (this.playlistElements.length === 0) return | |
140 | ||
141 | const oldFirst = first || this.findFirst() | |
15e9d5ca C |
142 | let i = 1 |
143 | ||
bfbd9128 C |
144 | for (const element of this.playlistElements) { |
145 | element.position = i | |
15e9d5ca C |
146 | i++ |
147 | } | |
65af03a2 C |
148 | |
149 | // Reload playlist thumbnail if the first element changed | |
150 | const newFirst = this.findFirst() | |
151 | if (oldFirst && newFirst && oldFirst.id !== newFirst.id) { | |
152 | this.playlist.refreshThumbnail() | |
153 | } | |
154 | } | |
155 | ||
156 | private findFirst () { | |
157 | return this.playlistElements.find(e => e.position === 1) | |
15e9d5ca | 158 | } |
f0a39880 | 159 | } |