summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/properties-table/dynamic-property/dynamic-property.component.ts
blob: 0ca93a773f8f4292da0f911f713878cbee7eb6cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {Component, Input, Output, EventEmitter} from "@angular/core";
import { PropertyBEModel, PropertyFEModel, DerivedFEProperty, DerivedPropertyType, SchemaPropertyGroupModel, DataTypeModel } from "app/models";
import { PROPERTY_DATA, PROPERTY_TYPES } from 'app/utils';
import { PropertiesUtils } from "app/ng2/pages/properties-assignment/properties.utils";
import { DataTypeService } from "../../../services/data-type.service";
import { UUID } from "angular2-uuid";


@Component({
    selector: 'dynamic-property',
    templateUrl: './dynamic-property.component.html',
    styleUrls: ['./dynamic-property.component.less']
})
export class DynamicPropertyComponent {

    derivedPropertyTypes = DerivedPropertyType; //http://stackoverflow.com/questions/35835984/how-to-use-a-typescript-enum-value-in-an-angular2-ngswitch-statement
    propType: DerivedPropertyType;
    propPath: string;
    isPropertyFEModel: boolean;
    mapOfIDsAndKeys: Map<string, string> = new Map(); //used for map and list

    childrenCanBeDeclared: boolean;
    @Input() canBeDeclared: boolean;
    @Input() property: PropertyFEModel | DerivedFEProperty;
    @Input() propChildren: Array<DerivedFEProperty>;
    @Input() expandedChildId: string;
    @Input() selectedPropertyId: string;

    @Output() valueChanged: EventEmitter<any> = new EventEmitter<any>();
    @Output() expandChild: EventEmitter<string> = new EventEmitter<string>();
    @Output() checkProperty: EventEmitter<string> = new EventEmitter<string>();
    @Output() deleteItem: EventEmitter<string> = new EventEmitter<string>();
    @Output() clickOnPropertyRow: EventEmitter<PropertyFEModel | DerivedFEProperty> = new EventEmitter<PropertyFEModel | DerivedFEProperty>();

    constructor(private propertiesUtils: PropertiesUtils, private dataTypeService: DataTypeService) {
    }

    ngOnInit() {
        this.isPropertyFEModel = this.property instanceof PropertyFEModel;
        if (this.property instanceof PropertyFEModel) {
            this.propType = this.getDerivedPropertyType(this.property.type);
            this.propPath = this.property.name;
        } else {
            this.propType = this.property.derivedDataType;
            this.propPath = this.property.propertiesName;
        }

        this.childrenCanBeDeclared = this.canBeDeclared && this.propType != this.derivedPropertyTypes.MAP && this.propType != this.derivedPropertyTypes.LIST;

        if (this.propType == this.derivedPropertyTypes.LIST || this.propType == this.derivedPropertyTypes.MAP) {
            this.initializeValues();
        }

    }

    initializeValues = () => {
        let tempValue: any;
        if (this.property.value) {
            tempValue = JSON.parse(this.property.value);
            if (!_.isEmpty(tempValue)) {
                tempValue.forEach((element, key) => {
                    let newChildID: string = this.createNewChildProperty(JSON.stringify(element));
                    this.mapOfIDsAndKeys[newChildID] = key;
                    console.log(this.mapOfIDsAndKeys);
                });
            }
        }
        //this.pseudoChildren = [];
        //this.valueObjRef = [];
        //TODO: generate necessary elements for existing values here
        // if (this.propType == this.derivedPropertyTypes.LIST) {
        //     this.valueObjRef = (this.property.value) ? JSON.parse(this.property.value) : [];
        // } else if (this.propType == this.derivedPropertyTypes.MAP) {
        //     this.valueObjRef = (this.property.value)? JSON.parse(this.property.value) : {};
        // }
        console.log(this.property.value);
    }

    onClickPropertyRow = (property, event) => {
        // Because DynamicPropertyComponent is recrusive second time the event is fire event.stopPropagation = undefined
        event && event.stopPropagation && event.stopPropagation();
        this.clickOnPropertyRow.emit(property);
    }

    deleteListOrMapItem  = (itemName: string) => {
        this.propChildren = this.propChildren.filter(prop => prop.propertiesName.indexOf(itemName) != 0); //remove item and children;
    }

    propValueChanged = (property) => {
        console.log("property value change!! Prop type: " + property.type + " New value: " + property.value);
        this.valueChanged.emit(property);
    };

    expandChildById = (id: string) => {
        this.expandedChildId = id;
         this.expandChild.emit(id);
    }

    checkedChange = (propName: string) => {
        this.checkProperty.emit(propName);
    }



    addRows = (): void => { //from within the template, when creating empty item
        let childPropId = this.createNewChildProperty();
        this.expandChildById(this.propPath + "#" + childPropId);
    }

    createNewChildProperty = (value?:string):string => {
        let propUUID:string = UUID.UUID();
        let newProp: DerivedFEProperty;
        if (this.propType == this.derivedPropertyTypes.LIST) { //for list - create new prop of schema type
            newProp = new DerivedFEProperty(propUUID, this.propPath, this.property.schema.property.type, value, true);
        } else { //for map - create new prop of type map, with schema, but with flag that its a child
            newProp = new DerivedFEProperty(propUUID, this.propPath, this.property.type, value, true, this.property.schema);
        }


        this.propChildren = this.propChildren || [];
        this.propChildren.push(newProp);

        //if it's a complex type, add children properties
        if (!this.property.schema.property.isSimpleType) {
            let schemaDataType: DataTypeModel = this.dataTypeService.getDataTypeByTypeName(this.property.schema.property.type);
            this.dataTypeService.getDerivedDataTypeProperties(schemaDataType, this.propChildren, newProp.propertiesName);
            this.propertiesUtils.assignValuesRecursively(JSON.parse(value), this.propChildren, newProp.propertiesName);
            console.log(JSON.stringify(this.propChildren));
        }

        return propUUID;
    }    



    //TODO: remove this and move to somewhere central!! (or make all properties be the same type...)
    getDerivedPropertyType = (type) => {
        if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(type) > -1) {
            return DerivedPropertyType.SIMPLE;
        } else if (type == PROPERTY_TYPES.LIST) {
            return DerivedPropertyType.LIST;
        } else if (type == PROPERTY_TYPES.MAP) {
            return DerivedPropertyType.MAP;
        } else {
            return DerivedPropertyType.COMPLEX;
        }
    }

}