2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import * as _ from "lodash";
23 import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute";
24 import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property";
26 export class AttributesGroup {
27 constructor(attributesObj?:AttributesGroup) {
28 _.forEach(attributesObj, (attributes:Array<AttributeModel>, instance) => {
30 _.forEach(attributes, (attribute:AttributeModel):void => {
31 attribute.resourceInstanceUniqueId = instance;
32 attribute.readonly = true;
33 this[instance].push(new AttributeModel(attribute));
39 export interface IAttributeModel {
47 schema:SchemaAttributeGroupModel;
51 parentUniqueId:string;
53 resourceInstanceUniqueId:string;
55 valueUniqueUid:string;
58 export class AttributeModel implements IAttributeModel {
66 schema:SchemaAttributeGroupModel;
70 parentUniqueId:string;
72 resourceInstanceUniqueId:string;
74 valueUniqueUid:string;
76 constructor(attribute?:AttributeModel) {
78 this.uniqueId = attribute.uniqueId;
79 this.name = attribute.name;
80 this.defaultValue = attribute.defaultValue;
81 this.description = attribute.description;
82 this.type = attribute.type;
83 this.status = attribute.status;
84 this.schema = attribute.schema;
85 this.value = attribute.value;
86 this.hidden = attribute.hidden;
87 this.parentUniqueId = attribute.parentUniqueId;
88 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
89 this.readonly = attribute.readonly;
90 this.valueUniqueUid = attribute.valueUniqueUid;
93 if (!this.schema || !this.schema.property) {
94 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
96 //forcing creating new object, so editing different one than the object in the table
97 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
100 this.convertValueToView();
103 public convertToServerObject:Function = ():string => {
104 if (this.defaultValue && this.type === 'map') {
105 this.defaultValue = '{' + this.defaultValue + '}';
107 if (this.defaultValue && this.type === 'list') {
108 this.defaultValue = '[' + this.defaultValue + ']';
110 this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null;
112 return JSON.stringify(this);
116 public convertValueToView() {
117 //unwrapping value {} or [] if type is complex
118 if (this.defaultValue && (this.type === 'map' || this.type === 'list') &&
119 ['[', '{'].indexOf(this.defaultValue.charAt(0)) > -1 &&
120 [']', '}'].indexOf(this.defaultValue.slice(-1)) > -1) {
121 this.defaultValue = this.defaultValue.slice(1, -1);
124 //also for value - for the modal in canvas
125 if (this.value && (this.type === 'map' || this.type === 'list') &&
126 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
127 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
128 this.value = this.value.slice(1, -1);
132 public toJSON = ():any => {
133 if (!this.resourceInstanceUniqueId) {
134 this.value = undefined;
136 this.readonly = undefined;
137 this.resourceInstanceUniqueId = undefined;