[SDC-31] add mising script got Comformance fix
[sdc.git] / catalog-ui / src / app / ng2 / components / properties-table / map-property / map-property.component.ts
1 /**
2  * Created by rc2122 on 4/24/2017.
3  */
4 /**
5  * Created by rc2122 on 4/23/2017.
6  */
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";
12
13 @Component({
14     selector: 'map-property',
15     templateUrl: './map-property.component.html',
16     styleUrls: ['../properties-value-inner-table/properties-value-inner-table.component.less']
17 })
18 export class MapPropertyComponent {
19
20     @Input() property: PropertyFEModel;
21     @Input() selectedPropertyId: string;
22     @Input() propertyNameSearchText:string;
23     
24     @Output() valueChanged: EventEmitter<any> = new EventEmitter<any>();
25     @Output() selectChildProperty: EventEmitter<any> = new EventEmitter<PropertyFEModel>();
26
27     constructor ( private propertiesService:PropertiesService){
28     }
29
30     mapKeys:Array<string>;
31
32     ngOnInit() {
33         this.mapKeys = Object.keys(this.property.valueObjectRef);
34     }
35
36     propValueChanged = () => {
37         this.valueChanged.emit(this.property);
38     };
39
40     onChildPropertySelected = (property) => {
41         this.selectChildProperty.emit(property);
42     };
43
44     getNumber = (num:number):Array<any> => {
45         return new Array(num);
46     }
47
48     createNewChildProperty = (mapKey:string):void => {
49
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);
57     }
58
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
67         } else {
68             //remove error for exists key validation and if the form is valid - update the map object
69             let newObj = {};
70             angular.forEach(this.property.valueObjectRef,function(value:any,key:string){
71                 if(key == oldKey){
72                     newObj[newKey] = value;
73                 }else{
74                     newObj[key] = value;
75                 }
76             });
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
81             }
82         }
83     }
84
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);
92         }
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);
100                     }, this);
101                 }
102             }
103         }
104         this.valueChanged.emit(this.property);
105     }
106
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;
112         }else{
113             if(!this.property.valueObjectRef['']){
114                 this.property.valueObjectRef[''] = {};
115                 this.createNewChildProperty('');
116             }
117         }
118         this.mapKeys = Object.keys(this.property.valueObjectRef);
119     }
120 }
121