]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
2 | import { Notifier, ServerService } from '@app/core' | |
3 | import { AuthService } from '../../core/auth' | |
4 | import { ConfirmService } from '../../core/confirm' | |
5 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
6 | import { Subscription } from 'rxjs' | |
7 | import { ActivatedRoute } from '@angular/router' | |
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' | |
11 | import { CdkDragDrop } from '@angular/cdk/drag-drop' | |
12 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
13 | ||
14 | @Component({ | |
15 | selector: 'my-account-video-playlist-elements', | |
16 | templateUrl: './my-account-video-playlist-elements.component.html', | |
17 | styleUrls: [ './my-account-video-playlist-elements.component.scss' ] | |
18 | }) | |
19 | export class MyAccountVideoPlaylistElementsComponent implements OnInit, OnDestroy { | |
20 | playlistElements: VideoPlaylistElement[] = [] | |
21 | playlist: VideoPlaylist | |
22 | ||
23 | pagination: ComponentPagination = { | |
24 | currentPage: 1, | |
25 | itemsPerPage: 30, | |
26 | totalItems: null | |
27 | } | |
28 | ||
29 | private videoPlaylistId: string | number | |
30 | private paramsSub: Subscription | |
31 | ||
32 | constructor ( | |
33 | private authService: AuthService, | |
34 | private serverService: ServerService, | |
35 | private notifier: Notifier, | |
36 | private confirmService: ConfirmService, | |
37 | private route: ActivatedRoute, | |
38 | private i18n: I18n, | |
39 | private videoPlaylistService: VideoPlaylistService | |
40 | ) {} | |
41 | ||
42 | ngOnInit () { | |
43 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
44 | this.videoPlaylistId = routeParams[ 'videoPlaylistId' ] | |
45 | this.loadElements() | |
46 | ||
47 | this.loadPlaylistInfo() | |
48 | }) | |
49 | } | |
50 | ||
51 | ngOnDestroy () { | |
52 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
53 | } | |
54 | ||
55 | drop (event: CdkDragDrop<any>) { | |
56 | const previousIndex = event.previousIndex | |
57 | const newIndex = event.currentIndex | |
58 | ||
59 | if (previousIndex === newIndex) return | |
60 | ||
61 | const oldPosition = this.playlistElements[previousIndex].position | |
62 | let insertAfter = this.playlistElements[newIndex].position | |
63 | ||
64 | if (oldPosition > insertAfter) insertAfter-- | |
65 | ||
66 | const element = this.playlistElements[previousIndex] | |
67 | ||
68 | this.playlistElements.splice(previousIndex, 1) | |
69 | this.playlistElements.splice(newIndex, 0, element) | |
70 | ||
71 | this.videoPlaylistService.reorderPlaylist(this.playlist.id, oldPosition, insertAfter) | |
72 | .subscribe( | |
73 | () => { | |
74 | this.reorderClientPositions() | |
75 | }, | |
76 | ||
77 | err => this.notifier.error(err.message) | |
78 | ) | |
79 | } | |
80 | ||
81 | onElementRemoved (element: VideoPlaylistElement) { | |
82 | const oldFirst = this.findFirst() | |
83 | ||
84 | this.playlistElements = this.playlistElements.filter(v => v.id !== element.id) | |
85 | this.reorderClientPositions(oldFirst) | |
86 | } | |
87 | ||
88 | onNearOfBottom () { | |
89 | // Last page | |
90 | if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return | |
91 | ||
92 | this.pagination.currentPage += 1 | |
93 | this.loadElements() | |
94 | } | |
95 | ||
96 | trackByFn (index: number, elem: VideoPlaylistElement) { | |
97 | return elem.id | |
98 | } | |
99 | ||
100 | private loadElements () { | |
101 | this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination) | |
102 | .subscribe(({ total, data }) => { | |
103 | this.playlistElements = this.playlistElements.concat(data) | |
104 | this.pagination.totalItems = total | |
105 | }) | |
106 | } | |
107 | ||
108 | private loadPlaylistInfo () { | |
109 | this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId) | |
110 | .subscribe(playlist => { | |
111 | this.playlist = playlist | |
112 | }) | |
113 | } | |
114 | ||
115 | private reorderClientPositions (first?: VideoPlaylistElement) { | |
116 | if (this.playlistElements.length === 0) return | |
117 | ||
118 | const oldFirst = first || this.findFirst() | |
119 | let i = 1 | |
120 | ||
121 | for (const element of this.playlistElements) { | |
122 | element.position = i | |
123 | i++ | |
124 | } | |
125 | ||
126 | // Reload playlist thumbnail if the first element changed | |
127 | const newFirst = this.findFirst() | |
128 | if (oldFirst && newFirst && oldFirst.id !== newFirst.id) { | |
129 | this.playlist.refreshThumbnail() | |
130 | } | |
131 | } | |
132 | ||
133 | private findFirst () { | |
134 | return this.playlistElements.find(e => e.position === 1) | |
135 | } | |
136 | } |