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'; 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'; 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'; import { PaginationService } from '../../app/services/pagination.service'; import { TokenService, TokenProvider } from '../services/login/token.service'; import { ModalComponent } from '../modal/modal.component'; import {Location} from '@angular/common'; import { AngularDraggableModule } from 'angular2-draggable'; import {MenuItem} from 'primeng/primeng'; import {PDFService} from '../services/transformers/pdf.service'; import './../../assets/xml2json.min.js'; declare var X2JS: any; var flatten = require('flat'); declare var $ :any; import * as scroll from '../../assets/jquery.scrollTo.min.js'; import '../../assets/custom.js'; declare function simple_notifier(type: string, title: string, message:string): any; //import '../../assets/perfect-scrollbar/perfect-scrollbar.js'; declare var PerfectScrollbar : any; @Component({ selector: 'dynamic-form', templateUrl: './dynamic-form.component.html', styleUrls: [ './dynamic-form.component.css', '../../assets/perfect-scrollbar/perfect-scrollbar.css' ], providers: [ FieldControlService, ServerService ] }) export class DynamicFormComponent implements OnInit { @Input() dataModel: DatasetModel = new DatasetModel(); form: FormGroup; payLoad = ''; @Input() dirtyValues: number = 0; // pagination object @Input() pagination: any = {}; finalizeStatus:boolean = false; id: string; datasetId: string; pathName:string; //datasetProperties:string; private fragment: string; xml2jsonOBJ: any; expandedToc : boolean = true; 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) { this.form = this.qcs.toFormGroup(new Array(), new Array()); this.xml2jsonOBJ = new X2JS(); } getSubForm(subformName) { return this.form.controls[subformName]; } ngOnInit() { this.dataModel = new JsonSerializer().fromJSONObject(TestModel,DatasetModel); this.form = this.dataModel.buildForm(); this.visibilityRulesService.formGroup = this.form; let rules:Rule[] = new JsonSerializer().fromJSONArray(TestModel.rules,Rule); this.visibilityRulesService.buildVisibilityRules(rules) } /* scrollToElemID(elemID) { scroll("#" + elemID); } private patchForm(flatList: any) { for (var prop in flatList) { if (flatList.hasOwnProperty(prop)) { 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()); } } } ngAfterViewChecked(): void { //navigate to certain section of the page try { document.querySelector('#' + this.fragment).scrollIntoView(); } catch (e) { } } submitForm(final){ debugger; this.serverService.getDatasetByID(this.datasetId).subscribe( (data) => { if (final) data.status = 2; //data.properties = ""; data.properties = JSON.stringify(this.form.value); data.profile = {"id": data.profile.id}; this.serverService.setDataset(data).subscribe( (data) => { console.log("Updated dataset"); if (final){ this._location.back(); simple_notifier("success",null,"Finalized form progress"); } else{ simple_notifier("success",null,"Saved form progress"); } }, (err) => { simple_notifier("danger",null,"Could not save form progress"); }); }, (err) => { simple_notifier("danger",null,"Could not save form progress"); }); } SaveForm() { let final = false; this.submitForm(false); this.payLoad = JSON.stringify(this.form.value); } SaveFinalizeForm(){ $("#confirmModal").modal("hide"); let final = true; this.submitForm(final); } shouldIShow(element) { //pagination , pages are declared in xml for every groupfield if (this.pagination.currentPage == element.page){ return true; } else return false; } setPage(page: number) { if (page < 1 || page > this.pagination.totalPages) { return; } var pagesize = 4; // get pagination object from service this.pagination = this.pagerService.getPagination(this.dataModel.groups, this.dataModel.groups.length, page, pagesize); //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); } toggleTOC(){ this.expandedToc = !this.expandedToc; } */ createPDF(elementID : string, pdffilename : string){ this.pdfService.toPDF(elementID, pdffilename); } }