Blame view

src/app/component/import-vessels/import-vessels.component.ts 2.67 KB
632c4ac2f   lsagona   import vessels
1
2
  import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
  import {VesselsService} from '../../service/vessels.service';
b02c6cc82   lsagona   rename vessel to ...
3
  import {Message} from '../../model/message';
de59ffef8   lsagona   add model + search
4
  import {Vessels} from '../../model/vessels';
632c4ac2f   lsagona   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   lsagona   add model + search
12
    vessels: Vessels;
632c4ac2f   lsagona   import vessels
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
    constructor(private vesselsService: VesselsService) {
    }
  
    @ViewChild('fileDropRef', {static: false}) fileDropEl: ElementRef;
    files: any[] = [];
  
    ngOnInit(): void {
      this.vesselsService.currentVessels.subscribe(vessels => this.vessels = vessels);
    }
  
    /**
     * on file drop handler
     */
    onFileDropped($event): void {
      this.prepareFilesList($event);
    }
  
    /**
     * handle file from browsing
     */
    fileBrowseHandler(files): void {
      this.prepareFilesList(files);
    }
  
    /**
     * Delete file from files list
     * @param index (File index)
     */
    deleteFile(index: number): void {
      if (this.files[index].progress < 100) {
        return;
      }
c4e00deb9   lsagona   add map
46
47
      this.vessels = new Vessels();
      this.vesselsService.changeVesselsSet(this.vessels);
632c4ac2f   lsagona   import vessels
48
49
      this.files.splice(index, 1);
    }
c4e00deb9   lsagona   add map
50
    uploadFiles(index: number): void {
632c4ac2f   lsagona   import vessels
51
52
      const fileReader = new FileReader();
      let nbLine: number;
632c4ac2f   lsagona   import vessels
53
      fileReader.onload = (e) => {
de59ffef8   lsagona   add model + search
54
        this.vessels = new Vessels();
632c4ac2f   lsagona   import vessels
55
56
57
        const lines: string[] = (fileReader.result as string).split('
  ');
        nbLine = lines.length;
de59ffef8   lsagona   add model + search
58
59
        for (let line of lines) {
          line = line.replace(/[^\x20-\x7F]/g, '');
632c4ac2f   lsagona   import vessels
60
          const splitLine = line.split(',');
de59ffef8   lsagona   add model + search
61
62
63
64
65
          if (isNaN(Number(splitLine[0])) || line === '') {
            continue;
          }
          const newMessage = new Message(splitLine);
          this.vessels.addMessage(newMessage);
632c4ac2f   lsagona   import vessels
66
        }
c4e931730   lsagona   add vessel list
67
        this.vesselsService.changeVesselsSet(this.vessels);
632c4ac2f   lsagona   import vessels
68
      };
c4e00deb9   lsagona   add map
69
      fileReader.onprogress = (e) => {
632c4ac2f   lsagona   import vessels
70
71
72
73
74
75
76
77
78
        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   lsagona   add map
79
      this.files = [];
632c4ac2f   lsagona   import vessels
80
81
82
83
84
      for (const item of files) {
        item.progress = 0;
        this.files.push(item);
      }
      this.fileDropEl.nativeElement.value = '';
c4e00deb9   lsagona   add map
85
      this.uploadFiles(0);
632c4ac2f   lsagona   import vessels
86
87
88
89
90
91
92
93
94
95
96
97
98
    }
  
    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];
    }
  }