aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/add-new-dataset/add-new-dataset.component.ts
blob: fceb53cf87d31b44e49ba28109215538a3228c51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Component, EventEmitter, Output, ViewChild } from '@angular/core';
import { NgxCsvParser, NgxCSVParserError } from 'ngx-csv-parser';
import Dataset from 'src/app/_data/Dataset';

@Component({
  selector: 'app-add-new-dataset',
  templateUrl: './add-new-dataset.component.html',
  styleUrls: ['./add-new-dataset.component.css']
})
export class AddNewDatasetComponent {

  @Output() loaded = new EventEmitter<string>();

  delimiterOptions: Array<string> = [",", ";", "\t", "razmak", "|"]; //podrazumevano ","

  //hasHeader: boolean = true;
  hasInput: boolean = false;

  csvRecords: any[] = [];
  files: File[] = [];
  rowsNumber: number = 0;
  colsNumber: number = 0;

  dataset: Dataset; //dodaj ! potencijalno

  constructor(private ngxCsvParser: NgxCsvParser) {
    this.dataset = new Dataset();
  }

  //@ViewChild('fileImportInput', { static: false }) fileImportInput: any; cemu je ovo sluzilo?

  changeListener($event: any): void {
    this.files = $event.srcElement.files;
    if (this.files.length == 0 || this.files[0] == null) {
      //console.log("NEMA FAJLA");
      //this.loaded.emit("not loaded");
      this.hasInput = false;
      return;
    }
    else
      this.hasInput = true;

    this.update();
  }

  update() {

    if (this.files.length < 1)
      return;

    this.ngxCsvParser.parse(this.files[0], { header: false, delimiter: (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "") ? "," : this.dataset.delimiter })
      .pipe().subscribe((result) => {

        console.log('Result', result);
        if (result.constructor === Array) {
          this.csvRecords = result;
          if (this.dataset.hasHeader)
            this.rowsNumber = this.csvRecords.length - 1;
          else
            this.rowsNumber = this.csvRecords.length;
          this.colsNumber = this.csvRecords[0].length;

          if (this.dataset.hasHeader) //kasnije dodati opciju kada nema header da korisnik rucno unosi header-e
            this.dataset.header = this.csvRecords[0];

          this.loaded.emit("loaded");
        }
      }, (error: NgxCSVParserError) => {
        console.log('Error', error);
      });
  }

  checkAccessible() {
    if (this.dataset.isPublic)
      this.dataset.accessibleByLink = true;
  }

}