blob: f2562e98bfb3463615fa74046b4f3c7bd98be261 [file] [log] [blame]
Michael Landoed64b5e2017-06-09 03:19:04 +03001'use strict';
2import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute";
3import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property";
4
5export class AttributesGroup {
6 constructor(attributesObj?:AttributesGroup) {
7 _.forEach(attributesObj, (attributes:Array<AttributeModel>, instance) => {
8 this[instance] = [];
9 _.forEach(attributes, (attribute:AttributeModel):void => {
10 attribute.resourceInstanceUniqueId = instance;
11 attribute.readonly = true;
12 this[instance].push(new AttributeModel(attribute));
13 });
14 });
15 }
16}
17
18export interface IAttributeModel {
19
20 //server data
21 uniqueId:string;
22 name:string;
23 defaultValue:string;
24 description:string;
25 type:string;
26 schema:SchemaAttributeGroupModel;
27 status:string;
28 value:string;
29 hidden:boolean;
30 parentUniqueId:string;
31 //custom data
32 resourceInstanceUniqueId:string;
33 readonly:boolean;
34 valueUniqueUid:string;
35}
36
37export class AttributeModel implements IAttributeModel {
38
39 //server data
40 uniqueId:string;
41 name:string;
42 defaultValue:string;
43 description:string;
44 type:string;
45 schema: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 constructor(attribute?:AttributeModel) {
56 if (attribute) {
57 this.uniqueId = attribute.uniqueId;
58 this.name = attribute.name;
59 this.defaultValue = attribute.defaultValue;
60 this.description = attribute.description;
61 this.type = attribute.type;
62 this.status = attribute.status;
63 this.schema = attribute.schema;
64 this.value = attribute.value;
65 this.hidden = attribute.hidden;
66 this.parentUniqueId = attribute.parentUniqueId;
67 this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId;
68 this.readonly = attribute.readonly;
69 this.valueUniqueUid = attribute.valueUniqueUid;
70 }
71
72 if (!this.schema || !this.schema.property) {
73 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
74 } else {
75 //forcing creating new object, so editing different one than the object in the table
76 this.schema = new SchemaAttributeGroupModel(new SchemaAttribute(this.schema.property));
77 }
78
79 this.convertValueToView();
80 }
81
82 public convertToServerObject:Function = ():string => {
83 if (this.defaultValue && this.type === 'map') {
84 this.defaultValue = '{' + this.defaultValue + '}';
85 }
86 if (this.defaultValue && this.type === 'list') {
87 this.defaultValue = '[' + this.defaultValue + ']';
88 }
89 this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null;
90
91 return JSON.stringify(this);
92 };
93
94
95 public convertValueToView() {
96 //unwrapping value {} or [] if type is complex
97 if (this.defaultValue && (this.type === 'map' || this.type === 'list') &&
98 ['[', '{'].indexOf(this.defaultValue.charAt(0)) > -1 &&
99 [']', '}'].indexOf(this.defaultValue.slice(-1)) > -1) {
100 this.defaultValue = this.defaultValue.slice(1, -1);
101 }
102
103 //also for value - for the modal in canvas
104 if (this.value && (this.type === 'map' || this.type === 'list') &&
105 ['[', '{'].indexOf(this.value.charAt(0)) > -1 &&
106 [']', '}'].indexOf(this.value.slice(-1)) > -1) {
107 this.value = this.value.slice(1, -1);
108 }
109 }
110
111 public toJSON = ():any => {
112 if (!this.resourceInstanceUniqueId) {
113 this.value = undefined;
114 }
115 this.readonly = undefined;
116 this.resourceInstanceUniqueId = undefined;
117 return this;
118 };
119}