Sync Integ to Master
[sdc.git] / catalog-ui / src / app / models / properties-inputs / input-fe-model.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 {PropertyFEModel} from "../../models";
24 import {PROPERTY_DATA} from "../../utils/constants";
25 import {InputBEModel} from "./input-be-model";
26 import {DerivedPropertyType} from "./property-be-model";
27
28 export class InputFEModel extends InputBEModel {
29     isSimpleType: boolean;
30     relatedPropertyValue: any;
31     relatedPropertyName: string;
32     defaultValueObj:any;
33     defaultValueObjIsValid:boolean;
34     defaultValueObjOrig:any;
35     defaultValueObjIsChanged:boolean;
36     derivedDataType: DerivedPropertyType;
37
38     constructor(input?: InputBEModel) {
39         super(input);
40         if (input) {
41             this.isSimpleType = PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1;
42             let relatedProperty = input.properties && input.properties[0] || input.inputs && input.inputs[0];
43             if (relatedProperty) {
44                 this.relatedPropertyValue = relatedProperty.value;
45                 this.relatedPropertyName = relatedProperty.name;
46             }
47             this.derivedDataType = this.getDerivedPropertyType();
48             this.resetDefaultValueObjValidation();
49             this.updateDefaultValueObjOrig();
50         }
51     }
52
53     public updateDefaultValueObj(defaultValueObj:any, isValid:boolean) {
54         this.defaultValueObj = PropertyFEModel.cleanValueObj(defaultValueObj);
55         this.defaultValueObjIsValid = isValid;
56         this.defaultValueObjIsChanged = this.hasDefaultValueChanged();
57     }
58
59     public updateDefaultValueObjOrig() {
60         this.defaultValueObjOrig = _.cloneDeep(this.defaultValueObj);
61         this.defaultValueObjIsChanged = false;
62     }
63
64     public getJSONDefaultValue(): string {
65         return PropertyFEModel.stringifyValueObj(this.defaultValueObj, this.schema.property.type, this.derivedDataType);
66     }
67
68     public getDefaultValueObj(): any {
69         return PropertyFEModel.parseValueObj(this.defaultValue, this.type, this.derivedDataType);
70     }
71
72     public resetDefaultValueObjValidation() {
73         this.defaultValueObjIsValid = true;
74     }
75
76     hasDefaultValueChanged(): boolean {
77         return !_.isEqual(this.defaultValueObj, this.defaultValueObjOrig);
78     }
79
80 }