Blame view
src/app/component/import-vessels/import-vessels.component.ts
2.51 KB
632c4ac2f import vessels |
1 2 |
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {VesselsService} from '../../service/vessels.service'; |
b02c6cc82 rename vessel to ... |
3 |
import {Message} from '../../model/message'; |
de59ffef8 add model + search |
4 |
import {Vessels} from '../../model/vessels'; |
632c4ac2f import vessels |
5 6 7 8 9 10 11 |
@Component({ selector: 'app-import-vessels', templateUrl: './import-vessels.component.html', styleUrls: ['./import-vessels.component.scss'] }) export class ImportVesselsComponent implements OnInit { |
de59ffef8 add model + search |
12 |
vessels: Vessels; |
632c4ac2f import vessels |
13 14 15 16 17 18 19 20 21 22 |
constructor(private vesselsService: VesselsService) { } @ViewChild('fileDropRef', {static: false}) fileDropEl: ElementRef; files: any[] = []; ngOnInit(): void { this.vesselsService.currentVessels.subscribe(vessels => this.vessels = vessels); } |
632c4ac2f import vessels |
23 24 25 |
onFileDropped($event): void { this.prepareFilesList($event); } |
632c4ac2f import vessels |
26 27 28 |
fileBrowseHandler(files): void { this.prepareFilesList(files); } |
632c4ac2f import vessels |
29 30 31 32 |
deleteFile(index: number): void { if (this.files[index].progress < 100) { return; } |
c4e00deb9 add map |
33 34 |
this.vessels = new Vessels(); this.vesselsService.changeVesselsSet(this.vessels); |
632c4ac2f import vessels |
35 36 |
this.files.splice(index, 1); } |
c4e00deb9 add map |
37 |
uploadFiles(index: number): void { |
632c4ac2f import vessels |
38 39 |
const fileReader = new FileReader(); let nbLine: number; |
287033a62 add vessel positi... |
40 |
fileReader.onload = () => { |
de59ffef8 add model + search |
41 |
this.vessels = new Vessels(); |
632c4ac2f import vessels |
42 43 44 |
const lines: string[] = (fileReader.result as string).split(' '); nbLine = lines.length; |
de59ffef8 add model + search |
45 46 |
for (let line of lines) { line = line.replace(/[^\x20-\x7F]/g, ''); |
632c4ac2f import vessels |
47 |
const splitLine = line.split(','); |
de59ffef8 add model + search |
48 49 50 51 52 |
if (isNaN(Number(splitLine[0])) || line === '') { continue; } const newMessage = new Message(splitLine); this.vessels.addMessage(newMessage); |
632c4ac2f import vessels |
53 |
} |
c4e931730 add vessel list |
54 |
this.vesselsService.changeVesselsSet(this.vessels); |
632c4ac2f import vessels |
55 |
}; |
c4e00deb9 add map |
56 |
fileReader.onprogress = (e) => { |
632c4ac2f import vessels |
57 58 59 60 61 62 63 64 65 |
if (e.lengthComputable) { this.files[index].progress = Math.round(((e.loaded / e.total) * 100)); } }; fileReader.readAsText(this.files[index]); } prepareFilesList(files: Array<any>): void { |
c4e00deb9 add map |
66 |
this.files = []; |
632c4ac2f import vessels |
67 68 69 70 71 |
for (const item of files) { item.progress = 0; this.files.push(item); } this.fileDropEl.nativeElement.value = ''; |
c4e00deb9 add map |
72 |
this.uploadFiles(0); |
632c4ac2f import vessels |
73 74 75 76 77 78 79 80 81 82 83 84 85 |
} formatBytes(bytes, decimals = 2): string { if (bytes === 0) { return '0 Bytes'; } const k = 1024; const dm = decimals <= 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } } |