diff options
Diffstat (limited to 'frontend/src/app/_elements/form-dataset')
4 files changed, 284 insertions, 0 deletions
diff --git a/frontend/src/app/_elements/form-dataset/form-dataset.component.css b/frontend/src/app/_elements/form-dataset/form-dataset.component.css new file mode 100644 index 00000000..da31cfcb --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.css @@ -0,0 +1,69 @@ +.folderBox { + width: 100%; + height: 100%; + position: relative; +} + +.file-container { + border: 4px solid transparent; + position: relative; + margin-left: 3%; + margin-top: 3rem; + width: 94%; + min-height: 300px; + height: 75%; +} + +.fileButton { + position: absolute; + margin-top: -3rem; + display: flex; + flex-direction: row; + align-items: center; +} + +.fileButton label { + margin-left: 10px; +} + +.dottedClass { + border: 4px dotted white; + border-radius: 25px; +} + +.hidden { + visibility: hidden; +} + +.file { + position: absolute; + width: 100%; + height: 100%; + opacity: 0; +} + +.file input { + border-radius: 4px; + margin-top: -15px; + width: 100%; + height: 100%; +} + +.icon-display { + position: absolute; + top: 45%; + left: 50%; + transform: translate(-50%, -50%) scale(4); +} + +.bottomBar { + width: 50%; + margin: 1rem; + align-items: flex-start; +} + +#bottomButton { + background-color: var(--ns-bg-dark-100); + width: 10%; + height: 65%; +}
\ No newline at end of file diff --git a/frontend/src/app/_elements/form-dataset/form-dataset.component.html b/frontend/src/app/_elements/form-dataset/form-dataset.component.html new file mode 100644 index 00000000..2176b130 --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.html @@ -0,0 +1,74 @@ +<div class="folderBox"> + + <div class="file-container" [ngClass]="{'dottedClass': !tableData.hasInput}"> + + <i class="material-icons-outlined icon-display" [ngClass]="{'hidden': tableData.hasInput}">file_upload</i> + + <div class="fileButton"> + <button type="button" mat-raised-button (click)="fileInput.click()">Choose File</button> + <label>{{filename}}</label> + </div> + + <input class="file" id="file-upload" (change)="changeListener($event)" #fileInput type="file" accept=".csv"> + + + <div class="mt-5 datatable"> + <app-datatable [tableData]="tableData"></app-datatable> + </div> + + + </div> + + + + + + <div class="bottomBar"> + <div class="row"> + <div class="col-sm"> + <div role="group"> + <div class="row"> + <mat-form-field class="example-full-width" appearance="fill"> + <mat-label>Naziv</mat-label> + <input type="text" matInput value="{{dataset?.name}}"> + <!--[formControl]="nameFormControl"--> + + <mat-error *ngIf="nameFormControl.hasError('required')"> + Naziv je <strong>obavezan</strong> + </mat-error> + </mat-form-field> + </div> + </div> + </div> + <div class="col-sm mb-3"> + + <!--<input id="fileInput" class="form-control btn-lg" type="file" class="upload" (change)="changeListener($event)" accept=".csv"> + --> + </div> + <div class="col-sm"> + + + + <mat-form-field appearance="fill"> + <mat-label>Delimiter</mat-label> + <mat-select id="delimiterOptions" [(ngModel)]="dataset.delimiter" (change)="update()" value=","> + <mat-option *ngFor="let option of delimiterOptions" [value]="option"> + {{ option }} + </mat-option> + </mat-select> + </mat-form-field> + </div> + </div> + </div> + + <div class="btn-group" role="group" aria-label="Button group with nested dropdown"> + + </div> + + + <!-- + <div class="d-flex flex-row align-items-center justify-content-center w-100 my-2"> + <button (click)="uploadDataset()" class="btn btn-lg col-4" style="background-color:#003459; color:white;">Dodaj izvor podataka</button> + </div> +--> +</div>
\ No newline at end of file diff --git a/frontend/src/app/_elements/form-dataset/form-dataset.component.spec.ts b/frontend/src/app/_elements/form-dataset/form-dataset.component.spec.ts new file mode 100644 index 00000000..51491c58 --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.spec.ts @@ -0,0 +1,25 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { FormDatasetComponent } from './form-dataset.component'; + +describe('FormDatasetComponent', () => { + let component: FormDatasetComponent; + let fixture: ComponentFixture<FormDatasetComponent>; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ FormDatasetComponent ] + }) + .compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(FormDatasetComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/_elements/form-dataset/form-dataset.component.ts b/frontend/src/app/_elements/form-dataset/form-dataset.component.ts new file mode 100644 index 00000000..63376524 --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.ts @@ -0,0 +1,116 @@ +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; + + @Input() dataset: Dataset; //dodaj ! potencijalno + + 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? + + 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.update(); + } + + update() { + + if (this.files.length < 1) + 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) + + if (this.dataset.hasHeader) + this.csvRecords = result.splice(0, 11); + else + this.csvRecords = result.splice(0, 10); + + this.colsNumber = result[0].length; + this.rowsNumber = result.length; + + this.tableData.data = this.csvRecords + this.tableData.hasHeader = this.dataset.hasHeader; + 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); + } + + checkAccessible() { + if (this.dataset.isPublic) + this.dataset.accessibleByLink = true; + } + + uploadDataset() { + if (this.files[0] == undefined) { + shared.openDialog("Greška", "Niste izabrali fajl za učitavanje."); + return; + } + + this.modelsService.uploadData(this.files[0]).subscribe((file) => { + //console.log('ADD MODEL: STEP 2 - ADD DATASET WITH FILE ID ' + file._id); + this.dataset.fileId = file._id; + this.dataset.uploaderId = shared.userId; + + this.datasetsService.addDataset(this.dataset).subscribe((dataset) => { + shared.openDialog("Obaveštenje", "Uspešno ste dodali novi izvor podataka u kolekciju. Molimo sačekajte par trenutaka da se procesira."); + }, (error) => { + shared.openDialog("Neuspeo pokušaj!", "Izvor podataka sa unetim nazivom već postoji u Vašoj kolekciji. Izmenite naziv ili iskoristite postojeći dataset."); + }); //kraj addDataset subscribe + }, (error) => { + + }); //kraj uploadData subscribe + } + + + +} |