[SDC-29] rebase continue work to align source
[sdc.git] / catalog-ui / src / app / models / properties-inputs / property-fe-model.ts
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';
4
5
6 export class PropertyFEModel extends PropertyBEModel {
7
8     expandedChildPropertyId: string;
9     flattenedChildren:  Array<DerivedFEProperty>; //[parentPath] : Array<DerivedFEProp>
10     isDeclared: boolean;
11     isDisabled: boolean;
12     isSelected: boolean;
13     isSimpleType: boolean; //for convenience only - we can really just check if derivedDataType == derivedPropertyTypes.SIMPLE to know if the prop is simple
14     uniqueId: string;
15     valueObj: any; //this is the only value we relate to in the html templates
16     derivedDataType: DerivedPropertyType;
17
18     constructor(property: PropertyBEModel){
19         super(property);
20         this.isSimpleType = PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1;
21         this.setNonDeclared();
22         this.derivedDataType = this.getDerivedPropertyType();
23         this.flattenedChildren = [];
24     }
25
26
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) {
31             try {
32                 return JSON.stringify(this.valueObj.map(item => JSON.parse(item)));
33             } catch (e){}
34         }
35
36         return (this.derivedDataType == DerivedPropertyType.SIMPLE) ? this.valueObj : JSON.stringify(this.valueObj);     
37     }
38
39     public setNonDeclared = (childPath?: string): void => {
40         if (!childPath) { //declaring a child prop
41             this.isDeclared = false;
42         } else {
43             let childProp: DerivedFEProperty = this.flattenedChildren.find(child => child.propertiesName == childPath);
44             childProp.isDeclared = false;
45         }
46     }
47
48     public setAsDeclared = (childNameToDeclare?:string): void => {
49         if (!childNameToDeclare) { //declaring a child prop
50             this.isSelected = false;
51             this.isDeclared = true;
52         } else {
53             let childProp: DerivedFEProperty = this.flattenedChildren.find(child => child.propertiesName == childNameToDeclare);
54             childProp.isSelected = false;
55             childProp.isDeclared = true;
56         }
57     }
58
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;
63         } else {
64             this.expandedChildPropertyId = this.name;
65         }
66     }
67
68     public getIndexOfChild = (childPropName: string): number => {
69         return this.flattenedChildren.findIndex(prop => prop.propertiesName.indexOf(childPropName) === 0);
70     }
71
72     public getCountOfChildren = (childPropName: string):number => {
73         let matchingChildren:Array<DerivedFEProperty> = this.flattenedChildren.filter(prop => prop.propertiesName.indexOf(childPropName) === 0) || [];
74         return matchingChildren.length;
75     }
76
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)
79     // }
80
81 }