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; |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 91 | } else { |
| 92 | this.defaultValue = ''; |
| 93 | this.hidden = false; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (!this.schema || !this.schema.property) { |
| 97 | this.schema = new SchemaPropertyGroupModel(new SchemaProperty()); |
| 98 | } else { |
| 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)); |
| 101 | } |
| 102 | |
| 103 | this.convertValueToView(); |
| 104 | } |
| 105 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 106 | public convertToServerObject():string { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 107 | if (this.defaultValue && this.type === 'map') { |
| 108 | this.defaultValue = '{' + this.defaultValue + '}'; |
| 109 | } |
| 110 | if (this.defaultValue && this.type === 'list') { |
| 111 | this.defaultValue = '[' + this.defaultValue + ']'; |
| 112 | } |
| 113 | this.defaultValue = this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null; |
| 114 | |
| 115 | return JSON.stringify(this); |
| 116 | }; |
| 117 | |
| 118 | |
| 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); |
| 125 | } |
| 126 | |
| 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); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public toJSON = ():any => { |
| 136 | if (!this.resourceInstanceUniqueId) { |
| 137 | this.value = undefined; |
| 138 | } |
| 139 | this.readonly = undefined; |
| 140 | this.resourceInstanceUniqueId = undefined; |
| 141 | return this; |
| 142 | }; |
| 143 | } |