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;
92 this.defaultValue = '';
96 if (!this.schema || !this.schema.property) {
97 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
99 //forcing creating new object, so editing different one than the object in the table
100 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
103 this.convertValueToView();
106 public convertToServerObject():string {
107 if (this.defaultValue && this.type === 'map') {
108 this.defaultValue = '{' + this.defaultValue + '}';
110 if (this.defaultValue && this.type === 'list') {
111 this.defaultValue = '[' + this.defaultValue + ']';
113 this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null;
115 return JSON.stringify(this);
119 public convertValueToView() {
120 //unwrapping value {} or [] if type is complex
121 if (this.defaultValue && (this.type === 'map' || this.type === 'list') &&
122 ['[', '{'].indexOf(this.defaultValue.charAt(0)) > -1 &&
123 [']', '}'].indexOf(this.defaultValue.slice(-1)) > -1) {
124 this.defaultValue = this.defaultValue.slice(1, -1);
127 //also for value - for the modal in canvas
128 if (this.value && (this.type === 'map' || this.type === 'list') &&
129 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
130 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
131 this.value = this.value.slice(1, -1);
135 public toJSON = ():any => {
136 if (!this.resourceInstanceUniqueId) {
137 this.value = undefined;
139 this.readonly = undefined;
140 this.resourceInstanceUniqueId = undefined;