diff options
Diffstat (limited to 'frontend/src/app/_elements/form-dataset')
4 files changed, 299 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..953daa0c --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.css @@ -0,0 +1,64 @@ +.folderBox { + width: 100%; + height: 100%; + position: relative; +} + +.topBar { + width: 100%; + margin: 1rem; + align-items: flex-start; +} + +.topBar label{ + font-size: 30px; +} +.topBar mat-form-field{ + width: 250px; +} + +.toptop{ + margin-left: 1.5%; + width: 50%; +} + +.fileButton{ + margin-top: 10px; +} + +.file-container { + border: 4px solid transparent; + position: relative; + margin-left: 3%; + width: 94%; + min-height: 400px; + height: 95%; +} + +.dottedClass { + border: 4px dotted white; + border-radius: 25px; +} + +.icon-display { + position: absolute; + top: 45%; + left: 50%; + transform: translate(-50%, -50%) scale(4); +} + +.hidden { + visibility: hidden; +} + +.file { + position: absolute; + width: 100%; + height: 100%; + opacity: 0; +} + +.file-container input{ + border-radius: 5px; + left: 0%; +}
\ 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..281f9c05 --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.html @@ -0,0 +1,65 @@ +<div class="folderBox" *ngIf="dataset"> + + <div class="row" style="margin-right: 0;"> + <div class="topBar"> + <div class="row toptop"> + <div class="col-sm mb-3"> + <div class="fileButton"> + <button type="button" mat-raised-button (click)="fileInput.click()">Dodaj izvor podataka</button> + + </div> + </div> + + <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}}" [(ngModel)]="dataset.name"> + + + <mat-error *ngIf="nameFormControl.hasError('required')"> + Naziv je <strong>obavezan</strong> + </mat-error> + </mat-form-field> + </div> + </div> + </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 class="col-sm"> + + </div> + </div> + <div class="row" *ngIf="firstInput"> + <label class=" mt-5">{{filename}}</label> + + </div> + </div> + </div> + + + <div class="row" style="margin-right: 0;"> + <div class="file-container" [ngClass]="{'dottedClass': !tableData.hasInput}"> + + <i class="material-icons-outlined icon-display" [ngClass]="{'hidden': tableData.hasInput}">file_upload</i> + + <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> + +</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..1eed2cdc --- /dev/null +++ b/frontend/src/app/_elements/form-dataset/form-dataset.component.ts @@ -0,0 +1,145 @@ +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? + + clear(){ + this.tableData.hasInput = false; + } + + 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(); + } + + firstInput = false; + + update() { + + this.firstInput = true + + 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) + + + 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.firstInput = false; + + this.tableData.hasInput = true; + this.tableData.loaded = false; + + this.datasetsService.getDatasetFile(this.dataset.fileId).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(file, (this.dataset.delimiter == "razmak") ? " " : (this.dataset.delimiter == "") ? "," : this.dataset.delimiter); + + } + }); + + + } + + /*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.modelsService.uploadData(this.files[0]).subscribe((file) => { + //console.log('ADD MODEL: STEP 2 - ADD DATASET WITH FILE ID ' + file._id); + 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 + } + + + +} |