aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/properties-table/list-property/list-property.component.ts
blob: 96f8c680a2b1a00b52a43321eb38814929e602ea (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
/**
 * Created by rc2122 on 4/23/2017.
 */
import {Component, Input, Output, EventEmitter} from "@angular/core";
import { PropertyFEModel} from "app/models";
import {PropertiesService} from "app/ng2/services/properties.service";
import { ContentAfterLastDotPipe } from "app/ng2/pipes/contentAfterLastDot.pipe";
import {UUID} from "angular2-uuid";
import {ComponentType} from "app/utils";

@Component({
    selector: 'list-property',
    templateUrl: './list-property.component.html',
    styleUrls: ['../properties-value-inner-table/properties-value-inner-table.component.less', './list-property.component.less']
})
export class ListPropertyComponent {

    @Input() property: PropertyFEModel;
    @Input() selectedPropertyId: string;
    @Input() propertyNameSearchText:string;
    
    @Output() valueChanged: EventEmitter<any> = new EventEmitter<any>();
    @Output() selectChildProperty: EventEmitter<any> = new EventEmitter<PropertyFEModel>();

    constructor ( private propertiesService:PropertiesService, private contentAfterLastDotPipe:ContentAfterLastDotPipe ){
    }

    propValueChanged = () => {
        this.valueChanged.emit(this.property);
    };

    onChildPropertySelected = (property) => {
        this.selectChildProperty.emit(property);
    };

    getNumber = (valueObjectRef: any): Array<any> => {
        let num: number = (valueObjectRef) ? valueObjectRef.length : 0;
        return new Array(num);
    }

    createNewChildProperty = ():void => {
        let newProperty: PropertyFEModel = new PropertyFEModel(this.contentAfterLastDotPipe.transform(this.property.schema.property.type),
            this.property.schema.property.type,
            UUID.UUID(),
            this.property,
            this.property.valueObjectRef[this.property.childrenProperties.length]
        );
        this.propertiesService.createPropertiesTreeForProp(newProperty);
        this.property.childrenProperties.push(newProperty);
    }

    addListItem = ():void => {
        this.property.valueObjectRef = this.property.valueObjectRef || [];
        this.property.childrenProperties = this.property.childrenProperties || [];
        if (this.property.schema.property.isSimpleType){
            if( this.property.valueObjectRef.indexOf("") == -1 ) {//prevent insert multiple empty simple type items to list
                this.property.valueObjectRef.push("");
            }
        }else{
            this.property.valueObjectRef[this.property.childrenProperties.length] = {};
            this.property.childrenProperties = this.property.childrenProperties || [];
            this.createNewChildProperty();
            this.valueChanged.emit(this.property);
        }
    }

    deleteListItem = (indexInList:number):void => {
        this.property.valueObjectRef.splice(indexInList, 1);
        if(this.property.childrenProperties){
            this.property.childrenProperties.splice(indexInList, 1);
        }
        if (!this.property.valueObjectRef.length) {//only when user removes all items from list - put the default
            if ( this.property.defaultValue ) {
                angular.copy(JSON.parse(this.property.defaultValue), this.property.valueObjectRef);
                if (this.property.schema.property.isDataType){
                    _.forEach(this.property.valueObjectRef, () => {
                        this.createNewChildProperty();
                    });
                }
            }
        }
        this.valueChanged.emit(this.property);
    }

}