]>
Commit | Line | Data |
---|---|---|
db400f44 C |
1 | import { from as observableFrom, Observable } from 'rxjs' |
2 | import { concatAll, tap } from 'rxjs/operators' | |
74d63469 | 3 | import { Component, OnDestroy, OnInit, Inject, LOCALE_ID, ViewChild } from '@angular/core' |
be447678 | 4 | import { ActivatedRoute, Router } from '@angular/router' |
2a2c19df | 5 | import { Location } from '@angular/common' |
0cd4344f C |
6 | import { immutableAssign } from '@app/shared/misc/utils' |
7 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
202f6b6c | 8 | import { NotificationsService } from 'angular2-notifications' |
b2731bff | 9 | import { AuthService } from '../../core/auth' |
332542bc | 10 | import { ConfirmService } from '../../core/confirm' |
be447678 | 11 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' |
332542bc | 12 | import { Video } from '../../shared/video/video.model' |
202f6b6c | 13 | import { VideoService } from '../../shared/video/video.service' |
b1d40cff | 14 | import { I18n } from '@ngx-translate/i18n-polyfill' |
bbe0f064 C |
15 | import { VideoPrivacy, VideoState } from '../../../../../shared/models/videos' |
16 | import { ScreenService } from '@app/shared/misc/screen.service' | |
74d63469 | 17 | import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component' |
202f6b6c C |
18 | |
19 | @Component({ | |
20 | selector: 'my-account-videos', | |
4bb6886d C |
21 | templateUrl: './my-account-videos.component.html', |
22 | styleUrls: [ './my-account-videos.component.scss' ] | |
202f6b6c | 23 | }) |
4bb6886d | 24 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { |
b1d40cff | 25 | titlePage: string |
4bb6886d | 26 | currentRoute = '/my-account/videos' |
ce0e281d | 27 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f | 28 | pagination: ComponentPagination = { |
234b535d | 29 | currentPage: 1, |
2e78e268 | 30 | itemsPerPage: 5, |
234b535d C |
31 | totalItems: null |
32 | } | |
202f6b6c | 33 | |
9af61e84 C |
34 | protected baseVideoWidth = -1 |
35 | protected baseVideoHeight = 155 | |
36 | ||
74d63469 GR |
37 | @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent |
38 | ||
b1d40cff C |
39 | constructor ( |
40 | protected router: Router, | |
41 | protected route: ActivatedRoute, | |
42 | protected authService: AuthService, | |
43 | protected notificationsService: NotificationsService, | |
b1d40cff | 44 | protected location: Location, |
bbe0f064 | 45 | protected screenService: ScreenService, |
b1d40cff | 46 | protected i18n: I18n, |
308c4275 | 47 | private confirmService: ConfirmService, |
bbe0f064 C |
48 | private videoService: VideoService, |
49 | @Inject(LOCALE_ID) private localeId: string | |
b1d40cff | 50 | ) { |
202f6b6c | 51 | super() |
b1d40cff C |
52 | |
53 | this.titlePage = this.i18n('My videos') | |
202f6b6c C |
54 | } |
55 | ||
56 | ngOnInit () { | |
57 | super.ngOnInit() | |
58 | } | |
59 | ||
9af61e84 C |
60 | ngOnDestroy () { |
61 | super.ngOnDestroy() | |
62 | } | |
63 | ||
ce0e281d C |
64 | abortSelectionMode () { |
65 | this.checkedVideos = {} | |
66 | } | |
67 | ||
68 | isInSelectionMode () { | |
c199c427 | 69 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true) |
ce0e281d C |
70 | } |
71 | ||
0cd4344f C |
72 | getVideosObservable (page: number) { |
73 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
74 | ||
75 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 76 | } |
332542bc | 77 | |
244e76a5 RK |
78 | generateSyndicationList () { |
79 | throw new Error('Method not implemented.') | |
80 | } | |
81 | ||
1f30a185 | 82 | async deleteSelectedVideos () { |
ce0e281d | 83 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
c199c427 | 84 | .filter(k => this.checkedVideos[ k ] === true) |
2186386c | 85 | .map(k => parseInt(k, 10)) |
ce0e281d | 86 | |
2186386c C |
87 | const res = await this.confirmService.confirm( |
88 | this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }), | |
89 | this.i18n('Delete') | |
90 | ) | |
1f30a185 C |
91 | if (res === false) return |
92 | ||
93 | const observables: Observable<any>[] = [] | |
94 | for (const videoId of toDeleteVideosIds) { | |
2186386c | 95 | const o = this.videoService.removeVideo(videoId) |
db400f44 | 96 | .pipe(tap(() => this.spliceVideosById(videoId))) |
1f30a185 C |
97 | |
98 | observables.push(o) | |
99 | } | |
100 | ||
2186386c C |
101 | observableFrom(observables) |
102 | .pipe(concatAll()) | |
1f30a185 C |
103 | .subscribe( |
104 | res => { | |
2186386c C |
105 | this.notificationsService.success( |
106 | this.i18n('Success'), | |
107 | this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length }) | |
108 | ) | |
109 | ||
06be7ed0 C |
110 | this.abortSelectionMode() |
111 | this.reloadVideos() | |
1f30a185 C |
112 | }, |
113 | ||
2186386c | 114 | err => this.notificationsService.error(this.i18n('Error'), err.message) |
1f30a185 | 115 | ) |
ce0e281d C |
116 | } |
117 | ||
1f30a185 | 118 | async deleteVideo (video: Video) { |
2186386c C |
119 | const res = await this.confirmService.confirm( |
120 | this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }), | |
121 | this.i18n('Delete') | |
122 | ) | |
1f30a185 C |
123 | if (res === false) return |
124 | ||
125 | this.videoService.removeVideo(video.id) | |
2186386c C |
126 | .subscribe( |
127 | status => { | |
128 | this.notificationsService.success( | |
129 | this.i18n('Success'), | |
130 | this.i18n('Video {{videoName}} deleted.', { videoName: video.name }) | |
131 | ) | |
132 | this.reloadVideos() | |
133 | }, | |
134 | ||
135 | error => this.notificationsService.error(this.i18n('Error'), error.message) | |
136 | ) | |
137 | } | |
1f30a185 | 138 | |
74d63469 GR |
139 | changeOwnership (event: Event, video: Video) { |
140 | event.preventDefault() | |
141 | this.videoChangeOwnershipModal.show(video) | |
142 | } | |
143 | ||
2186386c | 144 | getStateLabel (video: Video) { |
bbe0f064 C |
145 | let suffix: string |
146 | ||
147 | if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) { | |
148 | suffix = this.i18n('Published') | |
149 | } else if (video.scheduledUpdate) { | |
150 | const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId) | |
151 | suffix = this.i18n('Publication scheduled on ') + updateAt | |
152 | } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) { | |
153 | suffix = this.i18n('Waiting transcoding') | |
154 | } else if (video.state.id === VideoState.TO_TRANSCODE) { | |
155 | suffix = this.i18n('To transcode') | |
ed31c059 C |
156 | } else if (video.state.id === VideoState.TO_IMPORT) { |
157 | suffix = this.i18n('To import') | |
bbe0f064 C |
158 | } else { |
159 | return '' | |
160 | } | |
2186386c | 161 | |
bbe0f064 | 162 | return ' - ' + suffix |
332542bc | 163 | } |
ce0e281d | 164 | |
a86887a4 C |
165 | protected buildVideoHeight () { |
166 | // In account videos, the video height is fixed | |
167 | return this.baseVideoHeight | |
168 | } | |
169 | ||
ce0e281d | 170 | private spliceVideosById (id: number) { |
c199c427 C |
171 | for (const key of Object.keys(this.loadedPages)) { |
172 | const videos: Video[] = this.loadedPages[ key ] | |
173 | const index = videos.findIndex(v => v.id === id) | |
0cd4344f C |
174 | |
175 | if (index !== -1) { | |
176 | videos.splice(index, 1) | |
177 | return | |
178 | } | |
179 | } | |
ce0e281d | 180 | } |
202f6b6c | 181 | } |