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

147 lines
4.7 KiB
TypeScript

import { VisibilityRulesService } from '../visibility-rules/visibility-rules.service';
import { DatasetModel } from '../models/DatasetModel';
import { Rule } from '../models/Rule';
import { Section } from '../models/Section';
import { JsonSerializer } from '../utilities/JsonSerializer';
import { Component, Input, OnInit, AfterViewChecked, ViewChild, forwardRef, ViewEncapsulation } from '@angular/core';
import { FormGroup, Validators, ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { NgForm } from '@angular/forms';
import { Router, ActivatedRoute, ParamMap, Params } from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { Location } from '@angular/common';
import {MatSidenavModule} from '@angular/material/sidenav';
declare function simple_notifier(type: string, title: string, message: string): any;
@Component({
selector: 'dynamic-form',
templateUrl: './dynamic-form.component.html',
styleUrls: [
'./dynamic-form.component.css'
],
providers: [
],
encapsulation: ViewEncapsulation.None,
})
export class DynamicFormComponent implements OnInit {
@Input() dataModel: DatasetModel = new DatasetModel();
@Input() path: string;
form: FormGroup;
id: string;
datasetId: string;
pathName: string;
pages: Array<number>;
activeStepperIndex: number = 1;
visibleSidebar: boolean = false;
private progressbar: boolean = false;
private currentPageIndex: number = 0;
private fragment: string;
constructor(private router: Router,
private _location: Location,
private route: ActivatedRoute,
private visibilityRulesService: VisibilityRulesService
) {
this.datasetId = route.snapshot.params['id'];
}
getSubForm(subformName) {
return this.form.controls[subformName];
}
ngOnInit() {
// this.serverService.getSingleDataset(this.datasetId).subscribe(
// (response: any) => {
// this.dataModel = new JsonSerializer<DatasetModel>().fromJSONObject(response, DatasetModel);
// this.pages = this.getPages(this.dataModel);
// this.createPagination();
// this.form = this.dataModel.buildForm();
// this.visibilityRulesService.formGroup = this.form;
// let rules: Rule[] = new JsonSerializer<Rule>().fromJSONArray(response.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"]);
// });
// },
// error => {
// console.log("Could not load dmp");
// }
// )
/* else{
this.addSection();
}
this.dataModel = new JsonSerializer<DatasetModel>().fromJSONObject(TestModel,DatasetModel);
this.form = this.dataModel.buildForm();
this.visibilityRulesService.formGroup = this.form;
let rules:Rule[] = new JsonSerializer<Rule>().fromJSONArray(TestModel.rules,Rule);
this.visibilityRulesService.buildVisibilityRules(rules) */
}
submit() {
//this.serverService.updateDataset(this.datasetId, this.form.value).subscribe()
}
toggleSidebar() {
this.visibleSidebar = !this.visibleSidebar;
}
getPages(model: DatasetModel): Array<number> {
let pageSet = new Set<number>();
model.sections.forEach(section => {
pageSet.add(section.page);
});
return Array.from(pageSet).sort((a, b) => a - b);
}
shouldDisplaySection(section: Section): Boolean {
return (section.page) == this.currentPageIndex;
}
createPagination() {
this.pages.forEach(item => {
// this.stepperItems.push({
// label: '',
// })
});
}
changePageIndex(index: any) {
this.router.navigate([this.route.snapshot.url[0] + "/" + this.route.snapshot.url[1]], { queryParams: { page: this.pages[index - 1] } });
}
scrollTo(sectionID: string) {
if (!sectionID) return;
var element = document.querySelector('#' + sectionID);
if (!element) return;
element.scrollIntoView();
// this.visibleSidebar = true;
//var scrollElement = document.querySelector('.scrollableContent');
//scrollElement.scrollTop = topElement.offsetTop;
}
changeCurrentPage(pageString: string) {
if (!pageString) return;
var page = parseInt(pageString);
if (isNaN(page)) return;
var pageIndex = this.pages.indexOf(page);
if (pageIndex === -1) return;
this.currentPageIndex = page;
}
}