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; |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 44 | _default:string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 45 | description:string; |
| 46 | type:string; |
| 47 | schema:SchemaAttributeGroupModel; |
| 48 | status:string; |
| 49 | value:string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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; |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 62 | _default:string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 63 | description:string; |
| 64 | type:string; |
| 65 | schema:SchemaAttributeGroupModel; |
| 66 | status:string; |
| 67 | value:string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 68 | parentUniqueId:string; |
| 69 | //custom data |
| 70 | resourceInstanceUniqueId:string; |
| 71 | readonly:boolean; |
| 72 | valueUniqueUid:string; |
| 73 | |
| 74 | constructor(attribute?:AttributeModel) { |
| 75 | if (attribute) { |
| 76 | this.uniqueId = attribute.uniqueId; |
| 77 | this.name = attribute.name; |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 78 | this._default = attribute._default; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 84 | this.parentUniqueId = attribute.parentUniqueId; |
| 85 | this.resourceInstanceUniqueId = attribute.resourceInstanceUniqueId; |
| 86 | this.readonly = attribute.readonly; |
| 87 | this.valueUniqueUid = attribute.valueUniqueUid; |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 88 | } else { |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 89 | this._default = ''; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 102 | public convertToServerObject():string { |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 103 | if (this._default && this.type === 'map') { |
| 104 | this._default = '{' + this._default + '}'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 105 | } |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 106 | if (this._default && this.type === 'list') { |
| 107 | this._default = '[' + this._default + ']'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 108 | } |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 109 | this._default = this._default != "" && this._default != "[]" && this._default != "{}" ? this._default : null; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 110 | |
| 111 | return JSON.stringify(this); |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | public convertValueToView() { |
| 116 | //unwrapping value {} or [] if type is complex |
aribeiro | 2f74a4a | 2020-11-17 10:52:41 +0000 | [diff] [blame] | 117 | if (this._default && (this.type === 'map' || this.type === 'list') && |
| 118 | ['[', '{'].indexOf(this._default.charAt(0)) > -1 && |
| 119 | [']', '}'].indexOf(this._default.slice(-1)) > -1) { |
| 120 | this._default = this._default.slice(1, -1); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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 | } |