drop down list from servicve

This commit is contained in:
annampak 2017-10-18 23:36:33 +03:00
parent 54a1796725
commit e4f302fd62
2 changed files with 248 additions and 204 deletions

View File

@ -14,6 +14,7 @@ export class FieldBase<T>{
description:string; description:string;
attributes: Attribute; attributes: Attribute;
regex:string; regex:string;
url: any;
constructor(options: { constructor(options: {
value?: T, value?: T,
@ -27,7 +28,8 @@ export class FieldBase<T>{
group?: string group?: string
description?: string, description?: string,
attributes?: Attribute, attributes?: Attribute,
regex?:string regex?:string,
url?: any
} = {}) { } = {}) {
this.value = options.value; this.value = options.value;
this.key = options.key || ''; this.key = options.key || '';
@ -41,5 +43,6 @@ export class FieldBase<T>{
this.description = options.description || ''; this.description = options.description || '';
this.attributes = options.attributes || new Attribute(); this.attributes = options.attributes || new Attribute();
this.regex = options.regex || ''; this.regex = options.regex || '';
this.url = options.url || {"url":null, "fieldpath":null, "data":null};
} }
} }

View File

@ -5,132 +5,135 @@ import { TextboxField } from '../../app/form/fields/textbox/field-textbox';
import { CheckBoxField } from '../../app/form/fields/checkbox/field-checkbox'; import { CheckBoxField } from '../../app/form/fields/checkbox/field-checkbox';
import { RadioBoxField } from '../../app/form/fields/radiobox/field-radiobox'; import { RadioBoxField } from '../../app/form/fields/radiobox/field-radiobox';
import { DataModel } from '../entities/DataModel'; import { DataModel } from '../entities/DataModel';
import {Rule} from '../entities/common/rule'; import { Rule } from '../entities/common/rule';
import { GroupBase } from '../form/dynamic-form-group/group-base'; import { GroupBase } from '../form/dynamic-form-group/group-base';
import { Attribute } from '../entities/model/attribute'; import { Attribute } from '../entities/model/attribute';
import { Param } from '../entities/model/param'; import { Param } from '../entities/model/param';
import { Section } from '../entities/model/section'; import { Section } from '../entities/model/section';
import { RestBase } from '../services/rest-base';
@Injectable() @Injectable()
export class dataModelBuilder { export class dataModelBuilder {
private dataModel : DataModel; private dataModel: DataModel;
private fields : FieldBase<any>[]; private fields: FieldBase<any>[];
constructor(private restBase: RestBase) { }
public getDataModel(data) {
if (this.dataModel != null)
return this.dataModel;
this.dataModel = new DataModel();
this.fields = this.buildFields(data.dataset.profile.viewstyle.definition.root.fields.field);
this.dataModel.groups = this.getGroups(data.dataset.profile.viewstyle.definition.root.fieldGroups.fieldGroup, this.fields);
this.dataModel.semanticAttr = new Array(new Attribute);
//this.dataModel.semanticAttr = data.dataset.profile.definition.root.fields.field;
this.dataModel.semanticAttr = this.getFieldsAttributes(data.dataset.profile.definition.root.fields.field, data.dataset.profile.ruleset.definition.root.functions.function, this.fields);
this.dataModel.sections = this.getSections(data.dataset.profile.viewstyle.definition.root.sections.section, this.dataModel.groups);
this.dataModel.buildIndex();
public getDataModel(data){
if(this.dataModel != null)
return this.dataModel; return this.dataModel;
this.dataModel = new DataModel();
this.fields = this.buildFields(data.dataset.profile.viewstyle.definition.root.fields.field);
this.dataModel.groups = this.getGroups(data.dataset.profile.viewstyle.definition.root.fieldGroups.fieldGroup, this.fields);
this.dataModel.semanticAttr = new Array(new Attribute);
//this.dataModel.semanticAttr = data.dataset.profile.definition.root.fields.field;
this.dataModel.semanticAttr = this.getFieldsAttributes(data.dataset.profile.definition.root.fields.field, data.dataset.profile.ruleset.definition.root.functions.function, this.fields) ;
this.dataModel.sections = this.getSections(data.dataset.profile.viewstyle.definition.root.sections.section, this.dataModel.groups) ;
this.dataModel.buildIndex();
return this.dataModel;
}
private buildFields(fields:any[]) {
let fieldsVisible :FieldBase<any>[] =[];
fields.forEach(element => {
if (element.viewStyle._renderstyle == "freetext"){
let newfield:FieldBase <any>;
let rule = new Rule();
newfield = new TextboxField({
label: element.title.__cdata,
key:element._id,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,
group : element._group,
description: element.description.__cdata
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "dropdown" ||element.viewStyle._renderstyle == "combobox"){//to choose one of these in xml
let newfield:DropdownField;
let rule = new Rule();
newfield = new DropdownField({
label: element.title.__cdata,
key:element._id,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,
description: element.description.__cdata,
group : element._group
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "checkbox"||element.viewStyle._renderstyle == "checkBox"){
let newfield:CheckBoxField;
let rule = new Array<Rule>();
newfield = new CheckBoxField({
label: element.title.__cdata,
key:element._id,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,
group : element._group,
description: element.description.__cdata,
type: "checkbox"
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "booleanDesicion"){
let newfield:RadioBoxField;
let rule = new Array<Rule>();
newfield = new RadioBoxField({
label: element.title.__cdata,
key:element._id,
value: element.value,
order:element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule: rule,
visible: element._defaultVisibility,
group : element._group,
type: "radio",
description: element.description.__cdata,
answers: [
{
id: 1,
answer: "Yes",
value: true
},
{
id: 2,
answer: "No",
value:false
}]
});
fieldsVisible.push(newfield);
}
});
fieldsVisible.sort((a, b) => a.order - b.order);
return fieldsVisible;
} }
private getGroups(fieldGroups:any, fields:any[]){
let groups :GroupBase<any>[] =[]; private buildFields(fields: any[]) {
let fieldsVisible: FieldBase<any>[] = [];
if(fieldGroups.length>1){ fields.forEach(element => {
fieldGroups.forEach(fieldGroup =>{ // each fiedgroup fills with its fields from json if (element.viewStyle._renderstyle == "freetext") {
let newfldGroup = new GroupBase(); let newfield: FieldBase<any>;
newfldGroup.groupFields = new Array(); let rule = new Rule();
newfield = new TextboxField({
label: element.title.__cdata,
key: element._id,
value: element.value,
order: element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule : rule,
visible: element._defaultVisibility,
group: element._group,
description: element.description.__cdata
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "dropdown" || element.viewStyle._renderstyle == "combobox") {//to choose one of these in xml
let newfield: DropdownField;
let rule = new Rule();
newfield = new DropdownField({
label: element.title.__cdata,
key: element._id,
value: element.value,
order: element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule : rule,
visible: element._defaultVisibility,
description: element.description.__cdata,
group: element._group
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "checkbox" || element.viewStyle._renderstyle == "checkBox") {
let newfield: CheckBoxField;
let rule = new Array<Rule>();
newfield = new CheckBoxField({
label: element.title.__cdata,
key: element._id,
value: element.value,
order: element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule : rule,
visible: element._defaultVisibility,
group: element._group,
description: element.description.__cdata,
type: "checkbox"
});
fieldsVisible.push(newfield);
}
else if (element.viewStyle._renderstyle == "booleanDesicion") {
let newfield: RadioBoxField;
let rule = new Array<Rule>();
newfield = new RadioBoxField({
label: element.title.__cdata,
key: element._id,
value: element.value,
order: element._ordinal,
rules: element.visible.rule != undefined ? element.visible.rule : rule,
visible: element._defaultVisibility,
group: element._group,
type: "radio",
description: element.description.__cdata,
answers: [
{
id: 1,
answer: "Yes",
value: true
},
{
id: 2,
answer: "No",
value: false
}]
});
fieldsVisible.push(newfield);
}
});
fieldsVisible.sort((a, b) => a.order - b.order);
return fieldsVisible;
}
private getGroups(fieldGroups: any, fields: any[]) {
let groups: GroupBase<any>[] = [];
if (fieldGroups.length > 1) {
fieldGroups.forEach(fieldGroup => { // each fiedgroup fills with its fields from json
let newfldGroup = new GroupBase();
newfldGroup.groupFields = new Array();
fields.forEach(field => { fields.forEach(field => {
if(fieldGroup._id == field.group){ if (fieldGroup._id == field.group) {
newfldGroup.groupFields.push(field); newfldGroup.groupFields.push(field);
}else{ } else {
//this.dataModel.fields.push(field); //this.dataModel.fields.push(field);
} }
}); });
newfldGroup.title = fieldGroup.title.__cdata; newfldGroup.title = fieldGroup.title.__cdata;
newfldGroup.key = fieldGroup._id; newfldGroup.key = fieldGroup._id;
@ -138,17 +141,17 @@ export class dataModelBuilder {
newfldGroup.style = fieldGroup.visible._style; newfldGroup.style = fieldGroup.visible._style;
newfldGroup.class = fieldGroup.visible._cssclass; newfldGroup.class = fieldGroup.visible._cssclass;
groups.push(newfldGroup) groups.push(newfldGroup)
}); });
} }
else{ else {
let newfldGroup = new GroupBase(); let newfldGroup = new GroupBase();
newfldGroup.groupFields = new Array(); newfldGroup.groupFields = new Array();
fields.forEach(field => { //for one fieldgroup, because xml to json transformation doesn't create array of one fieldfroup fields.forEach(field => { //for one fieldgroup, because xml to json transformation doesn't create array of one fieldfroup
if(fieldGroups._id == field.group){ if (fieldGroups._id == field.group) {
newfldGroup.groupFields.push(field); newfldGroup.groupFields.push(field);
}else } else
this.dataModel.fields.push(field); this.dataModel.fields.push(field);
}); });
newfldGroup.title = fieldGroups.title.__cdata; newfldGroup.title = fieldGroups.title.__cdata;
newfldGroup.key = fieldGroups._id; newfldGroup.key = fieldGroups._id;
@ -157,56 +160,56 @@ export class dataModelBuilder {
} }
return groups; return groups;
} }
private getDummyGroups(){
let groups :GroupBase<any>[] =[];
let group: GroupBase<any>;
group = new GroupBase<any>({
key: "adfgadfsg",
title: "GroupA",
rules: new Array(),
groupFields: new Array(),
value: "value",
visible: true,
order: 1
//,
//controlType: "none"
});
let newfield1:FieldBase <any>;
newfield1 = new TextboxField({
key:"tf1-455",
label: "Field from group1",
value: 'this is a groupfield',
required: true,
ordinal:1
});
group.groupFields.push(newfield1);
let newfield2:FieldBase <any>;
newfield2 = new TextboxField({
key:"tf1-443",
label: "Another field from group1",
value: 'this is a groupfield',
required: true,
ordinal:2
});
group.groupFields.push(newfield2);
groups.push(group);
return groups; private getDummyGroups() {
} let groups: GroupBase<any>[] = [];
private getFieldsAttributes(attributes:any, functions:any, fields:any[]){
let attribute:Attribute[]=[]; let group: GroupBase<any>;
group = new GroupBase<any>({
key: "adfgadfsg",
title: "GroupA",
rules: new Array(),
groupFields: new Array(),
value: "value",
visible: true,
order: 1
//,
//controlType: "none"
});
let newfield1: FieldBase<any>;
newfield1 = new TextboxField({
key: "tf1-455",
label: "Field from group1",
value: 'this is a groupfield',
required: true,
ordinal: 1
});
group.groupFields.push(newfield1);
let newfield2: FieldBase<any>;
newfield2 = new TextboxField({
key: "tf1-443",
label: "Another field from group1",
value: 'this is a groupfield',
required: true,
ordinal: 2
});
group.groupFields.push(newfield2);
groups.push(group);
return groups;
}
private getFieldsAttributes(attributes: any, functions: any, fields: any[]) {
let attribute: Attribute[] = [];
attributes.forEach(attr => { attributes.forEach(attr => {
let newAttribute = new Attribute(); let newAttribute = new Attribute();
newAttribute.datatype = attr._datatype; newAttribute.datatype = attr._datatype;
@ -215,65 +218,96 @@ export class dataModelBuilder {
newAttribute.multiplicityMax = attr.multiplicity._max; newAttribute.multiplicityMax = attr.multiplicity._max;
newAttribute.multiplicityMin = attr.multiplicity._min; newAttribute.multiplicityMin = attr.multiplicity._min;
newAttribute.ordinal = attr._ordinal; newAttribute.ordinal = attr._ordinal;
newAttribute.sources = new Array(); newAttribute.sources = new Array();
newAttribute.validation = new Array(); newAttribute.validation = new Array();
//newAttribute.validation.push(attr.validation.rule); //newAttribute.validation.push(attr.validation.rule);
if(attr.sources){ if (attr.sources) {
newAttribute.sources.push(attr.sources.source); newAttribute.sources.push(attr.sources.source);
newAttribute.sources.forEach(src => {
src.params=new Array(); if (attr.sources.source.url !== undefined) {
for (var i=0, len=attr.sources.source.value.length; i<len; i++){
let prm = new Param();
prm.key= attr.sources.source.value[i]._value;
prm.value= attr.sources.source.value[i]._label; fields.find(x => x.key == newAttribute.id).url.url = attr.sources.source.url._value;
src.params.push(prm);
} this.restBase.proxy_get(attr.sources.source.url._value).subscribe((data) => {
}); console.log(data);
newAttribute.sources.forEach(src => {
src.params = new Array();
data.data.forEach(data => {
let prm = new Param();
prm.key = data.id;
prm.value = data.attributes.name;
src.params.push(prm);
});
});
});
} else {
newAttribute.sources.forEach(src => {
src.params = new Array();
for (var i = 0, len = attr.sources.source.value.length; i < len; i++) {
let prm = new Param();
prm.key = attr.sources.source.value[i]._value;
prm.value = attr.sources.source.value[i]._label;
src.params.push(prm);
}
});
}
} }
if (attr.validation.rule.length) if (attr.validation.rule.length)
for (var i=0, len=attr.validation.rule.length; i<len; i++){ for (var i = 0, len = attr.validation.rule.length; i < len; i++) {
let rule = new Rule(); let rule = new Rule();
rule.ruleStyle= attr.validation.rule[i]._ruleStyle; rule.ruleStyle = attr.validation.rule[i]._ruleStyle;
rule.ruleType= attr.validation.rule[i]._type; rule.ruleType = attr.validation.rule[i]._type;
if (attr.validation.rule[i]._ruleStyle == "regex") if (attr.validation.rule[i]._ruleStyle == "regex")
rule.regex= attr.validation.rule[i].__cdata; rule.regex = attr.validation.rule[i].__cdata;
if (attr.validation.rule[i]._ruleStyle == "customValidation"){ if (attr.validation.rule[i]._ruleStyle == "customValidation") {
rule.method= attr.validation.rule[i]._method; rule.method = attr.validation.rule[i]._method;
functions.forEach(fnc => { functions.forEach(fnc => {
if(fnc._id == rule.method) if (fnc._id == rule.method)
rule.methodJs = fnc.script.__cdata; rule.methodJs = fnc.script.__cdata;
}); });
} }
newAttribute.validation.push(rule); newAttribute.validation.push(rule);
} }
else{ else {
if(attr.validation.rule){ if (attr.validation.rule) {
let rule = new Rule(); let rule = new Rule();
rule.ruleStyle= attr.validation.rule._ruleStyle; rule.ruleStyle = attr.validation.rule._ruleStyle;
rule.ruleType= attr.validation.rule._type; rule.ruleType = attr.validation.rule._type;
if (attr.validation.rule._ruleStyle == "regex") if (attr.validation.rule._ruleStyle == "regex")
rule.regex= attr.validation.rule.__cdata; rule.regex = attr.validation.rule.__cdata;
if (attr.validation.rule._ruleStyle == "customValidation"){ if (attr.validation.rule._ruleStyle == "customValidation") {
rule.method= attr.validation.rule._method; rule.method = attr.validation.rule._method;
functions.forEach(fnc => { functions.forEach(fnc => {
if(fnc._id == rule.method) if (fnc._id == rule.method)
rule.methodJs = fnc.__cdata; rule.methodJs = fnc.__cdata;
}); });
} }
newAttribute.validation.push(rule); newAttribute.validation.push(rule);
} }
} }
attribute.push(newAttribute); attribute.push(newAttribute);
fields.find(x => x.key == newAttribute.id).attributes.sources = newAttribute.sources; //if (fields.find(x => x.key == newAttribute.id).url.url == null)
fields.find(x => x.key == newAttribute.id).attributes.sources = newAttribute.sources;
fields.find(x => x.key == newAttribute.id).attributes.validation = newAttribute.validation; fields.find(x => x.key == newAttribute.id).attributes.validation = newAttribute.validation;
}); });
@ -281,10 +315,10 @@ export class dataModelBuilder {
return attribute; return attribute;
} }
private getSections(sections:any, fieldGroups:GroupBase<any>[]){ private getSections(sections: any, fieldGroups: GroupBase<any>[]) {
let sects: Section[]= []; let sects: Section[] = [];
if(sections.length){ if (sections.length) {
sections.forEach(section => { sections.forEach(section => {
let newSection = new Section(); let newSection = new Section();
newSection.defaultVisibility = section.defaultVisibility; newSection.defaultVisibility = section.defaultVisibility;
@ -294,7 +328,7 @@ export class dataModelBuilder {
newSection.ordinal = section._ordinal; newSection.ordinal = section._ordinal;
newSection.groupFields = new Array(); newSection.groupFields = new Array();
fieldGroups.forEach(fldgroup => { fieldGroups.forEach(fldgroup => {
if(fldgroup.section == newSection.id) if (fldgroup.section == newSection.id)
newSection.groupFields.push(fldgroup); newSection.groupFields.push(fldgroup);
}) })
sects.push(newSection); sects.push(newSection);
@ -303,5 +337,12 @@ export class dataModelBuilder {
sects.sort((a, b) => a.ordinal - b.ordinal); sects.sort((a, b) => a.ordinal - b.ordinal);
return sects; return sects;
} }
getValuesFromEestore(url: string, fieldPath: string) {
this.restBase.proxy_get(url).subscribe((data) => {
data.data.forEach(data => {
console.log(data);
});
});
}
} }