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';
|
2017-09-14 12:37:36 +02:00
|
|
|
|
import { NgForm } from '@angular/forms';
|
2017-10-30 15:56:50 +01:00
|
|
|
|
import { Router, ActivatedRoute, ParamMap, Params } from '@angular/router';
|
2017-09-28 17:05:46 +02:00
|
|
|
|
import 'rxjs/add/operator/switchMap';
|
2017-09-14 12:37:36 +02:00
|
|
|
|
|
|
|
|
|
//import { FieldBase } from '../../app/form/fields/field-base';
|
|
|
|
|
import { FieldControlService } from '../../app/services/field-control.service';
|
|
|
|
|
import { ServerService } from '../../app/services/server.service';
|
|
|
|
|
import { dataModelBuilder } from '../../app/services/dataModelBuilder.service';
|
|
|
|
|
import { DataModel } from '../entities/DataModel';
|
|
|
|
|
import { GroupBase } from './dynamic-form-group/group-base';
|
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-11-10 10:53:01 +01:00
|
|
|
|
import { AutocompleteRemoteComponent } from './fields/autocomplete-remote/autocomplete-remote.component';
|
2017-09-14 12:37:36 +02:00
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
import { AngularDraggableModule } from 'angular2-draggable';
|
|
|
|
|
|
2017-10-17 11:13:10 +02:00
|
|
|
|
import './../../assets/xml2json.min.js';
|
|
|
|
|
declare var X2JS: any;
|
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
var flatten = require('flat');
|
2017-11-07 15:40:32 +01:00
|
|
|
|
declare var $ :any;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as scroll from '../../assets/jquery.scrollTo.min.js';
|
|
|
|
|
|
|
|
|
|
|
2017-11-10 12:38:18 +01:00
|
|
|
|
import '../../assets/custom.js';
|
|
|
|
|
declare function simple_notifier(type: string, title: string, message:string): any;
|
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-11-10 12:38:18 +01:00
|
|
|
|
//import '../../assets/perfect-scrollbar/perfect-scrollbar.js';
|
|
|
|
|
declare var PerfectScrollbar : any;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-09-14 12:37:36 +02:00
|
|
|
|
@Component({
|
|
|
|
|
selector: 'dynamic-form',
|
|
|
|
|
templateUrl: './dynamic-form.component.html',
|
2017-11-10 12:38:18 +01:00
|
|
|
|
styleUrls: [
|
|
|
|
|
'./dynamic-form.component.css',
|
|
|
|
|
'../../assets/perfect-scrollbar/perfect-scrollbar.css'
|
|
|
|
|
],
|
2017-11-10 11:24:20 +01:00
|
|
|
|
providers: [
|
|
|
|
|
FieldControlService, ServerService, dataModelBuilder
|
|
|
|
|
]
|
2017-09-14 12:37:36 +02:00
|
|
|
|
})
|
|
|
|
|
export class DynamicFormComponent implements OnInit {
|
|
|
|
|
|
|
|
|
|
@Input() dataModel: DataModel = new DataModel();
|
2017-10-04 11:39:45 +02:00
|
|
|
|
form: FormGroup;
|
|
|
|
|
payLoad = '';
|
|
|
|
|
@Input() dirtyValues: number = 0;
|
2017-10-09 15:36:29 +02:00
|
|
|
|
// pagination object
|
|
|
|
|
@Input() pagination: any = {};
|
2017-11-10 16:29:57 +01:00
|
|
|
|
finalizeStatus:boolean = false;
|
2017-11-03 18:14:32 +01:00
|
|
|
|
id: string;
|
|
|
|
|
datasetId: string;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
//datasetProperties:string;
|
2017-11-03 18:14:32 +01:00
|
|
|
|
|
2017-10-10 16:10:00 +02:00
|
|
|
|
private fragment: string;
|
2017-10-17 11:13:10 +02:00
|
|
|
|
xml2jsonOBJ: any;
|
2017-10-10 16:10:00 +02:00
|
|
|
|
|
2017-11-09 11:46:48 +01:00
|
|
|
|
expandedToc : boolean = true;
|
2017-11-09 15:18:02 +01:00
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-10-09 15:36:29 +02:00
|
|
|
|
constructor(private qcs: FieldControlService, private serverService: ServerService, private dataModelService: dataModelBuilder, private router: Router,
|
2017-11-03 18:14:32 +01:00
|
|
|
|
private route: ActivatedRoute, private pagerService: PaginationService, private tokenService: TokenService) {
|
2017-09-21 12:32:58 +02:00
|
|
|
|
this.form = this.qcs.toFormGroup(new Array(), new Array());
|
2017-10-17 11:13:10 +02:00
|
|
|
|
this.xml2jsonOBJ = new X2JS();
|
2017-09-14 12:37:36 +02: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
|
|
|
|
|
|
|
|
|
|
2017-09-14 12:37:36 +02:00
|
|
|
|
ngOnInit() {
|
2017-09-26 17:16:04 +02:00
|
|
|
|
|
2017-11-10 16:29:57 +01:00
|
|
|
|
//this.route.fragment.subscribe(fragment => { this.fragment = fragment; }); //navigate to certain section of the page, it doesn't use anymore
|
2017-11-03 18:14:32 +01:00
|
|
|
|
let sub = this.route.params.subscribe(params => {
|
2017-10-30 15:56:50 +01:00
|
|
|
|
this.id = params.id;
|
2017-10-30 16:35:11 +01:00
|
|
|
|
this.datasetId = params.datasetId;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
//this.datasetProperties = params.datasetProperties
|
2017-11-03 18:14:32 +01:00
|
|
|
|
});
|
2017-10-04 11:39:45 +02:00
|
|
|
|
|
2017-11-03 18:14:32 +01:00
|
|
|
|
|
|
|
|
|
this.serverService.getDatasetByID(this.datasetId).subscribe(
|
2017-11-01 10:09:47 +01:00
|
|
|
|
(data) => {
|
2017-11-02 15:46:43 +01:00
|
|
|
|
var flatList;
|
2017-11-03 18:14:32 +01:00
|
|
|
|
var formValues = "";
|
|
|
|
|
if (data.properties) {
|
2017-11-01 10:09:47 +01:00
|
|
|
|
console.log("Found already submitted form, loading that one!");
|
|
|
|
|
|
2017-11-02 17:39:05 +01:00
|
|
|
|
formValues = JSON.parse(data.properties);
|
2017-11-02 15:46:43 +01:00
|
|
|
|
flatList = flatten(formValues);
|
2017-11-02 17:39:05 +01:00
|
|
|
|
//this.patchForm(flatList);
|
2017-11-10 16:29:57 +01:00
|
|
|
|
if(data.status == 2) this.finalizeStatus=true;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 18:14:32 +01:00
|
|
|
|
//η κληση για το dataset profile pou htan prin pio panw anexarthth
|
|
|
|
|
this.serverService.getDatasetProfileByID(this.id).subscribe(
|
|
|
|
|
|
|
|
|
|
response => {
|
|
|
|
|
|
|
|
|
|
console.log("response");
|
|
|
|
|
console.log(response);
|
|
|
|
|
|
|
|
|
|
const data = response;
|
|
|
|
|
|
|
|
|
|
//replace the xmls {model,view,rule} definitions with json -- https://github.com/abdmob/x2js library
|
|
|
|
|
data.definition = this.xml2jsonOBJ.xml_str2json(data.definition);
|
|
|
|
|
data.ruleset.definition = this.xml2jsonOBJ.xml_str2json(data.ruleset.definition);
|
|
|
|
|
data.viewstyle.definition = this.xml2jsonOBJ.xml_str2json(data.viewstyle.definition);
|
|
|
|
|
//can be converted back to xml (which shouldn't be needed) with this.xml2jsonOBJ.json2xml_str
|
|
|
|
|
|
|
|
|
|
console.log("this.serverService.getFields");
|
|
|
|
|
|
|
|
|
|
console.log("data.dataset")
|
|
|
|
|
console.log(data.definition)
|
|
|
|
|
console.log(data.ruleset.definition)
|
|
|
|
|
console.log(data.viewstyle.definition)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.dataModel = new DataModel();
|
2017-11-09 17:43:01 +01:00
|
|
|
|
this.dataModel = this.dataModelService.getDataModel(data, formValues); //get formvalues and acossiate them with fields
|
2017-11-03 18:14:32 +01:00
|
|
|
|
|
|
|
|
|
this.form = this.qcs.toFormGroup(this.dataModel.fields, this.dataModel.groups);
|
|
|
|
|
|
|
|
|
|
this.form.valueChanges.subscribe(data => {
|
|
|
|
|
// console.log('Form changes', data);
|
|
|
|
|
let dirtyValuesArray: Array<any> = [];
|
|
|
|
|
let count = 0;
|
|
|
|
|
let countDirtyValues = 0;
|
|
|
|
|
let percentage = 0;
|
|
|
|
|
Object.keys(this.form.controls).forEach((c) => {
|
|
|
|
|
//count++;
|
|
|
|
|
let currentControl = this.form.controls[c];
|
|
|
|
|
if (currentControl.dirty)
|
|
|
|
|
dirtyValuesArray.push(currentControl.value);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Object.keys(this.form.value).forEach((c) => {
|
|
|
|
|
//count++;
|
|
|
|
|
Object.keys(this.form.value[c]).forEach((item) => {
|
|
|
|
|
let value = this.form.value[c][item]
|
2017-11-09 17:43:01 +01:00
|
|
|
|
if (typeof(value) == "string") value.trim();
|
|
|
|
|
if(typeof(value)== "boolean") value.toString();
|
|
|
|
|
if (value != undefined && value != "")
|
|
|
|
|
countDirtyValues++; //TODO REMOVE SPACES FROM EMPTY STRING
|
2017-11-02 17:39:05 +01:00
|
|
|
|
});
|
2017-11-03 18:14:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.dataModel.groups.forEach(grp => {
|
|
|
|
|
grp.groupFields.forEach((fld) => {
|
|
|
|
|
if (fld.visible == true || fld.visible == "true")
|
|
|
|
|
count++;
|
|
|
|
|
// if (fld.value != undefined && fld.value != " ")
|
|
|
|
|
// countDirtyValues++;
|
2017-11-02 17:39:05 +01:00
|
|
|
|
});
|
2017-11-03 18:14:32 +01:00
|
|
|
|
});
|
|
|
|
|
//console.log(count);
|
|
|
|
|
// var percentage = Math.floor(dirtyValuesArray.length * 100 / count);
|
|
|
|
|
percentage = Math.floor(countDirtyValues * 100 / count);
|
|
|
|
|
this.dirtyValues = percentage;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//this.form = this.qcs.toFormGroup(this.fields);
|
|
|
|
|
|
|
|
|
|
console.log("SUMMARY: ======>");
|
|
|
|
|
console.log(this.dataModel);
|
|
|
|
|
console.log(this.form);
|
|
|
|
|
|
|
|
|
|
this.route.paramMap //this is how i get the projects's id
|
|
|
|
|
|
|
|
|
|
// initialize to page 1
|
|
|
|
|
this.setPage(1);
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
err => {
|
|
|
|
|
console.log("There was an error fetching the data from server");
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
//end- klhsh gia to dataset profiel
|
2017-11-01 10:09:47 +01:00
|
|
|
|
},
|
|
|
|
|
(err) => {
|
2017-11-03 18:14:32 +01:00
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
});
|
2017-11-10 12:38:18 +01:00
|
|
|
|
|
|
|
|
|
var formScroller = new PerfectScrollbar("#form-container");
|
|
|
|
|
var tocScroller = new PerfectScrollbar("#toc-container");
|
2017-11-10 14:57:08 +01:00
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 18:14:32 +01:00
|
|
|
|
scrollToElemID(elemID) {
|
|
|
|
|
scroll("#" + elemID);
|
2017-09-14 12:37:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-11-03 18:14:32 +01:00
|
|
|
|
private patchForm(flatList: any) {
|
|
|
|
|
|
2017-11-10 12:38:18 +01:00
|
|
|
|
|
2017-11-01 10:09:47 +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;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
//console.log("updating value of "+prop +" to "+flatList[prop].valueOf())
|
|
|
|
|
this.form.get(prop).setValue(flatList[prop].valueOf());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//this.form.get("namingConventionGroup.nonamingConventionA213").setValue("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:10:00 +02:00
|
|
|
|
ngAfterViewChecked(): void { //navigate to certain section of the page
|
2017-11-10 12:38:18 +01:00
|
|
|
|
|
2017-10-10 16:10:00 +02:00
|
|
|
|
try {
|
|
|
|
|
document.querySelector('#' + this.fragment).scrollIntoView();
|
|
|
|
|
} catch (e) { }
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 16:29:57 +01:00
|
|
|
|
submitForm(final){
|
2017-11-03 18:14:32 +01:00
|
|
|
|
this.serverService.getDatasetByID(this.datasetId).subscribe(
|
2017-11-01 10:09:47 +01:00
|
|
|
|
(data) => {
|
2017-11-10 16:29:57 +01:00
|
|
|
|
if (final) data.status = 2;
|
2017-11-01 10:09:47 +01:00
|
|
|
|
//is xml, so transform them to xml (it's json)
|
|
|
|
|
//data.properties = this.xml2jsonOBJ.json2xml_str(JSON.stringify(this.form.value));
|
|
|
|
|
data.properties = JSON.stringify(this.form.value);
|
|
|
|
|
|
2017-11-10 12:38:18 +01:00
|
|
|
|
this.serverService.setDataset(data).subscribe(
|
|
|
|
|
(data) => {
|
|
|
|
|
console.log("Updated dataset");
|
|
|
|
|
//simple_notifier("success",null,"Saved form progress");
|
|
|
|
|
this.router.navigate(['/workspace'], { queryParams: { /*returnUrl: this.state.url*/ }});
|
|
|
|
|
},
|
2017-11-03 18:14:32 +01:00
|
|
|
|
(err) => {
|
2017-11-10 12:38:18 +01:00
|
|
|
|
simple_notifier("danger",null,"Could not save form progress");
|
2017-11-03 18:14:32 +01:00
|
|
|
|
});
|
2017-11-01 10:09:47 +01:00
|
|
|
|
},
|
|
|
|
|
(err) => {
|
2017-11-10 12:38:18 +01:00
|
|
|
|
simple_notifier("danger",null,"Could not save form progress");
|
2017-11-01 10:09:47 +01:00
|
|
|
|
});
|
2017-11-07 15:40:32 +01:00
|
|
|
|
}
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-11-07 15:40:32 +01:00
|
|
|
|
onSubmit() {
|
2017-11-10 16:29:57 +01:00
|
|
|
|
let final = false;
|
|
|
|
|
this.submitForm(false);
|
2017-11-01 10:09:47 +01:00
|
|
|
|
|
2017-09-14 12:37:36 +02:00
|
|
|
|
this.payLoad = JSON.stringify(this.form.value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 15:40:32 +01:00
|
|
|
|
SaveFinalize(){
|
2017-11-10 16:29:57 +01:00
|
|
|
|
$("#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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signOut2() {
|
|
|
|
|
var auth2 = gapi.auth2.getAuthInstance();
|
|
|
|
|
auth2.signOut().then(function () {
|
|
|
|
|
console.log('User signed out.');
|
|
|
|
|
localStorage.removeItem('currentUser');
|
2017-10-09 15:36:29 +02:00
|
|
|
|
});
|
2017-11-03 18:14:32 +01:00
|
|
|
|
this.tokenService.setToken(null); //kanonika prepei na mpei mesa sthn function.....
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 17:19:43 +01:00
|
|
|
|
toggleTOC(){
|
|
|
|
|
console.log("toggling toc")
|
|
|
|
|
this.expandedToc = !this.expandedToc;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 12:37:36 +02:00
|
|
|
|
}
|