Fix bugs in attribute outputs page
[sdc.git] / catalog-ui / src / app / models / attributes.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 'use strict';
22 import { AttributeBEModel } from 'app/models/attributes-outputs/attribute-be-model';
23 import { AttributeOutputDetail } from 'app/models/attributes-outputs/attribute-output-detail';
24 import * as _ from 'lodash';
25 import { SchemaAttribute, SchemaAttributeGroupModel } from './schema-attribute';
26
27 export class AttributesGroup {
28   constructor(attributesObj?: AttributesGroup) {
29     _.forEach(attributesObj, (attributes: AttributeModel[], instance) => {
30       this[instance] = [];
31       _.forEach(attributes, (attribute: AttributeModel): void => {
32         attribute.resourceInstanceUniqueId = instance;
33         attribute.readonly = true;
34         this[instance].push(new AttributeModel(attribute));
35       });
36     });
37   }
38 }
39
40 export interface IAttributeModel {
41
42   // server data
43   uniqueId: string;
44   name: string;
45   _default: string;
46   description: string;
47   type: string;
48   schema: SchemaAttributeGroupModel;
49   status: string;
50   value: string;
51   parentUniqueId: string;
52   // custom data
53   resourceInstanceUniqueId: string;
54   readonly: boolean;
55   valueUniqueUid: string;
56 }
57
58 export class AttributeModel extends AttributeBEModel implements IAttributeModel {
59
60   // server data
61   uniqueId: string;
62   name: string;
63   _default: string;
64   description: string;
65   type: string;
66   schema: SchemaAttributeGroupModel;
67   status: string;
68   value: string;
69   parentUniqueId: string;
70   // custom data
71   resourceInstanceUniqueId: string;
72   readonly: boolean;
73   valueUniqueUid: string;
74
75   getOutputValues: AttributeOutputDetail[];
76   subAttributeOutputPath: string;
77   outputPath: string;
78
79   constructor(attribute?: AttributeModel) {
80     super(attribute);
81     if (attribute) {
82       this.uniqueId = attribute.uniqueId;
83       this.name = attribute.name;
84       this._default = attribute._default;
85       this.description = attribute.description;
86       this.type = attribute.type;
87       this.status = attribute.status;
88       this.schema = attribute.schema;
89       this.value = attribute.value;
90       this.parentUniqueId = attribute.parentUniqueId;
91       this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
92       this.readonly = attribute.readonly;
93       this.valueUniqueUid = attribute.valueUniqueUid;
94
95       this.getOutputValues = attribute.getOutputValues;
96       this.subAttributeOutputPath = attribute.subAttributeOutputPath;
97       this.outputPath = attribute.outputPath;
98     } else {
99       this._default = '';
100     }
101
102     if (!this.schema || !this.schema.property) {
103       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute());
104     } else {
105       // forcing creating new object, so editing different one than the object in the table
106       this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
107     }
108   }
109
110   public convertToServerObject(): string {
111     if (this._default && this.type === 'map') {
112       this._default = '{' + this._default + '}';
113     }
114     if (this._default && this.type === 'list') {
115       this._default = '[' + this._default + ']';
116     }
117     this._default = this._default != '' && this._default != '[]' && this._default != '{}' ? this._default : null;
118
119     return JSON.stringify(this);
120   }
121
122   public convertValueToView() {
123     // unwrapping value {} or [] if type is complex
124     if (this._default && (this.type === 'map' || this.type === 'list') &&
125         ['[', '{'].indexOf(this._default.charAt(0)) > -1 &&
126         [']', '}'].indexOf(this._default.slice(-1)) > -1) {
127       this._default = this._default.slice(1, -1);
128     }
129
130     // also for value - for the modal in canvas
131     if (this.value && (this.type === 'map' || this.type === 'list') &&
132         ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
133         [']', '}'].indexOf(this.value.slice(-1)) > -1) {
134       this.value = this.value.slice(1, -1);
135     }
136   }
137
138   public toJSON = (): any => {
139     if (!this.resourceInstanceUniqueId) {
140       this.value = undefined;
141     }
142     this.readonly = undefined;
143     this.resourceInstanceUniqueId = undefined;
144
145     return this;
146   }
147 }