1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, Inject, LOCALE_ID } 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 { VideoPrivacy, VideoState } from '../../../../../shared/models/videos'
16 import { ScreenService } from '@app/shared/misc/screen.service'
19 selector: 'my-account-videos',
20 templateUrl: './my-account-videos.component.html',
21 styleUrls: [ './my-account-videos.component.scss' ]
23 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
25 currentRoute = '/my-account/videos'
26 checkedVideos: { [ id: number ]: boolean } = {}
27 pagination: ComponentPagination = {
33 protected baseVideoWidth = -1
34 protected baseVideoHeight = 155
37 protected router: Router,
38 protected route: ActivatedRoute,
39 protected authService: AuthService,
40 protected notificationsService: NotificationsService,
41 protected confirmService: ConfirmService,
42 protected location: Location,
43 protected screenService: ScreenService,
45 private videoService: VideoService,
46 @Inject(LOCALE_ID) private localeId: string
50 this.titlePage = this.i18n('My videos')
61 abortSelectionMode () {
62 this.checkedVideos = {}
65 isInSelectionMode () {
66 return Object.keys(this.checkedVideos).some(k => this.checkedVideos[ k ] === true)
69 getVideosObservable (page: number) {
70 const newPagination = immutableAssign(this.pagination, { currentPage: page })
72 return this.videoService.getMyVideos(newPagination, this.sort)
75 generateSyndicationList () {
76 throw new Error('Method not implemented.')
79 async deleteSelectedVideos () {
80 const toDeleteVideosIds = Object.keys(this.checkedVideos)
81 .filter(k => this.checkedVideos[ k ] === true)
82 .map(k => parseInt(k, 10))
84 const res = await this.confirmService.confirm(
85 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
88 if (res === false) return
90 const observables: Observable<any>[] = []
91 for (const videoId of toDeleteVideosIds) {
92 const o = this.videoService.removeVideo(videoId)
93 .pipe(tap(() => this.spliceVideosById(videoId)))
98 observableFrom(observables)
102 this.notificationsService.success(
103 this.i18n('Success'),
104 this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })
107 this.abortSelectionMode()
111 err => this.notificationsService.error(this.i18n('Error'), err.message)
115 async deleteVideo (video: Video) {
116 const res = await this.confirmService.confirm(
117 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
120 if (res === false) return
122 this.videoService.removeVideo(video.id)
125 this.notificationsService.success(
126 this.i18n('Success'),
127 this.i18n('Video {{videoName}} deleted.', { videoName: video.name })
132 error => this.notificationsService.error(this.i18n('Error'), error.message)
136 getStateLabel (video: Video) {
139 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
140 suffix = this.i18n('Published')
141 } else if (video.scheduledUpdate) {
142 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
143 suffix = this.i18n('Publication scheduled on ') + updateAt
144 } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
145 suffix = this.i18n('Waiting transcoding')
146 } else if (video.state.id === VideoState.TO_TRANSCODE) {
147 suffix = this.i18n('To transcode')
152 return ' - ' + suffix
155 protected buildVideoHeight () {
156 // In account videos, the video height is fixed
157 return this.baseVideoHeight
160 private spliceVideosById (id: number) {
161 for (const key of Object.keys(this.loadedPages)) {
162 const videos = this.loadedPages[ key ]
163 const index = videos.findIndex(v => v.id === id)
166 videos.splice(index, 1)