blob: ea4c7a5a23b72988f02a4196da687e154ae1ad46 [file] [log] [blame]
Michael Lando451a3402017-02-19 10:28:42 +02001/*-
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/// <reference path="../references"/>
21module Sdc.Models {
22 'use strict';
23
24 export class AttributesGroup {
25 constructor(attributesObj?:Models.AttributesGroup) {
26 _.forEach(attributesObj, (attributes:Array<Models.AttributeModel>, instance) => {
27 this[instance] = [];
28 _.forEach(attributes, (attribute:Models.AttributeModel):void => {
29 attribute.resourceInstanceUniqueId = instance;
30 attribute.readonly = true;
31 this[instance].push(new Models.AttributeModel(attribute));
32 });
33 });
34 }
35 }
36
37 export interface IAttributeModel {
38
39 //server data
40 uniqueId:string;
41 name:string;
42 defaultValue:string;
43 description:string;
44 type:string;
45 schema:Models.SchemaAttributeGroupModel;
46 status:string;
47 value:string;
48 hidden:boolean;
49 parentUniqueId:string;
50 //custom data
51 resourceInstanceUniqueId:string;
52 readonly:boolean;
53 valueUniqueUid:string;
54 }
55
56 export class AttributeModel implements IAttributeModel {
57
58 //server data
59 uniqueId:string;
60 name:string;
61 defaultValue:string;
62 description:string;
63 type:string;
64 schema:Models.SchemaAttributeGroupModel;
65 status:string;
66 value:string;
67 hidden:boolean;
68 parentUniqueId:string;
69 //custom data
70 resourceInstanceUniqueId:string;
71 readonly:boolean;
72 valueUniqueUid:string;
73
74 constructor(attribute?:Models.AttributeModel) {
75 if (attribute) {
76 this.uniqueId = attribute.uniqueId;
77 this.name = attribute.name;
78 this.defaultValue = attribute.defaultValue;
79 this.description = attribute.description;
80 this.type = attribute.type;
81 this.status = attribute.status;
82 this.schema = attribute.schema;
83 this.value = attribute.value;
84 this.hidden = attribute.hidden;
85 this.parentUniqueId = attribute.parentUniqueId;
86 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
87 this.readonly = attribute.readonly;
88 this.valueUniqueUid = attribute.valueUniqueUid;
89 }
90
91 if (!this.schema || !this.schema.property) {
92 this.schema = new Models.SchemaPropertyGroupModel(new Models.SchemaProperty());
93 } else {
94 //forcing creating new object, so editing different one than the object in the table
95 this.schema = new Models.SchemaAttributeGroupModel(new Models.SchemaAttribute(this.schema.property));
96 }
97
98 this.convertValueToView();
99 }
100
101 public convertToServerObject:Function = ():string => {
102 if (this.defaultValue && this.type === 'map') {
103 this.defaultValue = '{' + this.defaultValue + '}';
104 }
105 if (this.defaultValue && this.type === 'list') {
106 this.defaultValue = '[' + this.defaultValue + ']';
107 }
108 this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null;
109
110 return JSON.stringify(this);
111 };
112
113
114 public convertValueToView() {
115 //unwrapping value {} or [] if type is complex
116 if (this.defaultValue && (this.type === 'map' || this.type === 'list') &&
117 ['[', '{'].indexOf(this.defaultValue.charAt(0)) > -1 &&
118 [']', '}'].indexOf(this.defaultValue.slice(-1)) > -1) {
119 this.defaultValue = this.defaultValue.slice(1, -1);
120 }
121
122 //also for value - for the modal in canvas
123 if (this.value && (this.type === 'map' || this.type === 'list') &&
124 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
125 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
126 this.value = this.value.slice(1, -1);
127 }
128 }
129
130 public toJSON = ():any => {
131 if (!this.resourceInstanceUniqueId) {
132 this.value = undefined;
133 }
134 this.readonly = undefined;
135 this.resourceInstanceUniqueId = undefined;
136 return this;
137 };
138 }
139}