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