]>
Commit | Line | Data |
---|---|---|
bf64ed41 | 1 | import { concat, Observable, Subject } from 'rxjs' |
67ed6552 C |
2 | import { debounceTime, tap, toArray } from 'rxjs/operators' |
3 | import { Component, OnInit, ViewChild } from '@angular/core' | |
be447678 | 4 | import { ActivatedRoute, Router } from '@angular/router' |
733dbc53 | 5 | import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core' |
67ed6552 C |
6 | import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook' |
7 | import { immutableAssign } from '@app/helpers' | |
d846d99c | 8 | import { DropdownAction, Video, VideoService } from '@app/shared/shared-main' |
f8c00564 | 9 | import { LiveStreamInformationComponent } from '@app/shared/shared-video-live' |
733dbc53 | 10 | import { MiniatureDisplayOptions, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature' |
67ed6552 | 11 | import { VideoSortField } from '@shared/models' |
d846d99c | 12 | import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component' |
202f6b6c C |
13 | |
14 | @Component({ | |
17119e4a C |
15 | templateUrl: './my-videos.component.html', |
16 | styleUrls: [ './my-videos.component.scss' ] | |
202f6b6c | 17 | }) |
17119e4a | 18 | export class MyVideosComponent implements OnInit, DisableForReuseHook { |
f36da21e C |
19 | @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent |
20 | @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent | |
d846d99c | 21 | @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent |
489290b8 | 22 | |
b1d40cff | 23 | titlePage: string |
693263e9 | 24 | selection: SelectionType = {} |
0cd4344f | 25 | pagination: ComponentPagination = { |
234b535d | 26 | currentPage: 1, |
1d904806 | 27 | itemsPerPage: 10, |
234b535d C |
28 | totalItems: null |
29 | } | |
e2409062 C |
30 | miniatureDisplayOptions: MiniatureDisplayOptions = { |
31 | date: true, | |
32 | views: true, | |
c4a6f790 | 33 | by: true, |
e2409062 C |
34 | privacyLabel: false, |
35 | privacyText: true, | |
36 | state: true, | |
37 | blacklistInfo: true | |
38 | } | |
c4a6f790 | 39 | |
d846d99c C |
40 | videoActions: DropdownAction<{ video: Video }>[] = [] |
41 | ||
693263e9 | 42 | videos: Video[] = [] |
bf64ed41 RK |
43 | videosSearch: string |
44 | videosSearchChanged = new Subject<string>() | |
693263e9 | 45 | getVideosObservableFunction = this.getVideosObservable.bind(this) |
8e286cdc | 46 | sort: VideoSortField = '-publishedAt' |
202f6b6c | 47 | |
241609f1 C |
48 | user: User |
49 | ||
b1d40cff C |
50 | constructor ( |
51 | protected router: Router, | |
489290b8 | 52 | protected serverService: ServerService, |
b1d40cff C |
53 | protected route: ActivatedRoute, |
54 | protected authService: AuthService, | |
f8b2c1b4 | 55 | protected notifier: Notifier, |
bbe0f064 | 56 | protected screenService: ScreenService, |
308c4275 | 57 | private confirmService: ConfirmService, |
693263e9 | 58 | private videoService: VideoService |
b1d40cff | 59 | ) { |
66357162 | 60 | this.titlePage = $localize`My videos` |
202f6b6c C |
61 | } |
62 | ||
bf64ed41 | 63 | ngOnInit () { |
d846d99c C |
64 | this.buildActions() |
65 | ||
241609f1 C |
66 | this.user = this.authService.getUser() |
67 | ||
bf64ed41 | 68 | this.videosSearchChanged |
4f5d0459 | 69 | .pipe(debounceTime(500)) |
bf64ed41 RK |
70 | .subscribe(() => { |
71 | this.videosSelection.reloadVideos() | |
72 | }) | |
73 | } | |
74 | ||
4f5d0459 RK |
75 | resetSearch () { |
76 | this.videosSearch = '' | |
77 | this.onVideosSearchChanged() | |
78 | } | |
79 | ||
bf64ed41 RK |
80 | onVideosSearchChanged () { |
81 | this.videosSearchChanged.next() | |
82 | } | |
83 | ||
8e286cdc RK |
84 | onChangeSortColumn () { |
85 | this.videosSelection.reloadVideos() | |
86 | } | |
87 | ||
693263e9 C |
88 | disableForReuse () { |
89 | this.videosSelection.disableForReuse() | |
9af61e84 C |
90 | } |
91 | ||
693263e9 C |
92 | enabledForReuse () { |
93 | this.videosSelection.enabledForReuse() | |
ce0e281d C |
94 | } |
95 | ||
0df302ca | 96 | getVideosObservable (page: number) { |
0cd4344f C |
97 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) |
98 | ||
8e286cdc | 99 | return this.videoService.getMyVideos(newPagination, this.sort, this.videosSearch) |
aa0f1963 RK |
100 | .pipe( |
101 | tap(res => this.pagination.totalItems = res.total) | |
102 | ) | |
244e76a5 RK |
103 | } |
104 | ||
1f30a185 | 105 | async deleteSelectedVideos () { |
693263e9 C |
106 | const toDeleteVideosIds = Object.keys(this.selection) |
107 | .filter(k => this.selection[ k ] === true) | |
2186386c | 108 | .map(k => parseInt(k, 10)) |
ce0e281d | 109 | |
2186386c | 110 | const res = await this.confirmService.confirm( |
66357162 C |
111 | $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`, |
112 | $localize`Delete` | |
2186386c | 113 | ) |
1f30a185 C |
114 | if (res === false) return |
115 | ||
116 | const observables: Observable<any>[] = [] | |
117 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 118 | const o = this.videoService.removeVideo(videoId) |
489290b8 | 119 | .pipe(tap(() => this.removeVideoFromArray(videoId))) |
1f30a185 C |
120 | |
121 | observables.push(o) | |
122 | } | |
123 | ||
489290b8 C |
124 | concat(...observables) |
125 | .pipe(toArray()) | |
1f30a185 | 126 | .subscribe( |
489290b8 | 127 | () => { |
66357162 | 128 | this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`) |
693263e9 | 129 | this.selection = {} |
1f30a185 C |
130 | }, |
131 | ||
f8b2c1b4 | 132 | err => this.notifier.error(err.message) |
1f30a185 | 133 | ) |
ce0e281d C |
134 | } |
135 | ||
1f30a185 | 136 | async deleteVideo (video: Video) { |
2186386c | 137 | const res = await this.confirmService.confirm( |
66357162 C |
138 | $localize`Do you really want to delete ${video.name}?`, |
139 | $localize`Delete` | |
2186386c | 140 | ) |
1f30a185 C |
141 | if (res === false) return |
142 | ||
143 | this.videoService.removeVideo(video.id) | |
2186386c | 144 | .subscribe( |
f8b2c1b4 | 145 | () => { |
66357162 | 146 | this.notifier.success($localize`Video ${video.name} deleted.`) |
693263e9 | 147 | this.removeVideoFromArray(video.id) |
2186386c C |
148 | }, |
149 | ||
f8b2c1b4 | 150 | error => this.notifier.error(error.message) |
2186386c C |
151 | ) |
152 | } | |
1f30a185 | 153 | |
d846d99c | 154 | changeOwnership (video: Video) { |
74d63469 GR |
155 | this.videoChangeOwnershipModal.show(video) |
156 | } | |
157 | ||
d846d99c C |
158 | displayLiveInformation (video: Video) { |
159 | this.liveStreamInformationModal.show(video) | |
160 | } | |
161 | ||
489290b8 C |
162 | private removeVideoFromArray (id: number) { |
163 | this.videos = this.videos.filter(v => v.id !== id) | |
ce0e281d | 164 | } |
d846d99c C |
165 | |
166 | private buildActions () { | |
167 | this.videoActions = [ | |
168 | { | |
169 | label: $localize`Display live information`, | |
170 | handler: ({ video }) => this.displayLiveInformation(video), | |
171 | isDisplayed: ({ video }) => video.isLive, | |
172 | iconName: 'live' | |
173 | }, | |
174 | { | |
175 | label: $localize`Change ownership`, | |
176 | handler: ({ video }) => this.changeOwnership(video), | |
177 | iconName: 'ownership-change' | |
178 | }, | |
179 | { | |
180 | label: $localize`Delete`, | |
181 | handler: ({ video }) => this.deleteVideo(video), | |
182 | iconName: 'delete' | |
183 | } | |
184 | ] | |
185 | } | |
202f6b6c | 186 | } |