/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { Component, OnInit, Input, SimpleChanges, EventEmitter, Output } from '@angular/core'; import { Router } from '@angular/router'; import { PopInService } from '../../service'; import { isNullOrUndefined } from '../../utils'; export class UploadProgress { labelMediaTitle: string = ''; mediaSendSize: number = 0; mediaSize: number = 99999999999999; result?: string; error?: string; clear() { this.labelMediaTitle = ''; this.mediaSendSize = 0; this.mediaSize = 99999999999999; this.result = undefined; this.error = undefined; } } @Component({ selector: 'upload-progress', templateUrl: './upload-progress.html', styleUrls: ['./upload-progress.less'], }) export class PopInUploadProgress implements OnInit { @Input() mediaTitle: string = ''; @Input() mediaUploaded: number = 0; @Input() mediaSize: number = 999999999999; @Input() result?: string; @Input() error?: string; @Output() abort: EventEmitter = new EventEmitter(); public closeButtonTitle?: string = 'Abort'; public otherButtonTitle?: string; public validateButtonTitle?: string; public uploadDisplay: string = ''; public sizeDisplay: string = ''; public progress: number = 0; constructor(private router: Router, private popInService: PopInService) { } OnDestroy() { } ngOnInit() { } eventPopUp(_event: string): void { if (_event == "close") { if (this.abort) { this.abort.emit(); } } else { console.log(`GET event: ${_event}`); this.popInService.close('popin-upload-progress'); } } updateNeedSend(): void { } limit3(count: number): string { if (count >= 1000) { return `${count}`; } if (count >= 100) { return ` ${count}`; } if (count >= 10) { return ` ${count}`; } return ` ${count}`; } convertInHuman(countIn: number): string { let count = countIn; let tera = Math.trunc(count / (1024 * 1024 * 1024 * 1024)); count = count - tera * 1024 * 1024 * 1024 * 1024; let giga = Math.trunc(count / (1024 * 1024 * 1024)); count = count - giga * 1024 * 1024 * 1024; let mega = Math.trunc(count / (1024 * 1024)); count = count - mega * 1024 * 1024; let kilo = Math.trunc(count / 1024); count = count - kilo * 1024; let out = ''; if (out.length !== 0 || tera !== 0) { out = `${out} ${this.limit3(tera)}T`; } if (out.length !== 0 || giga !== 0) { out = `${out} ${this.limit3(giga)}G`; } if (out.length !== 0 || mega !== 0) { out = `${out} ${this.limit3(mega)}M`; } if (out.length !== 0 || kilo !== 0) { out = `${out} ${this.limit3(kilo)}k`; } if (out.length !== 0 || count !== 0) { out = `${out} ${this.limit3(count)}B`; } return out; } ngOnChanges(changes: SimpleChanges) { //console.log(`Upload progress event : ${JSON.stringify(changes)}`); this.progress = Math.trunc((this.mediaUploaded * 100) / this.mediaSize); this.uploadDisplay = this.convertInHuman(this.mediaUploaded); this.sizeDisplay = this.convertInHuman(this.mediaSize); if (isNullOrUndefined(this.error) && isNullOrUndefined(this.result)) { this.closeButtonTitle = 'Abort'; this.otherButtonTitle = undefined; this.validateButtonTitle = undefined; } else if (isNullOrUndefined(this.result)) { this.closeButtonTitle = undefined; this.otherButtonTitle = 'Close'; this.validateButtonTitle = undefined; } else { this.closeButtonTitle = undefined; this.otherButtonTitle = undefined; this.validateButtonTitle = 'Ok'; } } }