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

191 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-11-23 12:26:22 +01:00
import { VisibilityRulesService } from '../visibility-rules/visibility-rules.service';
import { TestModel } from '../testModel/testmodel';
import { DatasetModel } from '../models/DatasetModel';
import { Rule } from '../models/Rule';
import { JsonSerializer } from '../utilities/JsonSerializer';
2017-11-10 10:53:01 +01:00
import { Component, Input, OnInit, AfterViewChecked, ViewChild, forwardRef } from '@angular/core';
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-10-09 15:36:29 +02:00
import { PaginationService } from '../../app/services/pagination.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-05 17:56:21 +01:00
'./dynamic-form.component.css',
2017-11-10 12:38:18 +01:00
'../../assets/perfect-scrollbar/perfect-scrollbar.css'
],
2017-11-10 11:24:20 +01:00
providers: [
2017-11-27 14:40:16 +01:00
FieldControlService, ServerService
2017-11-10 11:24:20 +01:00
]
})
export class DynamicFormComponent implements OnInit {
2017-11-23 12:26:22 +01:00
@Input() dataModel: DatasetModel = new DatasetModel();
2017-12-05 17:56:21 +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-11-03 18:14:32 +01:00
2017-12-05 17:56:21 +01:00
private progressbar:boolean = false;
2017-11-09 15:18:02 +01:00
2017-12-05 17:56:21 +01:00
constructor(private qcs: FieldControlService, private serverService: ServerService, private router: Router, private pdfService: PDFService,
private _location: Location, private route: ActivatedRoute, private pagerService: PaginationService, private tokenService: TokenService, private visibilityRulesService: VisibilityRulesService) {
}
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-05 17:56:21 +01:00
//this.dataModel = new JsonSerializer<DatasetModel>().fromJSONObject(new DatasetModel(), DatasetModel);
//this.form = this.dataModel.buildForm();
let sub = this.route.queryParams.subscribe(params => {
let dmpid = params.id;
this.datasetId = params.datasetId;
this.serverService.getDmp(this.datasetId).subscribe(
response => {
this.dataModel = new JsonSerializer<DatasetModel>().fromJSONObject(response, DatasetModel);
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;
},
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) */
});
}
2017-12-05 17:56:21 +01:00
submit(){
this.serverService.updateDataset(this.datasetId,this.form.value).subscribe()
}
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
*/
createPDF(elementID : string, pdffilename : string){
2017-12-05 17:56:21 +01:00
this.pdfService.toPDF(elementID, pdffilename);
}
}