Catalog alignment
[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 '../aschema-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
45     constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
46         if (!createChildOfListOrMap) { //creating a standard derived prop
47             super(property);
48             this.parentName = parentName ? parentName : null;
49             this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
50             this.canBeDeclared = true; //defaults to true
51         } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
52             super(null);
53             this.isChildOfListOrMap = true;
54             this.canBeDeclared = false;
55             this.name = UUID.UUID();
56             this.parentName = parentName;
57             this.propertiesName = parentName + '#' + this.name;
58             
59             if (property.type == PROPERTY_TYPES.LIST) {
60                 this.mapKey = property.schema.property.type.split('.').pop();
61                 this.mapKeyError = null;
62                 this.type = property.schema.property.type;
63             } else { //map
64                 if (key) {
65                     this.mapKey = key;
66                     this.mapKeyError = null;
67                 } else {
68                     this.mapKey = '';
69                     this.mapKeyError = 'Key cannot be empty.';
70                 }
71                 this.type = property.type;
72             }
73             this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
74             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
75             this.updateValueObjOrig();
76         }
77         // this.constraints = property ? property.constraints : null;
78         this.valueObjIsValid = true;
79         this.derivedDataType = this.getDerivedPropertyType();
80     }
81
82     public getActualMapKey() {
83         return (this.mapKeyError) ? this.name : this.mapKey;
84     }
85
86     public updateValueObj(valueObj:any, isValid:boolean) {
87         this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
88         this.valueObjIsValid = isValid;
89         this.valueObjIsChanged = this.hasValueObjChanged();
90     }
91
92     public updateValueObjOrig() {
93         this.valueObjOrig = _.cloneDeep(this.valueObj);
94         this.valueObjIsChanged = false;
95     }
96
97     public hasValueObjChanged() {
98         return !_.isEqual(this.valueObj, this.valueObjOrig);
99     }
100 }
101 export class DerivedFEPropertyMap {
102     [parentPath: string]: Array<DerivedFEProperty>;
103 }
104