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'; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame^] | 22 | import * as _ from "lodash"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 23 | import {SchemaAttributeGroupModel, SchemaAttribute} from "./schema-attribute"; |
| 24 | import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property"; |
| 25 | |
| 26 | export class AttributesGroup { |
| 27 | constructor(attributesObj?:AttributesGroup) { |
| 28 | _.forEach(attributesObj, (attributes:Array<AttributeModel>, instance) => { |
| 29 | this[instance] = []; |
| 30 | _.forEach(attributes, (attribute:AttributeModel):void => { |
| 31 | attribute.resourceInstanceUniqueId = instance; |
| 32 | attribute.readonly = true; |
| 33 | this[instance].push(new AttributeModel(attribute)); |
| 34 | }); |
| 35 | }); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export interface IAttributeModel { |
| 40 | |
| 41 | //server data |
| 42 | uniqueId:string; |
| 43 | name:string; |
| 44 | defaultValue:string; |
| 45 | description:string; |
| 46 | type:string; |
| 47 | schema:SchemaAttributeGroupModel; |
| 48 | status:string; |
| 49 | value:string; |
| 50 | hidden:boolean; |
| 51 | parentUniqueId:string; |
| 52 | //custom data |
| 53 | resourceInstanceUniqueId:string; |
| 54 | readonly:boolean; |
| 55 | valueUniqueUid:string; |
| 56 | } |
| 57 | |
| 58 | export class AttributeModel implements IAttributeModel { |
| 59 | |
| 60 | //server data |
| 61 | uniqueId:string; |
| 62 | name:string; |
| 63 | defaultValue:string; |
| 64 | description:string; |
| 65 | type:string; |
| 66 | schema:SchemaAttributeGroupModel; |
| 67 | status:string; |
| 68 | value:string; |
| 69 | hidden:boolean; |
| 70 | parentUniqueId:string; |
| 71 | //custom data |
| 72 | resourceInstanceUniqueId:string; |
| 73 | readonly:boolean; |
| 74 | valueUniqueUid:string; |
| 75 | |
| 76 | constructor(attribute?:AttributeModel) { |
| 77 | if (attribute) { |
| 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; |
| 91 | } |
| 92 | |
| 93 | if (!this.schema || !this.schema.property) { |
| 94 | this.schema = new SchemaPropertyGroupModel(new SchemaProperty()); |
| 95 | } else { |
| 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)); |
| 98 | } |
| 99 | |
| 100 | this.convertValueToView(); |
| 101 | } |
| 102 | |
| 103 | public convertToServerObject:Function = ():string => { |
| 104 | if (this.defaultValue && this.type === 'map') { |
| 105 | this.defaultValue = '{' + this.defaultValue + '}'; |
| 106 | } |
| 107 | if (this.defaultValue && this.type === 'list') { |
| 108 | this.defaultValue = '[' + this.defaultValue + ']'; |
| 109 | } |
| 110 | this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null; |
| 111 | |
| 112 | return JSON.stringify(this); |
| 113 | }; |
| 114 | |
| 115 | |
| 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); |
| 122 | } |
| 123 | |
| 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); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public toJSON = ():any => { |
| 133 | if (!this.resourceInstanceUniqueId) { |
| 134 | this.value = undefined; |
| 135 | } |
| 136 | this.readonly = undefined; |
| 137 | this.resourceInstanceUniqueId = undefined; |
| 138 | return this; |
| 139 | }; |
| 140 | } |