argos/dmp-frontend/src/app/form/dynamic-form.component.ts

146 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-12-22 14:42:47 +01:00
import { DatasetWizardModel } from '../models/datasets/DatasetWizardModel';
import { DatasetWizardComponent } from '../dataset-wizard/dataset-wizard.component';
import { DatasetProfileDefinitionModel } from '../models/DatasetProfileDefinitionModel';
import { VisibilityRulesService } from '../visibility-rules/visibility-rules.service';
2017-11-23 12:26:22 +01:00
import { Rule } from '../models/Rule';
import { Section } from '../models/Section';
2017-11-23 12:26:22 +01:00
import { JsonSerializer } from '../utilities/JsonSerializer';
2017-12-06 09:46:05 +01:00
import { Component, Input, OnInit, AfterViewChecked, ViewChild, forwardRef, ViewEncapsulation } from '@angular/core';
2017-11-10 10:53:01 +01:00
import { FormGroup, Validators, ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { NgForm } from '@angular/forms';
2017-10-30 15:56:50 +01:00
import { Router, ActivatedRoute, ParamMap, Params } from '@angular/router';
import 'rxjs/add/operator/switchMap';
2017-12-05 17:56:21 +01:00
import { Location } from '@angular/common';
2018-01-25 09:14:43 +01:00
import { MatSidenavModule } from '@angular/material/sidenav';
2018-01-12 10:00:26 +01:00
import { BaseHttpService } from '../utilities/cite-http-service-module/base-http.service';
import { DatasetWizardService } from '../services/dataset-wizard/dataset-wizard.service';
2017-11-20 11:29:42 +01:00
2017-12-05 17:56:21 +01:00
declare function simple_notifier(type: string, title: string, message: string): any;
2017-11-10 12:38:18 +01:00
@Component({
selector: 'dynamic-form',
templateUrl: './dynamic-form.component.html',
2017-11-10 12:38:18 +01:00
styleUrls: [
2018-01-18 10:10:16 +01:00
'./dynamic-form.component.scss'
2017-11-10 12:38:18 +01:00
],
2017-11-10 11:24:20 +01:00
providers: [
2017-12-06 09:46:05 +01:00
],
encapsulation: ViewEncapsulation.None,
})
export class DynamicFormComponent implements OnInit {
2017-12-22 14:42:47 +01:00
@Input() dataModel: DatasetWizardModel = new DatasetWizardModel();
2017-12-06 13:16:31 +01:00
@Input() path: string;
2017-10-04 11:39:45 +02:00
form: FormGroup;
2017-11-03 18:14:32 +01:00
id: string;
2018-01-17 18:30:09 +01:00
// @Input() datasetId: string;
2017-12-05 17:56:21 +01:00
pathName: string;
2017-12-11 15:21:49 +01:00
pages: Array<number>;
2017-12-06 09:46:05 +01:00
activeStepperIndex: number = 1;
visibleSidebar: boolean = false;
2017-12-22 15:30:15 +01:00
datasetProfileDefinitionModel: DatasetProfileDefinitionModel
2017-12-06 13:16:31 +01:00
private progressbar: boolean = false;
2017-12-11 15:21:49 +01:00
private currentPageIndex: number = 0;
2017-12-06 13:16:31 +01:00
private fragment: string;
2017-12-19 18:34:00 +01:00
constructor(private router: Router,
private _location: Location,
private route: ActivatedRoute,
private visibilityRulesService: VisibilityRulesService,
private http: BaseHttpService,
private datasetWizardService: DatasetWizardService,
2017-12-06 17:45:54 +01:00
) {
2018-01-17 18:30:09 +01:00
//this.datasetId = route.snapshot.params['id'];
2017-12-06 13:16:31 +01:00
}
2017-10-04 11:39:45 +02:00
getSubForm(subformName) {
2017-09-29 18:16:37 +02:00
return this.form.controls[subformName];
}
2017-10-04 11:39:45 +02:00
ngOnInit() {
2018-01-25 09:14:43 +01:00
this.datasetProfileDefinitionModel = this.dataModel.datasetProfileDefinition
this.createPagination();
this.form = this.datasetProfileDefinitionModel.buildForm();
2017-12-06 13:16:31 +01:00
this.visibilityRulesService.formGroup = this.form;
2018-01-25 09:14:43 +01:00
let rules: Rule[] = JsonSerializer.fromJSONArray(this.datasetProfileDefinitionModel.rules, Rule);
this.visibilityRulesService.buildVisibilityRules(rules)
this.progressbar = true;
this.route.fragment.subscribe((fragment: string) => {
var self = this;
setTimeout(function () { self.scrollTo(fragment) });
});
this.route.queryParams.subscribe((params) => {
if (params && "page" in params)
this.changeCurrentPage(params["page"]);
});
}
submit() {
this.datasetWizardService.saveDataset(this.dataModel, this.form.value).subscribe(data => {
this.router.navigateByUrl("/datasets/" + this.dataModel.dmp.id);
2018-01-25 09:14:43 +01:00
});
}
2018-01-25 09:14:43 +01:00
save() {
this.dataModel.status = "0";
this.submit();
2017-12-05 17:56:21 +01:00
}
saveFinalize() {
2018-01-25 09:14:43 +01:00
this.dataModel.status = "1";
this.submit();
2018-01-08 12:50:43 +01:00
}
2017-12-13 13:01:43 +01:00
2017-12-06 09:46:05 +01:00
toggleSidebar() {
this.visibleSidebar = !this.visibleSidebar;
}
2017-12-06 13:16:31 +01:00
2018-01-19 14:56:27 +01:00
// getPages(model: DatasetProfileDefinitionModel): Array<number> {
// let pageSet = new Set<number>();
2017-12-11 15:21:49 +01:00
2018-01-19 14:56:27 +01:00
// model.sections.forEach(section => {
// pageSet.add(section.page);
// });
2017-12-11 15:21:49 +01:00
2018-01-19 14:56:27 +01:00
// return Array.from(pageSet).sort((a, b) => a - b);
// }
2017-12-06 13:16:31 +01:00
shouldDisplaySection(section: Section): Boolean {
2017-12-13 13:01:43 +01:00
return (section.page) == this.currentPageIndex;
2017-12-11 10:20:27 +01:00
}
2017-12-11 11:36:29 +01:00
2017-12-11 10:20:27 +01:00
createPagination() {
2017-12-22 14:42:47 +01:00
/*this.pages.forEach(item => {
this.stepperItems.push({
label: '',
})
});*/
2017-12-06 13:16:31 +01:00
}
2017-12-11 15:21:49 +01:00
changePageIndex(index: any) {
2017-12-19 18:34:00 +01:00
this.router.navigate([this.route.snapshot.url[0] + "/" + this.route.snapshot.url[1]], { queryParams: { page: this.pages[index - 1] } });
2017-12-11 15:21:49 +01:00
}
2017-12-11 17:39:32 +01:00
scrollTo(sectionID: string) {
2017-12-13 13:01:43 +01:00
if (!sectionID) return;
var element = document.querySelector('#' + sectionID);
if (!element) return;
element.scrollIntoView();
2018-01-25 09:14:43 +01:00
this.visibleSidebar = true;
2017-12-22 14:42:47 +01:00
var scrollElement = document.querySelector('.scrollableContent');
2017-12-13 13:01:43 +01:00
//scrollElement.scrollTop = topElement.offsetTop;
2017-12-11 17:39:32 +01:00
}
changeCurrentPage(pageString: string) {
2017-12-13 13:01:43 +01:00
if (!pageString) return;
var page = parseInt(pageString);
if (isNaN(page)) return;
var pageIndex = this.pages.indexOf(page);
if (pageIndex === -1) return;
this.currentPageIndex = page;
}
}