]>
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' | |
bfbd9128 | 6 | import { 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' | |
bfbd9128 C |
11 | import { CdkDragDrop } from '@angular/cdk/drag-drop' |
12 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
f0a39880 C |
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 { | |
bfbd9128 | 20 | playlistElements: VideoPlaylistElement[] = [] |
c5a1ae50 | 21 | playlist: VideoPlaylist |
f0a39880 C |
22 | |
23 | pagination: ComponentPagination = { | |
24 | currentPage: 1, | |
bce47964 | 25 | itemsPerPage: 30, |
f0a39880 C |
26 | totalItems: null |
27 | } | |
28 | ||
29 | private videoPlaylistId: string | number | |
30 | private paramsSub: Subscription | |
31 | ||
32 | constructor ( | |
33 | private authService: AuthService, | |
c5a1ae50 | 34 | private serverService: ServerService, |
f0a39880 C |
35 | private notifier: Notifier, |
36 | private confirmService: ConfirmService, | |
37 | private route: ActivatedRoute, | |
c5a1ae50 | 38 | private i18n: I18n, |
c5a1ae50 | 39 | private videoPlaylistService: VideoPlaylistService |
f0a39880 C |
40 | ) {} |
41 | ||
42 | ngOnInit () { | |
43 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
44 | this.videoPlaylistId = routeParams[ 'videoPlaylistId' ] | |
45 | this.loadElements() | |
c5a1ae50 C |
46 | |
47 | this.loadPlaylistInfo() | |
f0a39880 C |
48 | }) |
49 | } | |
50 | ||
51 | ngOnDestroy () { | |
52 | if (this.paramsSub) this.paramsSub.unsubscribe() | |
53 | } | |
54 | ||
15e9d5ca C |
55 | drop (event: CdkDragDrop<any>) { |
56 | const previousIndex = event.previousIndex | |
57 | const newIndex = event.currentIndex | |
58 | ||
59 | if (previousIndex === newIndex) return | |
60 | ||
bfbd9128 C |
61 | const oldPosition = this.playlistElements[previousIndex].position |
62 | let insertAfter = this.playlistElements[newIndex].position | |
b767c4a7 C |
63 | |
64 | if (oldPosition > insertAfter) insertAfter-- | |
15e9d5ca | 65 | |
bfbd9128 | 66 | const element = this.playlistElements[previousIndex] |
15e9d5ca | 67 | |
bfbd9128 C |
68 | this.playlistElements.splice(previousIndex, 1) |
69 | this.playlistElements.splice(newIndex, 0, element) | |
15e9d5ca | 70 | |
65af03a2 C |
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 | ) | |
15e9d5ca C |
79 | } |
80 | ||
bfbd9128 | 81 | onElementRemoved (element: VideoPlaylistElement) { |
65af03a2 C |
82 | const oldFirst = this.findFirst() |
83 | ||
bfbd9128 | 84 | this.playlistElements = this.playlistElements.filter(v => v.id !== element.id) |
65af03a2 | 85 | this.reorderClientPositions(oldFirst) |
c5a1ae50 C |
86 | } |
87 | ||
f0a39880 C |
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 | ||
bfbd9128 | 96 | trackByFn (index: number, elem: VideoPlaylistElement) { |
bce47964 C |
97 | return elem.id |
98 | } | |
99 | ||
f0a39880 | 100 | private loadElements () { |
bfbd9128 | 101 | this.videoPlaylistService.getPlaylistVideos(this.videoPlaylistId, this.pagination) |
93cae479 | 102 | .subscribe(({ total, data }) => { |
bfbd9128 | 103 | this.playlistElements = this.playlistElements.concat(data) |
93cae479 | 104 | this.pagination.totalItems = total |
f0a39880 C |
105 | }) |
106 | } | |
c5a1ae50 C |
107 | |
108 | private loadPlaylistInfo () { | |
109 | this.videoPlaylistService.getVideoPlaylist(this.videoPlaylistId) | |
110 | .subscribe(playlist => { | |
111 | this.playlist = playlist | |
112 | }) | |
113 | } | |
15e9d5ca | 114 | |
65af03a2 C |
115 | private reorderClientPositions (first?: VideoPlaylistElement) { |
116 | if (this.playlistElements.length === 0) return | |
117 | ||
118 | const oldFirst = first || this.findFirst() | |
15e9d5ca C |
119 | let i = 1 |
120 | ||
bfbd9128 C |
121 | for (const element of this.playlistElements) { |
122 | element.position = i | |
15e9d5ca C |
123 | i++ |
124 | } | |
65af03a2 C |
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) | |
15e9d5ca | 135 | } |
f0a39880 | 136 | } |