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

235 lines
7.1 KiB
TypeScript
Raw Normal View History

import { VisibilityRulesService } from '../visibility-rules/visibility-rules.service';
2017-11-23 12:26:22 +01:00
import { TestModel } from '../testModel/testmodel';
import { DatasetModel } from '../models/DatasetModel';
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';
//import { FieldBase } from '../../app/form/fields/field-base';
import { FieldControlService } from '../../app/services/field-control.service';
import { ServerService } from '../../app/services/server.service';
2017-11-01 18:18:27 +01:00
import { TokenService, TokenProvider } from '../services/login/token.service';
2017-11-07 15:40:32 +01:00
import { ModalComponent } from '../modal/modal.component';
2017-12-05 17:56:21 +01:00
import { Location } from '@angular/common';
2017-11-20 11:29:42 +01:00
import { AngularDraggableModule } from 'angular2-draggable';
2017-12-05 17:56:21 +01:00
import { MenuItem } from 'primeng/primeng';
2017-11-15 16:11:35 +01:00
2017-12-05 17:56:21 +01:00
import { PDFService } from '../services/transformers/pdf.service';
2017-10-17 11:13:10 +02:00
import './../../assets/xml2json.min.js';
declare var X2JS: any;
var flatten = require('flat');
2017-12-05 17:56:21 +01:00
declare var $: any;
import * as scroll from '../../assets/jquery.scrollTo.min.js';
2017-11-10 12:38:18 +01:00
import '../../assets/custom.js';
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
2017-11-10 12:38:18 +01:00
//import '../../assets/perfect-scrollbar/perfect-scrollbar.js';
2017-12-05 17:56:21 +01:00
declare var PerfectScrollbar: any;
@Component({
selector: 'dynamic-form',
templateUrl: './dynamic-form.component.html',
2017-11-10 12:38:18 +01:00
styleUrls: [
2017-12-06 09:46:05 +01:00
'./dynamic-form.component.css'
2017-11-10 12:38:18 +01:00
],
2017-11-10 11:24:20 +01:00
providers: [
2017-11-27 14:40:16 +01:00
FieldControlService, ServerService
2017-12-06 09:46:05 +01:00
],
encapsulation: ViewEncapsulation.None,
})
export class DynamicFormComponent implements OnInit {
2017-11-23 12:26:22 +01:00
@Input() dataModel: DatasetModel = new DatasetModel();
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;
datasetId: string;
2017-12-05 17:56:21 +01:00
pathName: string;
2017-12-06 13:16:31 +01:00
pages: Set<number>;
stepperItems: MenuItem[] = new Array<MenuItem>();
2017-12-06 09:46:05 +01:00
activeStepperIndex: number = 1;
visibleSidebar: boolean = false;
2017-12-06 13:16:31 +01:00
private progressbar: boolean = false;
2017-12-11 11:36:29 +01:00
private currentPage: number = 0;
2017-12-06 13:16:31 +01:00
private fragment: string;
2017-12-06 17:45:54 +01:00
constructor(private serverService: ServerService, private router: Router, private pdfService: PDFService,
private _location: Location, private route: ActivatedRoute, private tokenService: TokenService
, private visibilityRulesService: VisibilityRulesService
) {
2017-12-06 13:16:31 +01:00
this.datasetId = route.snapshot.params['id'];
}
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() {
2017-12-06 09:46:05 +01:00
2017-12-07 16:15:28 +01:00
this.serverService.getSingleDataset(this.datasetId).subscribe(
2017-12-06 13:16:31 +01:00
response => {
this.dataModel = new JsonSerializer<DatasetModel>().fromJSONObject(response, DatasetModel);
this.pages = this.getPages(this.dataModel);
2017-12-11 10:20:27 +01:00
this.createPagination();
2017-12-06 13:16:31 +01:00
this.form = this.dataModel.buildForm();
this.visibilityRulesService.formGroup = this.form;
let rules: Rule[] = new JsonSerializer<Rule>().fromJSONArray(response.rules, Rule);
this.visibilityRulesService.buildVisibilityRules(rules)
2017-12-11 10:20:27 +01:00
this.progressbar = true;
this.route.fragment.subscribe((fragment: string) => {
if (fragment && document.querySelector('#' + fragment)) {
document.querySelector('#' + fragment).scrollIntoView();
this.visibleSidebar = true;
}
});
this.route.queryParams.subscribe((params) => {
if (params && "page" in params && !isNaN(params["page"]))
this.currentPage = Number.parseInt(params["page"]);
//this.visibleSidebar = true;
});
2017-12-06 13:16:31 +01:00
},
error => {
console.log("Could not load dmp");
2017-12-06 09:46:05 +01:00
}
2017-12-06 13:16:31 +01:00
)
2017-12-05 17:56:21 +01:00
2017-12-06 13:16:31 +01:00
/* 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) */
}
2017-12-06 13:16:31 +01:00
submit() {
this.serverService.updateDataset(this.datasetId, this.form.value).subscribe()
2017-12-05 17:56:21 +01:00
}
2017-12-06 09:46:05 +01:00
toggleSidebar() {
this.visibleSidebar = !this.visibleSidebar;
}
2017-12-06 13:16:31 +01:00
getPages(model: DatasetModel): Set<number> {
let pageSet = new Set<number>();
model.sections.forEach(section => {
pageSet.add(section.page);
})
return pageSet;
}
shouldDisplaySection(section: Section): Boolean {
2017-12-11 11:36:29 +01:00
return (section.page - 1) == this.currentPage;
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() {
this.pages.forEach(item => {
2017-12-11 11:36:29 +01:00
this.stepperItems.push({
label: '',
command: (event: any) => {
this.currentPage = event.index;
}
})
2017-12-11 10:20:27 +01:00
});
2017-12-06 13:16:31 +01:00
}
2017-11-23 12:26:22 +01:00
/* scrollToElemID(elemID) {
2017-11-03 18:14:32 +01:00
scroll("#" + elemID);
}
2017-11-03 18:14:32 +01:00
private patchForm(flatList: any) {
2017-11-10 12:38:18 +01:00
for (var prop in flatList) {
if (flatList.hasOwnProperty(prop)) {
2017-11-03 18:14:32 +01:00
if (prop.endsWith('.id') || prop.endsWith('.answer') || prop.endsWith('.value')) continue;
//console.log("updating value of "+prop +" to "+flatList[prop].valueOf())
this.form.get(prop).setValue(flatList[prop].valueOf());
}
}
2017-11-15 10:03:06 +01:00
}
ngAfterViewChecked(): void { //navigate to certain section of the page
try {
document.querySelector('#' + this.fragment).scrollIntoView();
} catch (e) { }
}
2017-12-05 17:56:21 +01:00
2017-11-15 18:32:34 +01:00
SaveForm() {
let final = false;
this.submitForm(false);
this.payLoad = JSON.stringify(this.form.value);
}
2017-11-15 18:32:34 +01:00
SaveFinalizeForm(){
$("#confirmModal").modal("hide");
let final = true;
this.submitForm(final);
2017-11-07 15:40:32 +01:00
}
2017-11-06 10:57:13 +01:00
shouldIShow(element) { //pagination , pages are declared in xml for every groupfield
if (this.pagination.currentPage == element.page){
2017-11-03 18:14:32 +01:00
return true;
}
2017-11-06 10:57:13 +01:00
else
2017-11-03 18:14:32 +01:00
return false;
}
2017-10-09 15:36:29 +02:00
setPage(page: number) {
2017-11-03 18:14:32 +01:00
2017-10-09 15:36:29 +02:00
if (page < 1 || page > this.pagination.totalPages) {
2017-11-03 18:14:32 +01:00
return;
2017-10-09 15:36:29 +02:00
}
2017-11-06 10:57:13 +01:00
var pagesize = 4;
2017-10-09 15:36:29 +02:00
// get pagination object from service
2017-11-03 18:14:32 +01:00
this.pagination = this.pagerService.getPagination(this.dataModel.groups, this.dataModel.groups.length, page, pagesize);
2017-10-09 15:36:29 +02:00
2017-11-03 18:14:32 +01:00
//get current page of items
// this.dataModel.sections.forEach(section => {
// if (section.groupFields.length > 0) {
// section.groupFields = this.dataModel.groups.slice(this.pagination.startIndex, this.pagination.endIndex + 1);
// }
// });
//this.dataModel.groups = this.dataModel.groups.slice(this.pagination.startIndex, this.pagination.endIndex + 1);
}
2017-11-15 11:59:08 +01:00
2017-11-03 18:14:32 +01:00
2017-11-13 17:19:43 +01:00
toggleTOC(){
this.expandedToc = !this.expandedToc;
}
2017-11-23 12:26:22 +01:00
*/
2017-12-06 13:16:31 +01:00
createPDF(elementID: string, pdffilename: string) {
this.pdfService.toPDF(elementID, pdffilename);
}
}