1 import {SchemaPropertyGroupModel, SchemaProperty} from '../aschema-property';
2 import { PROPERTY_DATA, PROPERTY_TYPES } from 'app/utils';
3 import { FilterPropertiesAssignmentData, PropertyBEModel, DerivedPropertyType, DerivedFEPropertyMap, DerivedFEProperty } from 'app/models';
6 export class PropertyFEModel extends PropertyBEModel {
8 expandedChildPropertyId: string;
9 flattenedChildren: Array<DerivedFEProperty>; //[parentPath] : Array<DerivedFEProp>
13 isSimpleType: boolean; //for convenience only - we can really just check if derivedDataType == derivedPropertyTypes.SIMPLE to know if the prop is simple
15 valueObj: any; //this is the only value we relate to in the html templates
16 derivedDataType: DerivedPropertyType;
18 constructor(property: PropertyBEModel){
20 this.isSimpleType = PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1;
21 this.setNonDeclared();
22 this.derivedDataType = this.getDerivedPropertyType();
23 this.flattenedChildren = [];
27 public getJSONValue = (): string => {
28 //If type is JSON, need to try parsing it before we stringify it so that it appears property in TOSCA - change per Bracha due to AMDOCS
29 //TODO: handle this.derivedDataType == DerivedPropertyType.MAP
30 if (this.derivedDataType == DerivedPropertyType.LIST && this.schema.property.type == PROPERTY_TYPES.JSON) {
32 return JSON.stringify(this.valueObj.map(item => JSON.parse(item)));
36 return (this.derivedDataType == DerivedPropertyType.SIMPLE) ? this.valueObj : JSON.stringify(this.valueObj);
39 public setNonDeclared = (childPath?: string): void => {
40 if (!childPath) { //declaring a child prop
41 this.isDeclared = false;
43 let childProp: DerivedFEProperty = this.flattenedChildren.find(child => child.propertiesName == childPath);
44 childProp.isDeclared = false;
48 public setAsDeclared = (childNameToDeclare?:string): void => {
49 if (!childNameToDeclare) { //declaring a child prop
50 this.isSelected = false;
51 this.isDeclared = true;
53 let childProp: DerivedFEProperty = this.flattenedChildren.find(child => child.propertiesName == childNameToDeclare);
54 childProp.isSelected = false;
55 childProp.isDeclared = true;
59 //For expand-collapse functionality - used within HTML template
60 public updateExpandedChildPropertyId = (childPropertyId: string): void => {
61 if (childPropertyId.lastIndexOf('#') > -1) {
62 this.expandedChildPropertyId = (this.expandedChildPropertyId == childPropertyId) ? (childPropertyId.substring(0, childPropertyId.lastIndexOf('#'))) : childPropertyId;
64 this.expandedChildPropertyId = this.name;
68 public getIndexOfChild = (childPropName: string): number => {
69 return this.flattenedChildren.findIndex(prop => prop.propertiesName.indexOf(childPropName) === 0);
72 public getCountOfChildren = (childPropName: string):number => {
73 let matchingChildren:Array<DerivedFEProperty> = this.flattenedChildren.filter(prop => prop.propertiesName.indexOf(childPropName) === 0) || [];
74 return matchingChildren.length;
77 // public getListIndexOfChild = (childPropName: string): number => { //gets list of siblings and then the index within that list
78 // this.flattenedChildren.filter(prop => prop.parentName == item.parentName).map(prop => prop.propertiesName).indexOf(item.propertiesName)
81 /* Updates parent valueObj when a child prop's value has changed */
82 public childPropUpdated = (childProp: DerivedFEProperty): void => {
83 let parentNames = this.getParentNamesArray(childProp.propertiesName, []);
84 if (parentNames.length) {
85 _.set(this.valueObj, parentNames.join('.'), childProp.valueObj);
89 /* Returns array of individual parents for given prop path, with list/map UUIDs replaced with index/mapkey */
90 private getParentNamesArray = (parentPropName: string, parentNames?: Array<string>): Array<string> => {
91 if (parentPropName.indexOf("#") == -1) { return parentNames; } //finished recursing parents. return
93 let parentProp: DerivedFEProperty = this.flattenedChildren.find(prop => prop.propertiesName === parentPropName);
94 let nameToInsert: string = parentProp.name;
96 if (parentProp.isChildOfListOrMap) {
97 if (parentProp.derivedDataType == DerivedPropertyType.MAP) {
98 nameToInsert = parentProp.mapKey;
100 let siblingProps = this.flattenedChildren.filter(prop => prop.parentName == parentProp.parentName).map(prop => prop.propertiesName);
101 nameToInsert = siblingProps.indexOf(parentProp.propertiesName).toString();
105 parentNames.splice(0, 0, nameToInsert); //add prop name to array
106 return this.getParentNamesArray(parentProp.parentName, parentNames); //continue recursing