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