1 import { Component, OnInit, OnDestroy } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { immutableAssign } from '@app/shared/misc/utils'
4 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
5 import { NotificationsService } from 'angular2-notifications'
6 import 'rxjs/add/observable/from'
7 import 'rxjs/add/operator/concatAll'
8 import { Observable } from 'rxjs/Observable'
9 import { AuthService } from '../../core/auth'
10 import { ConfirmService } from '../../core/confirm'
11 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
12 import { Video } from '../../shared/video/video.model'
13 import { VideoService } from '../../shared/video/video.service'
16 selector: 'my-account-videos',
17 templateUrl: './account-videos.component.html',
18 styleUrls: [ './account-videos.component.scss' ]
20 export class AccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
21 titlePage = 'My videos'
22 currentRoute = '/account/videos'
23 checkedVideos: { [ id: number ]: boolean } = {}
24 pagination: ComponentPagination = {
30 protected baseVideoWidth = -1
31 protected baseVideoHeight = 155
33 constructor (protected router: Router,
34 protected route: ActivatedRoute,
35 protected authService: AuthService,
36 protected notificationsService: NotificationsService,
37 protected confirmService: ConfirmService,
38 private videoService: VideoService) {
50 abortSelectionMode () {
51 this.checkedVideos = {}
54 isInSelectionMode () {
55 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[k] === true)
58 getVideosObservable (page: number) {
59 const newPagination = immutableAssign(this.pagination, { currentPage: page })
61 return this.videoService.getMyVideos(newPagination, this.sort)
64 async deleteSelectedVideos () {
65 const toDeleteVideosIds = Object.keys(this.checkedVideos)
66 .filter(k => this.checkedVideos[k] === true)
67 .map(k => parseInt(k, 10))
69 const res = await this.confirmService.confirm(`Do you really want to delete ${toDeleteVideosIds.length} videos?`, 'Delete')
70 if (res === false) return
72 const observables: Observable<any>[] = []
73 for (const videoId of toDeleteVideosIds) {
74 const o = this.videoService
76 .do(() => this.spliceVideosById(videoId))
81 Observable.from(observables)
85 this.notificationsService.success('Success', `${toDeleteVideosIds.length} videos deleted.`)
86 this.buildVideoPages()
89 err => this.notificationsService.error('Error', err.message)
93 async deleteVideo (video: Video) {
94 const res = await this.confirmService.confirm(`Do you really want to delete ${video.name}?`, 'Delete')
95 if (res === false) return
97 this.videoService.removeVideo(video.id)
100 this.notificationsService.success('Success', `Video ${video.name} deleted.`)
101 this.spliceVideosById(video.id)
102 this.buildVideoPages()
105 error => this.notificationsService.error('Error', error.message)
109 protected buildVideoHeight () {
110 // In account videos, the video height is fixed
111 return this.baseVideoHeight
114 private spliceVideosById (id: number) {
115 for (const key of Object.keys(this.loadedPages)) {
116 const videos = this.loadedPages[key]
117 const index = videos.findIndex(v => v.id === id)
120 videos.splice(index, 1)