]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, OnDestroy } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { Location } from '@angular/common' | |
4 | import { immutableAssign } from '@app/shared/misc/utils' | |
5 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
6 | import { NotificationsService } from 'angular2-notifications' | |
7 | import 'rxjs/add/observable/from' | |
8 | import 'rxjs/add/operator/concatAll' | |
9 | import { Observable } from 'rxjs/Observable' | |
10 | import { AuthService } from '../../core/auth' | |
11 | import { ConfirmService } from '../../core/confirm' | |
12 | import { AbstractVideoList } from '../../shared/video/abstract-video-list' | |
13 | import { Video } from '../../shared/video/video.model' | |
14 | import { VideoService } from '../../shared/video/video.service' | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-account-videos', | |
18 | templateUrl: './my-account-videos.component.html', | |
19 | styleUrls: [ './my-account-videos.component.scss' ] | |
20 | }) | |
21 | export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy { | |
22 | titlePage = 'My videos' | |
23 | currentRoute = '/my-account/videos' | |
24 | checkedVideos: { [ id: number ]: boolean } = {} | |
25 | pagination: ComponentPagination = { | |
26 | currentPage: 1, | |
27 | itemsPerPage: 5, | |
28 | totalItems: null | |
29 | } | |
30 | ||
31 | protected baseVideoWidth = -1 | |
32 | protected baseVideoHeight = 155 | |
33 | ||
34 | constructor (protected router: Router, | |
35 | protected route: ActivatedRoute, | |
36 | protected authService: AuthService, | |
37 | protected notificationsService: NotificationsService, | |
38 | protected confirmService: ConfirmService, | |
39 | protected location: Location, | |
40 | private videoService: VideoService) { | |
41 | super() | |
42 | } | |
43 | ||
44 | ngOnInit () { | |
45 | super.ngOnInit() | |
46 | } | |
47 | ||
48 | ngOnDestroy () { | |
49 | super.ngOnDestroy() | |
50 | } | |
51 | ||
52 | abortSelectionMode () { | |
53 | this.checkedVideos = {} | |
54 | } | |
55 | ||
56 | isInSelectionMode () { | |
57 | return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true) | |
58 | } | |
59 | ||
60 | getVideosObservable (page: number) { | |
61 | const newPagination = immutableAssign(this.pagination, { currentPage: page }) | |
62 | ||
63 | return this.videoService.getMyVideos(newPagination, this.sort) | |
64 | } | |
65 | ||
66 | generateSyndicationList () { | |
67 | throw new Error('Method not implemented.') | |
68 | } | |
69 | ||
70 | async deleteSelectedVideos () { | |
71 | const toDeleteVideosIds = Object.keys(this.checkedVideos) | |
72 | .filter(k => this.checkedVideos[k] === true) | |
73 | .map(k => parseInt(k, 10)) | |
74 | ||
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 | ) | |
97 | } | |
98 | ||
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 | ) | |
113 | } | |
114 | ||
115 | protected buildVideoHeight () { | |
116 | // In account videos, the video height is fixed | |
117 | return this.baseVideoHeight | |
118 | } | |
119 | ||
120 | private spliceVideosById (id: number) { | |
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 | } | |
130 | } | |
131 | } |