1 import { from as observableFrom, Observable } from 'rxjs'
2 import { concatAll, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, Inject, LOCALE_ID, ViewChild } 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'
17 import { VideoChangeOwnershipComponent } from './video-change-ownership/video-change-ownership.component'
20 selector: 'my-account-videos',
21 templateUrl: './my-account-videos.component.html',
22 styleUrls: [ './my-account-videos.component.scss' ]
24 export class MyAccountVideosComponent extends AbstractVideoList implements OnInit, OnDestroy {
26 currentRoute = '/my-account/videos'
27 checkedVideos: { [ id: number ]: boolean } = {}
28 pagination: ComponentPagination = {
34 protected baseVideoWidth = -1
35 protected baseVideoHeight = 155
37 @ViewChild('videoChangeOwnershipModal') videoChangeOwnershipModal: VideoChangeOwnershipComponent
40 protected router: Router,
41 protected route: ActivatedRoute,
42 protected authService: AuthService,
43 protected notificationsService: NotificationsService,
44 protected location: Location,
45 protected screenService: ScreenService,
47 private confirmService: ConfirmService,
48 private videoService: VideoService,
49 @Inject(LOCALE_ID) private localeId: string
53 this.titlePage = this.i18n('My videos')
64 abortSelectionMode () {
65 this.checkedVideos = {}
68 isInSelectionMode () {
69 return Object.keys(this.checkedVideos).some((k: any) => this.checkedVideos[ k ] === true)
72 getVideosObservable (page: number) {
73 const newPagination = immutableAssign(this.pagination, { currentPage: page })
75 return this.videoService.getMyVideos(newPagination, this.sort)
78 generateSyndicationList () {
79 throw new Error('Method not implemented.')
82 async deleteSelectedVideos () {
83 const toDeleteVideosIds = Object.keys(this.checkedVideos)
84 .filter((k: any) => this.checkedVideos[ k ] === true)
85 .map(k => parseInt(k, 10))
87 const res = await this.confirmService.confirm(
88 this.i18n('Do you really want to delete {{deleteLength}} videos?', { deleteLength: toDeleteVideosIds.length }),
91 if (res === false) return
93 const observables: Observable<any>[] = []
94 for (const videoId of toDeleteVideosIds) {
95 const o = this.videoService.removeVideo(videoId)
96 .pipe(tap(() => this.spliceVideosById(videoId)))
101 observableFrom(observables)
105 this.notificationsService.success(
106 this.i18n('Success'),
107 this.i18n('{{deleteLength}} videos deleted.', { deleteLength: toDeleteVideosIds.length })
110 this.abortSelectionMode()
114 err => this.notificationsService.error(this.i18n('Error'), err.message)
118 async deleteVideo (video: Video) {
119 const res = await this.confirmService.confirm(
120 this.i18n('Do you really want to delete {{videoName}}?', { videoName: video.name }),
123 if (res === false) return
125 this.videoService.removeVideo(video.id)
128 this.notificationsService.success(
129 this.i18n('Success'),
130 this.i18n('Video {{videoName}} deleted.', { videoName: video.name })
135 error => this.notificationsService.error(this.i18n('Error'), error.message)
139 changeOwnership (event: Event, video: Video) {
140 event.preventDefault()
141 this.videoChangeOwnershipModal.show(video)
144 getStateLabel (video: Video) {
147 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
148 suffix = this.i18n('Published')
149 } else if (video.scheduledUpdate) {
150 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
151 suffix = this.i18n('Publication scheduled on ') + updateAt
152 } else if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
153 suffix = this.i18n('Waiting transcoding')
154 } else if (video.state.id === VideoState.TO_TRANSCODE) {
155 suffix = this.i18n('To transcode')
156 } else if (video.state.id === VideoState.TO_IMPORT) {
157 suffix = this.i18n('To import')
162 return ' - ' + suffix
165 protected buildVideoHeight () {
166 // In account videos, the video height is fixed
167 return this.baseVideoHeight
170 private spliceVideosById (id: number) {
172 for (key of Object.keys(this.loadedPages)) {
173 const videos = this.loadedPages[ key ]
174 const index = videos.findIndex((v: any) => v.id === id)
177 videos.splice(index, 1)