Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | 'use strict'; |
| 2 | import {SchemaPropertyGroupModel, SchemaProperty} from "./aschema-property"; |
| 3 | import {InputPropertyBase} from "./input-property-base"; |
| 4 | |
| 5 | export class PropertiesGroup { |
| 6 | constructor(propertiesObj?:PropertiesGroup) { |
| 7 | _.forEach(propertiesObj, (properties:Array<PropertyModel>, instance) => { |
| 8 | this[instance] = []; |
| 9 | _.forEach(properties, (property:PropertyModel):void => { |
| 10 | property.resourceInstanceUniqueId = instance; |
| 11 | property.readonly = true; |
| 12 | this[instance].push(new PropertyModel(property)); |
| 13 | }); |
| 14 | }); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | export interface IPropertyModel extends InputPropertyBase { |
| 19 | |
| 20 | //server data |
| 21 | constraints:Array<Object>; |
| 22 | source:string; |
| 23 | |
| 24 | //instance properties |
| 25 | valueUniqueUid:string; |
| 26 | path:Array<string>; |
| 27 | rules:Array<Object>; |
| 28 | propertiesName:string; |
| 29 | input:any; |
| 30 | |
| 31 | //custom properties |
| 32 | resourceInstanceUniqueId:string; |
| 33 | readonly:boolean; |
| 34 | simpleType:string; |
| 35 | } |
| 36 | |
| 37 | export class PropertyModel implements IPropertyModel { |
| 38 | |
| 39 | //server data |
| 40 | uniqueId:string; |
| 41 | name:string; |
| 42 | constraints:Array<Object>; |
| 43 | defaultValue:string; |
| 44 | description:string; |
| 45 | password:boolean; |
| 46 | required:boolean; |
| 47 | type:string; |
| 48 | source:string; |
| 49 | parentUniqueId:string; |
| 50 | schema:SchemaPropertyGroupModel; |
| 51 | componentInstanceId:string; |
| 52 | parentValue:string; |
| 53 | |
| 54 | //instance properties |
| 55 | value:string; |
| 56 | valueUniqueUid:string; |
| 57 | path:Array<string>; |
| 58 | rules:Array<Object>; |
| 59 | propertiesName:string; |
| 60 | input:any; |
| 61 | |
| 62 | //custom properties |
| 63 | resourceInstanceUniqueId:string; |
| 64 | readonly:boolean; |
| 65 | simpleType:string; |
| 66 | filterTerm:string; |
| 67 | isAlreadySelected:boolean; |
| 68 | addOn:string; |
| 69 | |
| 70 | |
| 71 | constructor(property?:PropertyModel) { |
| 72 | if (property) { |
| 73 | this.uniqueId = property.uniqueId; |
| 74 | this.name = property.name; |
| 75 | this.constraints = property.constraints; |
| 76 | this.defaultValue = property.defaultValue; |
| 77 | this.description = property.description; |
| 78 | this.password = property.password; |
| 79 | this.required = property.required; |
| 80 | this.type = property.type; |
| 81 | this.source = property.source; |
| 82 | this.parentUniqueId = property.parentUniqueId; |
| 83 | this.schema = property.schema; |
| 84 | this.value = property.value ? property.value : property.defaultValue; |
| 85 | this.valueUniqueUid = property.valueUniqueUid; |
| 86 | this.path = property.path; |
| 87 | this.rules = property.rules; |
| 88 | this.resourceInstanceUniqueId = property.resourceInstanceUniqueId; |
| 89 | this.readonly = property.readonly; |
| 90 | this.simpleType = property.simpleType; |
| 91 | this.componentInstanceId = property.componentInstanceId; |
| 92 | this.parentValue = property.parentValue; |
| 93 | } |
| 94 | |
| 95 | if (!this.schema || !this.schema.property) { |
| 96 | this.schema = new SchemaPropertyGroupModel(new SchemaProperty()); |
| 97 | } else { |
| 98 | //forcing creating new object, so editing different one than the object in the table |
| 99 | this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property)); |
| 100 | } |
| 101 | if (property && property.uniqueId) { |
| 102 | this.filterTerm = this.name + " " + (this.description || "") + " " + this.type.replace("org.openecomp.datatypes.heat.", ""); |
| 103 | if (this.schema.property && this.schema.property.type) { |
| 104 | this.filterTerm += " " + this.schema.property.type.replace("org.openecomp.datatypes.heat.", ""); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public convertToServerObject:Function = ():string => { |
| 110 | let serverObject = {}; |
| 111 | let mapData = { |
| 112 | "type": this.type, |
| 113 | "required": this.required || false, |
| 114 | "defaultValue": this.defaultValue != "" && this.defaultValue != "[]" && this.defaultValue != "{}" ? this.defaultValue : null, |
| 115 | "description": this.description, |
| 116 | "constraints": this.constraints, |
| 117 | "isPassword": this.password || false, |
| 118 | "schema": this.schema, |
| 119 | "name": this.name |
| 120 | }; |
| 121 | serverObject[this.name] = mapData; |
| 122 | |
| 123 | return JSON.stringify(serverObject); |
| 124 | }; |
| 125 | |
| 126 | public toJSON = ():any => { |
| 127 | // if(!this.resourceInstanceUniqueId){ |
| 128 | // this.value = undefined; |
| 129 | // } |
| 130 | let temp = angular.copy(this); |
| 131 | temp.readonly = undefined; |
| 132 | temp.resourceInstanceUniqueId = undefined; |
| 133 | temp.simpleType = undefined; |
| 134 | temp.value = temp.value === "{}" || temp.value === "[]" ? undefined : temp.value; |
| 135 | temp.defaultValue = temp.defaultValue === "{}" || temp.defaultValue === "[]" ? undefined : temp.defaultValue; |
| 136 | temp.rules = null; //don't send rules to server until feature is fully supported |
| 137 | temp.isAlreadySelected = undefined; |
| 138 | temp.addOn = undefined; |
| 139 | return temp; |
| 140 | }; |
| 141 | } |