import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../api' ; import IVideoApiModel from '../..//api/video/IVideoApiModel' ; import IRecordedApiModel from '../../api/recorded/IRecordedApiModel' ; import IRecordedState , { MultipleDeletionOption , SelectedInfo } from './IRecordedState' ; import IRecordedUtil , { RecordedDisplayData } from './IRecordedUtil' ; @ injectable ( ) export default class RecordedState implements IRecordedState { private recordedApiModel : IRecordedApiModel ; private recordedUtil : IRecordedUtil ; private videoApiModel : IVideoApiModel ; private recorded : RecordedDisplayData [ ] | null = null ; private total : number = 0 ; constructor ( @ inject ( 'IRecordedApiModel' ) recordedApiModel , @ inject ( 'IRecordedUtil' ) recordedUtil , @ inject ( 'IVideoApiModel' ) videoApiModel ) { this . recordedApiModel = recordedApiModel ; this . recordedUtil = recordedUtil ; this . videoApiModel = videoApiModel ; } /**
     * 取得した録画情報をクリア
     */ public clearData ( ) { this . recorded = null ; this . total = 0 ; } param option return public async fetchData ( option ) { const recrods = await this . recordedApiModel . gets ( option ) ; this . total = recrods . total ; const oldSelectedIndex = { } ; if ( this . recorded !== null ) { for ( const r of this . recorded ) { oldSelectedIndex [ r . recordedItem . id ] = r . isSelected ; } } this . recorded = recrods . records . map ( r => { const result = this . recordedUtil . convertRecordedItemToDisplayData ( r , option . isHalfWidth ) ; if ( typeof oldSelectedIndex [ result . recordedItem . id ] !== 'undefined' ) { result . isSelected = oldSelectedIndex [ result . recordedItem . id ] ; } return result ; } ) ; } return public getRecorded ( ) { return this . recorded === null ? [ ] : this . recorded ; } return public getTotal ( ) { return this . total ; } param recordedId return public async stopEncode ( recordedId ) { await this . recordedApiModel . stopEncode ( recordedId ) ; } return public getSelectedCnt ( ) { if ( this . recorded === null ) { return { cnt : 0 , size : 0 , } ; } let selectedCnt = 0 ; let selectedSize = 0 ; for ( const r of this . recorded ) { if ( r . isSelected === true ) { selectedCnt ++ ; if ( typeof r . recordedItem . videoFiles !== 'undefined' ) { for ( const v of r . recordedItem . videoFiles ) { selectedSize += v . size ; } } } } return { cnt : selectedCnt , size : selectedSize , } ; } param recordedId public select ( recordedId ) { if ( this . recorded === null ) { return ; } for ( const r of this . recorded ) { if ( r . recordedItem . id === recordedId ) { r . isSelected = ! r . isSelected ; return ; } } } /**
     * 全て選択 (削除時の複数選択)
     */ public selectAll ( ) { if ( this . recorded === null ) { return ; } let isUnselectAll = true ; for ( const r of this . recorded ) { if ( r . isSelected === false ) { isUnselectAll = false ; } r . isSelected = true ; } if ( isUnselectAll === true ) { for ( const r of this . recorded ) { r . isSelected = false ; } } } /**
     * 全ての選択解除 (削除時の複数選択)
     */ public clearSelect ( ) { if ( this . recorded === null ) { return ; } for ( const r of this . recorded ) { r . isSelected = false ; } } param option public async multiplueDeletion ( option ) { if ( this . recorded === null ) { return ; } const videoFileIds = [ ] ; for ( const r of this . recorded ) { if ( r . isSelected === false || typeof r . recordedItem . videoFiles === 'undefined' ) { continue ; } for ( const v of r . recordedItem . videoFiles ) { if ( option === 'All' || ( option === 'OnlyOriginalFile' && v . type === 'ts' ) || ( option === 'OnlyEncodedFile' && v . type === 'encoded' ) ) { videoFileIds . push ( v . id ) ; } } } this . clearSelect ( ) ; let hasError = false ; for ( const v of videoFileIds ) { try { await this . videoApiModel . delete ( v ) ; } catch ( err ) { console . error ( err ) ; hasError = true ; } } if ( hasError === true ) { throw new Error ( ) ; } } }