2 * Created by rc2122 on 4/24/2017.
5 * Created by rc2122 on 4/23/2017.
7 import {Component, Input, Output, EventEmitter} from "@angular/core";
8 import { PropertyFEModel} from "app/models";
9 import { PropertiesService } from "../../../services/properties.service";
10 import {ComponentType} from "app/utils";
11 import {UUID} from "angular2-uuid";
14 selector: 'map-property',
15 templateUrl: './map-property.component.html',
16 styleUrls: ['../properties-value-inner-table/properties-value-inner-table.component.less']
18 export class MapPropertyComponent {
20 @Input() property: PropertyFEModel;
21 @Input() selectedPropertyId: string;
22 @Input() propertyNameSearchText:string;
24 @Output() valueChanged: EventEmitter<any> = new EventEmitter<any>();
25 @Output() selectChildProperty: EventEmitter<any> = new EventEmitter<PropertyFEModel>();
27 constructor ( private propertiesService:PropertiesService){
30 mapKeys:Array<string>;
33 this.mapKeys = Object.keys(this.property.valueObjectRef);
36 propValueChanged = () => {
37 this.valueChanged.emit(this.property);
40 onChildPropertySelected = (property) => {
41 this.selectChildProperty.emit(property);
44 getNumber = (num:number):Array<any> => {
45 return new Array(num);
48 createNewChildProperty = (mapKey:string):void => {
50 let newProperty: PropertyFEModel = new PropertyFEModel(mapKey,
51 this.property.schema.property.type,
52 UUID.UUID(), this.property,
53 this.property.valueObjectRef[mapKey]);
54 this.propertiesService.createPropertiesTreeForProp(newProperty);
55 this.property.childrenProperties = this.property.childrenProperties || [];
56 this.property.childrenProperties.push(newProperty);
59 //get: new key and the index of this item in the map
60 //This method checks if the new key isn't exist already in the map and update the object and the children array with the new key
61 changeKeyOfMap = (newKey:string, index:number):void => {
62 //let fieldName:string = "mapKey" + this.property.treeNodeId + index;
63 let oldKey:string = Object.keys(this.property.valueObjectRef)[index];
64 let existsKeyIndex:number = Object.keys(this.property.valueObjectRef).indexOf(newKey);
65 if (existsKeyIndex > -1 && existsKeyIndex != index) {
66 //error for exists key validation
68 //remove error for exists key validation and if the form is valid - update the map object
70 angular.forEach(this.property.valueObjectRef,function(value:any,key:string){
72 newObj[newKey] = value;
77 this.property.valueObjectRef = newObj;
78 this.property.parent.valueObjectRef[this.property.name] = this.property.valueObjectRef;//in order to prevent break ref
79 if(this.property.childrenProperties){
80 this.property.childrenProperties[index].name = newKey;//update this property childrenProperties with the new key
85 //get: index of the item in the map
86 //This method removes item from map.
87 deleteMapItem = (index:number):void=> {
88 delete this.property.valueObjectRef[this.mapKeys[index]];
89 this.mapKeys.splice(index, 1);
90 if(this.property.childrenProperties){
91 this.property.childrenProperties.splice(index, 1);
93 if (!this.mapKeys.length) {//only when user removes all pairs of key-value fields - put the default
94 if (this.property.defaultValue) {
95 angular.copy(JSON.parse(this.property.defaultValue), this.property.valueObjectRef);
96 this.mapKeys = Object.keys(this.property.valueObjectRef);
97 if (this.property.schema.property.isDataType){
98 angular.forEach(this.property.valueObjectRef, (value, key) => {
99 this.createNewChildProperty(key);
104 this.valueChanged.emit(this.property);
107 //This method inserts new empty item to map
108 addMapItemFields = ():void => {
109 this.property.valueObjectRef = this.property.valueObjectRef || {};
110 if (this.property.schema.property.isSimpleType){
111 this.property.valueObjectRef[''] = null;
113 if(!this.property.valueObjectRef['']){
114 this.property.valueObjectRef[''] = {};
115 this.createNewChildProperty('');
118 this.mapKeys = Object.keys(this.property.valueObjectRef);