aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/directives/select-property-types/select-data-type-fields-structure/select-data-type-fields-structure.ts
blob: aee4b3b6afa19930306a5b8b5ce57b45d603f5ee (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Created by obarda on 1/27/2016.
 */
'use strict';
import {ValidationUtils} from "app/utils";
import { DataTypesService } from "app/services";
import { DataTypePropertyModel } from "app/models/data-type-properties";
import {DataTypesMap, PropertyModel} from "app/models";

export interface ISelectDataTypeFieldsStructureScope extends ng.IScope {
    parentFormObj:ng.IFormController;
    dataTypeProperties:Array<DataTypePropertyModel>;
    typeName:string;
    valueObjRef:any;
    propertyNameValidationPattern:RegExp;
    fieldsPrefixName:string;
    readOnly:boolean;
    currentTypeDefaultValue:any;
    types:DataTypesMap;
    expandByDefault:boolean;
    expand:boolean;
    expanded:boolean;
    dataTypesService:DataTypesService;
    path:string;
    isParentAlreadyInput:boolean;

    expandAndCollapse():void;
    getValidationPattern(type:string):RegExp;
    validateIntRange(value:string):boolean;
    isAlreadyInput(property:PropertyModel):boolean;
    setSelectedType(property:PropertyModel):void;
    onValueChange(propertyName:string, type:string):void;
}


export class SelectDataTypeFieldsStructureDirective implements ng.IDirective {

    constructor(private DataTypesService:DataTypesService,
                private PropertyNameValidationPattern:RegExp,
                private ValidationUtils:ValidationUtils) {
    }

    scope = {
        valueObjRef: '=',
        typeName: '=',
        parentFormObj: '=',
        fieldsPrefixName: '=',
        readOnly: '=',
        defaultValue: '@',
        expandByDefault: '=',
        path: '@',
        isParentAlreadyInput: '='
    };

    restrict = 'E';
    replace = true;
    template = ():string => {
        return require('./select-data-type-fields-structure.html');
    };
    // public types=Utils.Constants.PROPERTY_DATA.TYPES;

    //get data type properties array and return object with the properties and their default value
    //(for example: get: [{name:"prop1",defaultValue:1 ...},{name:"prop2", defaultValue:"bla bla" ...}]
    //              return: {prop1: 1, prop2: "bla bla"}
    private getDefaultValue = (dataTypeProperties:Array<DataTypePropertyModel>):any => {
        let defaultValue = {};
        for (let i = 0; i < dataTypeProperties.length; i++) {
            if (dataTypeProperties[i].type != 'string') {
                if (!angular.isUndefined(dataTypeProperties[i].defaultValue)) {
                    defaultValue[dataTypeProperties[i].name] = JSON.parse(dataTypeProperties[i].defaultValue);
                }
            } else {
                defaultValue[dataTypeProperties[i].name] = dataTypeProperties[i].defaultValue;
            }
        }
        return defaultValue;
    };

    private initDataOnScope = (scope:ISelectDataTypeFieldsStructureScope, $attr:any):void => {
        scope.dataTypesService = this.DataTypesService;
        scope.dataTypeProperties = angular.copy(this.DataTypesService.getFirsLevelOfDataTypeProperties(scope.typeName));
        if ($attr.defaultValue) {
            scope.currentTypeDefaultValue = JSON.parse($attr.defaultValue);
        } else {
            scope.currentTypeDefaultValue = this.getDefaultValue(scope.dataTypeProperties);
        }

        if (!scope.valueObjRef) {
            scope.valueObjRef = {};
        }

        _.forEach(scope.currentTypeDefaultValue, (value, key)=> {
            if (angular.isUndefined(scope.valueObjRef[key])) {
                if (typeof scope.currentTypeDefaultValue[key] == 'object') {
                    angular.copy(scope.currentTypeDefaultValue[key], scope.valueObjRef[key]);
                } else {
                    scope.valueObjRef[key] = scope.currentTypeDefaultValue[key];
                }
            }
        });
    };

    private rerender = (scope:any):void => {
        scope.expanded = false;
        scope.expand = false;
        if (scope.expandByDefault) {
            scope.expandAndCollapse();
        }
    };

    link = (scope:ISelectDataTypeFieldsStructureScope, element:any, $attr:any) => {
        scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;

        scope.$watchCollection('[typeName,fieldsPrefixName]', (newData:any):void => {
            this.rerender(scope);
        });


        scope.expandAndCollapse = ():void => {
            if (!scope.expanded) {
                this.initDataOnScope(scope, $attr);
                scope.expanded = true;
            }
            scope.expand = !scope.expand;
        };

        scope.getValidationPattern = (type:string):RegExp => {
            return this.ValidationUtils.getValidationPattern(type);
        };

        scope.validateIntRange = (value:string):boolean => {
            return !value || this.ValidationUtils.validateIntRange(value);
        };

        /*
         check if property is alrady declered on the service by meatching the input name & the property name

         */
        scope.isAlreadyInput = (property:PropertyModel):boolean => {
            if (scope.path) {
                if (scope.isParentAlreadyInput) {
                    return true;
                }
                let parentInputName = this.DataTypesService.selectedInstance.normalizedName + '_' + scope.path.replace('#', '_');// set the input parent  as he need to declared as input
                let inputName = parentInputName + '_' + property.name;// set the input name as he need to declared as input
                let selectedProperty = _.find(this.DataTypesService.selectedComponentInputs, (componentInput)=> {
                    if (componentInput.name == parentInputName) { //check if the parent(all the complex) is already declared
                        scope.isParentAlreadyInput = true;
                        return true;
                    } else if (componentInput.name.substring(0, inputName.length) == inputName) { //check if specific property inside the complex
                        return true;
                    }
                    //return componentInput.name == parentInputName || componentInput.name.substring(0,inputName.length) == inputName;//check if the parent(all the complex) is already declared or specific property inside the complex
                });
                if (selectedProperty) {
                    return true;
                }
            }
            return false;
        };

        scope.setSelectedType = (property:PropertyModel):void=> {
            scope.dataTypesService.selectedInput = property;
            scope.dataTypesService.selectedPropertiesName = scope.path + '#' + property.name;
        };

        scope.onValueChange = (propertyName:string, type:string):void => {
            scope.valueObjRef[propertyName] = !angular.isUndefined(scope.valueObjRef[propertyName]) ? scope.valueObjRef[propertyName] : scope.currentTypeDefaultValue[propertyName];
            if (scope.valueObjRef[propertyName] && type != 'string') {
                scope.valueObjRef[propertyName] = JSON.parse(scope.valueObjRef[propertyName]);
            }
        };


    };

    public static factory = (DataTypesService:DataTypesService,
                             PropertyNameValidationPattern:RegExp,
                             ValidationUtils:ValidationUtils)=> {
        return new SelectDataTypeFieldsStructureDirective(DataTypesService, PropertyNameValidationPattern, ValidationUtils);
    };
}

SelectDataTypeFieldsStructureDirective.factory.$inject = ['Sdc.Services.DataTypesService', 'PropertyNameValidationPattern', 'ValidationUtils'];