aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/form-dataset/form-dataset.component.ts
blob: 8714ae7f7b9d63cb53a5f0d1b83c0aaa14331c61 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Component, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import Dataset from 'src/app/_data/Dataset';
import { DatasetsService } from 'src/app/_services/datasets.service';
import { ModelsService } from 'src/app/_services/models.service';
import shared from 'src/app/Shared';
import { DatatableComponent, TableData } from '../datatable/datatable.component';
import { CsvParseService } from 'src/app/_services/csv-parse.service';
import { FormControl, Validators } from '@angular/forms';

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

  @ViewChild(DatatableComponent) datatable!: DatatableComponent;

  nameFormControl = new FormControl('', [Validators.required, Validators.email]);

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

  csvRecords: any[] = [];
  files: File[] = [];
  rowsNumber: number = 0;
  colsNumber: number = 0;
  begin: number = 0;
  step: number = 10;
  existingFlag: boolean = false;

  @Input() dataset: Dataset; //dodaj ! potencijalno
  @Output() editEvent = new EventEmitter();

  tableData: TableData = new TableData();

  @ViewChild('fileInput') fileInput!: ElementRef

  filename: String;

  constructor(private modelsService: ModelsService, private datasetsService: DatasetsService, private csv: CsvParseService) {
    this.dataset = new Dataset();
    this.dataset.delimiter = ',';
    this.filename = "";
  }

  //@ViewChild('fileImportInput', { static: false }) fileImportInput: any; cemu je ovo sluzilo?
  resetPagging() {
    this.begin = 0;
  }
  goBack() {
    if (this.begin - 10 < 0)
      this.begin = 0;
    else {
      this.begin -= 10;
      this.loadExisting();
    }

  }
  goForward() {
    this.begin += 10;
    if (this.dataset.rowCount < this.begin)
      this.begin -= 10;
    else
      this.loadExisting();
  }
  clear() {
    this.tableData.hasInput = false;
  }
  getPage() {
    return Math.ceil(this.dataset.rowCount / 10)
  }

  changeListener($event: any): void {
    this.files = $event.srcElement.files;
    if (this.files.length == 0 || this.files[0] == null) {
      this.tableData.hasInput = false;
      return;
    }
    else
      this.tableData.hasInput = true;

    this.filename = this.files[0].name;
    this.tableData.loaded = false;
    this.existingFlag = false;
    this.update();
  }

  firstInput = false;

  update() {
    this.firstInput = true;
    if (this.files.length < 1) {
      this.loadExisting();
      return;
    }


    const fileReader = new FileReader();
    fileReader.onload = (e) => {
      if (typeof fileReader.result === 'string') {
        const result = this.csv.csvToArray(fileReader.result, (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "novi red") ? "\t" : this.dataset.delimiter)


        this.csvRecords = result.splice(0, 11);

        this.colsNumber = result[0].length;
        this.rowsNumber = result.length;

        this.tableData.data = this.csvRecords;
        this.tableData.loaded = true;
        this.tableData.numCols = this.colsNumber;
        this.tableData.numRows = this.rowsNumber;
      }
    }
    fileReader.readAsText(this.files[0]);

    this.dataset.name = this.filename.slice(0, this.filename.length - 4);
  }

  loadExisting() {
    this.existingFlag = true;
    this.firstInput = false;

    this.tableData.hasInput = true;
    this.tableData.loaded = false;
    this.datasetsService.getDatasetHeader(this.dataset.fileId).subscribe((header: string | undefined) => {

      this.datasetsService.getDatasetFilePaging(this.dataset.fileId, this.begin, this.step).subscribe((file: string | undefined) => {
        if (file) {
          this.tableData.loaded = true;
          this.tableData.numRows = this.dataset.rowCount;
          this.tableData.numCols = this.dataset.columnInfo.length;
          this.tableData.data = this.csv.csvToArray(header + '\n' + file, (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "novi red") ? "\t" : this.dataset.delimiter);

        }
        else {
          this.begin -= 10;
          this.loadExisting();
        }
      });
    });


  }

  /*exportAsXLSX():void {
    this.excelService.exportAsExcelFile(this.data, 'sample');
  }*/

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

  uploadDataset(onSuccess: Function = (dataset: Dataset) => { }, onError: Function = () => { }) {
    if (this.files[0] == undefined) {
      shared.openDialog("Greška", "Niste izabrali fajl za učitavanje.");
      return;
    }

    return this.datasetsService.uploadData(this.files[0]).subscribe((file) => {
      this.dataset._id = "";
      this.dataset.fileId = file._id;
      this.dataset.uploaderId = shared.userId;

      this.datasetsService.addDataset(this.dataset).subscribe((dataset) => {
        onSuccess();
      }, (error) => {
        onError();
      }); //kraj addDataset subscribe
    }, (error) => {
      onError();
    }); //kraj uploadData subscribe
  }



}