]>
Commit | Line | Data |
---|---|---|
9af61e84 | 1 | import { Component, OnInit, OnDestroy } from '@angular/core' |
be447678 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
0cd4344f C |
3 | import { immutableAssign } from '@app/shared/misc/utils' |
4 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
202f6b6c | 5 | import { NotificationsService } from 'angular2-notifications' |
ce0e281d C |
6 | import 'rxjs/add/observable/from' |
7 | import 'rxjs/add/operator/concatAll' | |
8 | import { Observable } from 'rxjs/Observable' | |
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 C |
13 | import { VideoService } from '../../shared/video/video.service' |
14 | ||
15 | @Component({ | |
16 | selector: 'my-account-videos', | |
17 | templateUrl: './account-videos.component.html', | |
18 | styleUrls: [ './account-videos.component.scss' ] | |
19 | }) | |
9af61e84 | 20 | export class AccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { |
202f6b6c C |
21 | titlePage = 'My videos' |
22 | currentRoute = '/account/videos' | |
ce0e281d | 23 | checkedVideos: { [ id: number ]: boolean } = {} |
0cd4344f | 24 | pagination: ComponentPagination = { |
234b535d | 25 | currentPage: 1, |
2e78e268 | 26 | itemsPerPage: 5, |
234b535d C |
27 | totalItems: null |
28 | } | |
202f6b6c | 29 | |
9af61e84 C |
30 | protected baseVideoWidth = -1 |
31 | protected baseVideoHeight = 155 | |
32 | ||
202f6b6c C |
33 | constructor (protected router: Router, |
34 | protected route: ActivatedRoute, | |
b2731bff | 35 | protected authService: AuthService, |
202f6b6c | 36 | protected notificationsService: NotificationsService, |
332542bc | 37 | protected confirmService: ConfirmService, |
202f6b6c C |
38 | private videoService: VideoService) { |
39 | super() | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | super.ngOnInit() | |
cc1561f9 C |
44 | |
45 | // this.generateSyndicationList() | |
202f6b6c C |
46 | } |
47 | ||
9af61e84 C |
48 | ngOnDestroy () { |
49 | super.ngOnDestroy() | |
50 | } | |
51 | ||
ce0e281d C |
52 | abortSelectionMode () { |
53 | this.checkedVideos = {} | |
54 | } | |
55 | ||
56 | isInSelectionMode () { | |
57 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
58 | } | |
59 | ||
0cd4344f C |
60 | getVideosObservable (page: number) { |
61 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
62 | ||
63 | return this.videoService.getMyVideos(newPagination, this.sort) | |
202f6b6c | 64 | } |
332542bc | 65 | |
244e76a5 RK |
66 | generateSyndicationList () { |
67 | throw new Error('Method not implemented.') | |
68 | } | |
69 | ||
1f30a185 | 70 | async deleteSelectedVideos () { |
ce0e281d C |
71 | const toDeleteVideosIds = Object.keys(this.checkedVideos) |
72 | .filter(k => this.checkedVideos[k] === true) | |
73 | .map(k => parseInt(k, 10)) | |
74 | ||
1f30a185 C |
75 | const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete') |
76 | if (res === false) return | |
77 | ||
78 | const observables: Observable<any>[] = [] | |
79 | for (const videoId of toDeleteVideosIds) { | |
80 | const o = this.videoService | |
81 | .removeVideo(videoId) | |
82 | .do(() => this.spliceVideosById(videoId)) | |
83 | ||
84 | observables.push(o) | |
85 | } | |
86 | ||
87 | Observable.from(observables) | |
88 | .concatAll() | |
89 | .subscribe( | |
90 | res => { | |
91 | this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`) | |
92 | this.buildVideoPages() | |
93 | }, | |
94 | ||
95 | err => this.notificationsService.error('Error', err.message) | |
96 | ) | |
ce0e281d C |
97 | } |
98 | ||
1f30a185 C |
99 | async deleteVideo (video: Video) { |
100 | const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete') | |
101 | if (res === false) return | |
102 | ||
103 | this.videoService.removeVideo(video.id) | |
104 | .subscribe( | |
105 | status => { | |
106 | this.notificationsService.success('Success', `Video ${video.name} deleted.`) | |
107 | this.spliceVideosById(video.id) | |
108 | this.buildVideoPages() | |
109 | }, | |
110 | ||
111 | error => this.notificationsService.error('Error', error.message) | |
112 | ) | |
332542bc | 113 | } |
ce0e281d | 114 | |
a86887a4 C |
115 | protected buildVideoHeight () { |
116 | // In account videos, the video height is fixed | |
117 | return this.baseVideoHeight | |
118 | } | |
119 | ||
ce0e281d | 120 | private spliceVideosById (id: number) { |
0cd4344f C |
121 | for (const key of Object.keys(this.loadedPages)) { |
122 | const videos = this.loadedPages[key] | |
123 | const index = videos.findIndex(v => v.id === id) | |
124 | ||
125 | if (index !== -1) { | |
126 | videos.splice(index, 1) | |
127 | return | |
128 | } | |
129 | } | |
ce0e281d | 130 | } |
202f6b6c | 131 | } |