1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { Location } from '@angular/common'
6 import { immutableAssign } from '@app/shared/misc/utils'
7 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
8 import { NotificationsService } from 'angular2-notifications'
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'
14 import { I18n } from '@ngx-translate/i18n-polyfill'
15 import { VideoState } from '../../../../../shared/models/videos'
18 selector: 'my-account-videos',
19 templateUrl: './my-account-videos.component.html',
20 styleUrls: [ './my-account-videos.component.scss' ]
22 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
24 currentRoute = '/my-account/videos'
25 checkedVideos: { [ id: number ]: boolean } = {}
26 pagination: ComponentPagination = {
32 protected baseVideoWidth = -1
33 protected baseVideoHeight = 155
36 protected router: Router,
37 protected route: ActivatedRoute,
38 protected authService: AuthService,
39 protected notificationsService: NotificationsService,
40 protected confirmService: ConfirmService,
41 protected location: Location,
43 private videoService: VideoService
47 this.titlePage = this.i18n('My videos')
58 abortSelectionMode () {
59 this.checkedVideos = {}
62 isInSelectionMode () {
63 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
66 getVideosObservable (page: number) {
67 const newPagination = immutableAssign(this.pagination, { currentPage: page })
69 return this.videoService.getMyVideos(newPagination, this.sort)
72 generateSyndicationList () {
73 throw new Error('Method not implemented.')
76 async deleteSelectedVideos () {
77 const toDeleteVideosIds = Object.keys(this.checkedVideos)
78 .filter(k => this.checkedVideos[ k ] === true)
79 .map(k => parseInt(k, 10))
81 const res = await this.confirmService.confirm(
82 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
85 if (res === false) return
87 const observables: Observable<any>[] = []
88 for (const videoId of toDeleteVideosIds) {
89 const o = this.videoService.removeVideo(videoId)
90 .pipe(tap(() => this.spliceVideosById(videoId)))
95 observableFrom(observables)
99 this.notificationsService.success(
100 this.i18n('Success'),
101 this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })
104 this.abortSelectionMode()
108 err => this.notificationsService.error(this.i18n('Error'), err.message)
112 async deleteVideo (video: Video) {
113 const res = await this.confirmService.confirm(
114 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
117 if (res === false) return
119 this.videoService.removeVideo(video.id)
122 this.notificationsService.success(
123 this.i18n('Success'),
124 this.i18n('Video {{videoName}} deleted.', { videoName: video.name })
129 error => this.notificationsService.error(this.i18n('Error'), error.message)
133 getStateLabel (video: Video) {
134 if (video.state.id === VideoState.PUBLISHED) return this.i18n('Published')
136 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) return this.i18n('Waiting transcoding')
137 if (video.state.id === VideoState.TO_TRANSCODE) return this.i18n('To transcode')
139 return this.i18n('Unknown state')
142 protected buildVideoHeight () {
143 // In account videos, the video height is fixed
144 return this.baseVideoHeight
147 private spliceVideosById (id: number) {
148 for (const key of Object.keys(this.loadedPages)) {
149 const videos = this.loadedPages[ key ]
150 const index = videos.findIndex(v => v.id === id)
153 videos.splice(index, 1)