2d406e210b617f29493525892be7311a0547c643
[sdc.git] / catalog-ui / src / app / models / properties-inputs / derived-fe-property.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import * as _ from "lodash";
22 import { SchemaPropertyGroupModel, SchemaProperty } from '../schema-property';
23 import { DerivedPropertyType, PropertyBEModel, PropertyFEModel } from '../../models';
24 import { PROPERTY_TYPES } from 'app/utils';
25 import { UUID } from "angular2-uuid";
26
27
28 export class DerivedFEProperty extends PropertyBEModel {
29     valueObj: any;
30     valueObjIsValid: boolean;
31     valueObjOrig: any;
32     valueObjIsChanged: boolean;
33     parentName: string;
34     propertiesName: string; //"network_assignments#ipv4_subnet#use_ipv4 =  parentPath + name
35     derivedDataType: DerivedPropertyType;
36     isDeclared: boolean;
37     isSelected: boolean;
38     isDisabled: boolean;
39     hidden: boolean;
40     isChildOfListOrMap: boolean;
41     canBeDeclared: boolean;
42     mapKey: string;
43     mapKeyError: string;
44     mapInlist: boolean
45
46     constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
47         if (!createChildOfListOrMap) { //creating a standard derived prop
48             super(property);
49             this.parentName = parentName ? parentName : null;
50             this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
51             this.canBeDeclared = true; //defaults to true
52         } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
53             super(null);
54             this.isChildOfListOrMap = true;
55             this.canBeDeclared = false;
56             this.name = UUID.UUID();
57             this.parentName = parentName;
58             this.propertiesName = parentName + '#' + this.name;
59             
60             if (property.type == PROPERTY_TYPES.LIST) {
61                 this.mapKey = property.schema.property.type.split('.').pop();
62                 this.mapKeyError = null;
63                 this.type = property.schema.property.type;
64                 if (this.type == PROPERTY_TYPES.MAP){
65                     this.mapInlist = true;
66                 }
67
68                 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
69             } else { //map
70                 if (key) {
71                     this.mapKey = key;
72                     this.mapKeyError = null;
73                 } else {
74                     this.mapKey = '';
75                     this.mapKeyError = 'Key cannot be empty.';
76                 }
77                 this.type = property.type;
78                 if (property.schema.property.type == PROPERTY_TYPES.MAP){
79                     const schProp = new SchemaProperty();
80                     schProp.isSimpleType = true;
81                     schProp.isDataType = false;
82                     schProp.simpleType = PROPERTY_TYPES.STRING;
83                     this.schema = new SchemaPropertyGroupModel(schProp);
84                     this.schemaType = PROPERTY_TYPES.STRING;
85                 } else {
86                     this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
87                 }
88         
89             }
90             this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
91             this.updateValueObjOrig();
92         }
93         // this.constraints = property ? property.constraints : null;
94         this.valueObjIsValid = true;
95         this.derivedDataType = this.getDerivedPropertyType();
96     }
97
98     public getActualMapKey() {
99         return (this.mapKeyError) ? this.name : this.mapKey;
100     }
101
102     public updateValueObj(valueObj:any, isValid:boolean) {
103         this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
104         this.valueObjIsValid = isValid;
105         this.valueObjIsChanged = this.hasValueObjChanged();
106     }
107
108     public updateValueObjOrig() {
109         this.valueObjOrig = _.cloneDeep(this.valueObj);
110         this.valueObjIsChanged = false;
111     }
112
113     public hasValueObjChanged() {
114         return !_.isEqual(this.valueObj, this.valueObjOrig);
115     }
116
117 }
118 export class DerivedFEPropertyMap {
119     [parentPath: string]: Array<DerivedFEProperty>;
120 }
121