8806562d06fd5fbec2f8ac06f376356c6130c41a
[sdc.git] / catalog-ui / src / app / models / attributes-outputs / output-fe-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 Nordix Foundation. 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 {PROPERTY_DATA} from "../../utils/constants";
23 import {OutputBEModel} from "./output-be-model";
24 import {AttributeFEModel} from "./attribute-fe-model";
25 import {DerivedAttributeType} from "./attribute-be-model";
26
27 export class OutputFEModel extends OutputBEModel {
28   isSimpleType: boolean;
29   relatedAttributeValue: any;
30   relatedAttributeName: string;
31   defaultValueObj: any;
32   defaultValueObjIsValid: boolean;
33   defaultValueObjOrig: any;
34   defaultValueObjIsChanged: boolean;
35   derivedDataType: DerivedAttributeType;
36
37   constructor(output?: OutputBEModel) {
38     super(output);
39     if (output) {
40       this.isSimpleType = PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1;
41       let relatedAttribute = output.attributes && output.attributes[0] || output.outputs && output.outputs[0];
42       if (relatedAttribute) {
43         this.relatedAttributeValue = relatedAttribute.value;
44         this.relatedAttributeName = relatedAttribute.name;
45       }
46       this.derivedDataType = this.getDerivedAttributeType();
47       this.resetDefaultValueObjValidation();
48       this.updateDefaultValueObjOrig();
49     }
50   }
51
52   public updateDefaultValueObjOrig() {
53     this.defaultValueObjOrig = _.cloneDeep(this.defaultValueObj);
54     this.defaultValueObjIsChanged = false;
55   }
56
57   public getJSONDefaultValue(): string {
58     return AttributeFEModel.stringifyValueObj(this.defaultValueObj, this.schema.property.type, this.derivedDataType);
59   }
60
61   public getDefaultValueObj(): any {
62     return AttributeFEModel.parseValueObj(this.defaultValueObj, this.type, this.derivedDataType);
63   }
64
65   public resetDefaultValueObjValidation() {
66     this.defaultValueObjIsValid = true;
67   }
68
69   hasDefaultValueChanged(): boolean {
70     return !_.isEqual(this.defaultValueObj, this.defaultValueObjOrig);
71   }
72
73   hasChanged(): boolean {
74     return this.hasDefaultValueChanged() ;
75   }
76 }